Skip to content

Commit

Permalink
Quite a failed attempt to move cursor back up for overdrawing…
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 9, 2020
1 parent 0134e0d commit 04c686b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
25 changes: 25 additions & 0 deletions crosstermion/src/cursor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[cfg(feature = "crossterm")]
mod _impl {
// pub use crossterm::cursor::MoveToPreviousLine;
pub use crossterm::cursor::MoveUp as MoveToPreviousLine;
}
#[cfg(feature = "crossterm")]
pub use _impl::*;

#[cfg(feature = "crossterm")]
#[macro_export]
macro_rules! execute {
($writer:expr $(, $command:expr)* $(,)? ) => {
// Queue each command, then flush
$crate::crossterm::queue!($writer $(, $command)*).and_then(|()| {
$writer.flush().map_err($crate::crossterm::ErrorKind::IoError)
}).map_err($crate::crossterm_utils::into_io_error)
}
}

#[cfg(all(feature = "termion", not(feature = "crossterm")))]
mod _impl {
compile_error!("not implemented");
}
#[cfg(all(feature = "termion", not(feature = "crossterm")))]
pub use _impl::*;
5 changes: 4 additions & 1 deletion crosstermion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ pub mod input;
#[cfg(any(feature = "termion", feature = "crossterm"))]
pub mod terminal;

#[cfg(any(feature = "termion", feature = "crossterm"))]
pub mod cursor;

pub mod color;

#[cfg(feature = "crossterm")]
mod crossterm_utils;
pub mod crossterm_utils;

// Reexports
#[cfg(feature = "ansi_term")]
Expand Down
2 changes: 1 addition & 1 deletion examples/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn launch_ambient_gui(
"line" => smol::Task::blocking(async move {
let output_is_terminal = atty::is(atty::Stream::Stdout);
let mut handle = line::render(
std::io::stdout(),
std::io::stderr(),
progress,
line::Options {
output_is_terminal,
Expand Down
16 changes: 13 additions & 3 deletions src/line/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,25 @@ fn messages(out: &mut impl io::Write, state: &mut State, colored: bool, timestam
Ok(())
}

pub fn lines(out: &mut impl io::Write, progress: &tree::Root, state: &mut State, config: &Options) -> io::Result<()> {
pub fn all(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 Err(io::Error::new(io::ErrorKind::Other, "stop as progress is empty"));
}
state.from_copying = Some(progress.copy_new_messages(&mut state.messages, state.from_copying.take()));
messages(out, state, config.colored, config.timestamp)?;

if config.output_is_terminal {
let level_range = config
.level_filter
.clone()
.unwrap_or(RangeInclusive::new(0, tree::Level::max_value()));
if state.blocks_per_line.len() > 0 {
// Move the cursor back all the way so we can start overwriting the screen
crosstermion::execute!(
out,
crosstermion::cursor::MoveToPreviousLine(state.blocks_per_line.len() as u16)
)?;
}
if state.blocks_per_line.len() < state.tree.len() {
state.blocks_per_line.resize(state.tree.len(), 0);
Expand All @@ -100,7 +105,7 @@ pub fn lines(out: &mut impl io::Write, progress: &tree::Root, state: &mut State,
// fill to the end of line to overwrite what was previously there
writeln!(
out,
"{:width$}",
"{:>width$}",
"",
width = (**blocks_in_last_iteration - current_block_count) as usize
)?;
Expand All @@ -112,8 +117,13 @@ pub fn lines(out: &mut impl io::Write, progress: &tree::Root, state: &mut State,
// overwrite remaining lines that we didn't touch naturally
if state.blocks_per_line.len() > state.tree.len() {
for blocks_in_last_iteration in &state.blocks_per_line[state.tree.len()..] {
writeln!(out, "{:width$}", width = *blocks_in_last_iteration as usize)?;
writeln!(out, "{:>width$}", "", width = *blocks_in_last_iteration as usize)?;
}
// Move cursor back to end of the portion we have actually drawn
crosstermion::execute!(
out,
crosstermion::cursor::MoveToPreviousLine((state.blocks_per_line.len() - state.tree.len()) as u16)
)?;
state.blocks_per_line.resize(state.tree.len(), 0);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/line/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn render(mut out: impl io::Write + Send + 'static, progress: tree::Root, co
if let Ok(Event::Quit) = quit_recv.try_recv() {
break;
}
draw::lines(&mut out, &progress, &mut state, &config)?;
draw::all(&mut out, &progress, &mut state, &config)?;
std::thread::sleep(Duration::from_secs_f32(1.0 / frames_per_second));
}
} else {
Expand All @@ -178,7 +178,7 @@ pub fn render(mut out: impl io::Write + Send + 'static, progress: tree::Root, co
})
.recv(&tick_recv, |_res| Event::Tick);
while let Event::Tick = selector.wait() {
draw::lines(&mut out, &progress, &mut state, &config)?;
draw::all(&mut out, &progress, &mut state, &config)?;
}
}
Ok(())
Expand Down

0 comments on commit 04c686b

Please sign in to comment.