Skip to content

Commit

Permalink
Sketch draw logic for draw loops that are fast enough
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 8, 2020
1 parent 1712221 commit b4db64e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tui-renderer = ["tui",
"futures-core",
"smol",
"humantime"]
line-renderer = []
line-renderer = ["flume"]
line-renderer-crossterm = ["crosstermion/crossterm"]
line-renderer-termion = ["crosstermion/termion"]
log-renderer = ["log"]
Expand Down Expand Up @@ -59,6 +59,9 @@ crosstermion = { version = "0.1.0", path = "crosstermion", optional = true, defa
# localtime support for tui-renderer
time = { version = "0.2.9", optional = true, features = ["std"], default-features = false }

# line renderer
flume = { version = "0.7.1", optional = true, default-features = false, features = ["select"]}

[package.metadata.docs.rs]
all-features = true

Expand Down
68 changes: 64 additions & 4 deletions src/line.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::tree;
use std::{ops::RangeInclusive, time::Duration};
use std::{io, ops::RangeInclusive, time::Duration};

#[derive(Clone)]
pub struct Options {
Expand All @@ -26,8 +26,68 @@ pub struct Options {
pub keep_running_if_progress_is_empty: bool,
}

pub struct JoinHandle(std::thread::JoinHandle<()>);
pub struct JoinHandle {
inner: Option<std::thread::JoinHandle<io::Result<()>>>,
connect: Option<flume::Sender<Event>>,
}

impl Drop for JoinHandle {
fn drop(&mut self) {
if let Some(chan) = self.connect.take() {
chan.send(Event::Quit).ok();
}
self.inner.take().and_then(|h| h.join().ok());
}
}

enum Event {
Tick,
Quit,
}

const FPS_NEEDED_TO_SHUTDOWN_FAST_ENOUGH: f32 = 4.0;

#[derive(Default)]
struct State {
tree: Vec<(tree::Key, tree::Value)>,
}

fn draw(
out: &mut impl io::Write,
progress: &tree::Root,
state: &mut State,
config: &Options,
) -> io::Result<()> {
progress.sorted_snapshot(&mut state.tree);
if !config.keep_running_if_progress_is_empty && state.tree.is_empty() {
return Ok(());
}
unimplemented!("drawing to be done")
}

pub fn render(
mut out: impl io::Write + Send + 'static,
progress: tree::Root,
config: Options,
) -> JoinHandle {
let (quit_send, quit_recv) = flume::bounded::<Event>(0);
let join = std::thread::spawn(move || {
let mut state = State::default();
if config.frames_per_second >= FPS_NEEDED_TO_SHUTDOWN_FAST_ENOUGH {
loop {
if let Err(flume::TryRecvError::Disconnected) = quit_recv.try_recv() {
break;
}
draw(&mut out, &progress, &mut state, &config)?;
std::thread::sleep(Duration::from_secs_f32(1.0 / config.frames_per_second));
}
} else {
}
Ok(())
});

pub fn render(out: impl std::io::Write, progress: tree::Root, config: Options) -> JoinHandle {
unimplemented!("hello")
JoinHandle {
inner: Some(join),
connect: Some(quit_send),
}
}

0 comments on commit b4db64e

Please sign in to comment.