summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Nguyen <benjamin.van.nguyen@gmail.com>2023-06-28 17:15:07 +0700
committerBenjamin Nguyen <benjamin.van.nguyen@gmail.com>2023-06-28 17:15:07 +0700
commitbe3c86cdd74c924a6c8915bcc65860f6643d66ba (patch)
tree8495926e2aef1a57ae1a77af88d39d021e213c8f
parentb711846999065a3519bf5abe27d6c80cd617d35a (diff)
cleanup tty restore logic
-rw-r--r--src/main.rs48
-rw-r--r--src/progress.rs1
-rw-r--r--src/tree/mod.rs2
-rw-r--r--src/tty/mod.rs6
4 files changed, 32 insertions, 25 deletions
diff --git a/src/main.rs b/src/main.rs
index 2bb1f1a..66d7722 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -64,7 +64,11 @@ mod tty;
mod utils;
fn main() -> ExitCode {
- if let Err(e) = run() {
+ let result = run();
+
+ tty::restore_tty();
+
+ if let Err(e) = result {
eprintln!("{e}");
return ExitCode::FAILURE;
}
@@ -80,35 +84,33 @@ fn run() -> Result<(), Box<dyn Error>> {
}
context::color::no_color_env();
+
styles::init(ctx.no_color());
let indicator = (ctx.stdout_is_tty && !ctx.no_progress).then(progress::Indicator::measure);
- let (tree, ctx) =
- Tree::try_init_and_update_context(ctx, indicator.as_ref()).map_err(|err| {
- if let Some(ref progress) = indicator {
- progress.mailbox().send(Message::RenderReady).unwrap();
+ let (tree, ctx) = match Tree::try_init(ctx, indicator.as_ref()) {
+ Ok(res) => res,
+ Err(err) => {
+ if let Some(thread) = indicator.map(|i| i.join_handle) {
+ thread.join().unwrap()?;
}
- err
- })?;
-
- let output = match ctx.layout {
- layout::Type::Flat => {
- let render = Engine::<Flat>::new(tree, ctx);
- format!("{render}")
- },
- layout::Type::Iflat => {
- let render = Engine::<FlatInverted>::new(tree, ctx);
- format!("{render}")
+ return Err(Box::new(err));
},
- layout::Type::Inverted => {
- let render = Engine::<Inverted>::new(tree, ctx);
- format!("{render}")
- },
- layout::Type::Regular => {
- let render = Engine::<Regular>::new(tree, ctx);
+ };
+
+ macro_rules! compute_output {
+ ($t:ty) => {{
+ let render = Engine::<$t>::new(tree, ctx);
format!("{render}")
- },
+ }};
+ }
+
+ let output = match ctx.layout {
+ layout::Type::Flat => compute_output!(Flat),
+ layout::Type::Iflat => compute_output!(FlatInverted),
+ layout::Type::Inverted => compute_output!(Inverted),
+ layout::Type::Regular => compute_output!(Regular),
};
if let Some(progress) = indicator {
diff --git a/src/progress.rs b/src/progress.rs
index 12b3fde..3e7a740 100644
--- a/src/progress.rs
+++ b/src/progress.rs
@@ -143,7 +143,6 @@ impl<'a> Indicator<'a> {
let stdout = &mut self.stdout;
stdout.execute(terminal::Clear(ClearType::CurrentLine))?;
stdout.execute(cursor::RestorePosition)?;
- stdout.execute(cursor::Show)?;
},
_ => (),
}
diff --git a/src/tree/mod.rs b/src/tree/mod.rs
index f7565a6..e214569 100644
--- a/src/tree/mod.rs
+++ b/src/tree/mod.rs
@@ -52,7 +52,7 @@ impl Tree {
/// Initiates file-system traversal and [Tree] as well as updates the [Context] object with
/// various properties necessary to render output.
- pub fn try_init_and_update_context(
+ pub fn try_init(
mut ctx: Context,
indicator: Option<&IndicatorHandle>,
) -> Result<(Self, Context)> {
diff --git a/src/tty/mod.rs b/src/tty/mod.rs
index 502c3d8..2dbe35b 100644
--- a/src/tty/mod.rs
+++ b/src/tty/mod.rs
@@ -1,4 +1,5 @@
#![allow(clippy::module_name_repetitions)]
+use crossterm::{cursor, ExecutableCommand};
use std::io::{stdin, stdout, IsTerminal};
#[cfg(windows)]
@@ -18,6 +19,11 @@ pub fn stdout_is_tty() -> bool {
stdout().is_terminal()
}
+/// Restore terminal settings.
+pub fn restore_tty() {
+ stdout().execute(cursor::Show).unwrap();
+}
+
/// Attempts to get the current size of the tty's window. Returns `None` if stdout isn't tty or if
/// failed to get width.
pub fn get_window_width(stdout_is_tty: bool) -> Option<usize> {