From 0e347cd89cec1bdec6a0d2a56e0ee64b923674d3 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Tue, 28 Jun 2022 22:35:47 -0400 Subject: [PATCH 1/4] feature: add check for tty --- CHANGELOG.md | 4 +++- src/bin/main.rs | 6 ++++++ src/lib.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99042bafa..f84c68ef9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#711](https://github.com/ClementTsang/bottom/pull/711): Fix building in Rust beta 1.61 due to `as_ref()` calls causing type inference issues. - - [#717](https://github.com/ClementTsang/bottom/pull/717): Fix clicking on empty space in tables selecting the very last entry of a list in some cases. +- [#717](https://github.com/ClementTsang/bottom/pull/717): Fix clicking on empty space in tables selecting the very last entry of a list in some cases. ## Changes @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#676](https://github.com/ClementTsang/bottom/pull/676): Adds support for NVIDIA GPU temperature sensors. +- [](): Adds a check for whether bottom is being run in a terminal. + ## [0.6.8] - 2022-02-01 ## Bug Fixes diff --git a/src/bin/main.rs b/src/bin/main.rs index e3a863151..effe49136 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -57,6 +57,11 @@ fn main() -> Result<()> { let mut painter = canvas::Painter::init(widget_layout, &config, get_color_scheme(&matches, &config)?)?; + // Check if the current environment is in a terminal. + if !check_if_terminal_and_wait() { + return Ok(()); + } + // Create termination mutex and cvar #[allow(clippy::mutex_atomic)] let thread_termination_lock = Arc::new(Mutex::new(false)); @@ -109,6 +114,7 @@ fn main() -> Result<()> { enable_raw_mode()?; let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?; + terminal.clear()?; terminal.hide_cursor()?; diff --git a/src/lib.rs b/src/lib.rs index 15b92f24f..6f0c83463 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -271,15 +271,34 @@ pub fn cleanup_terminal( )?; terminal.show_cursor()?; - // if is_debug { - // let mut tmp_dir = std::env::temp_dir(); - // tmp_dir.push("bottom_debug.log"); - // println!("Your debug file is located at {:?}", tmp_dir.as_os_str()); - // } - Ok(()) } +/// Check and report to the user if the current environment is not a terminal. If it isn't a terminal, wait +/// for user input and return `false` if the caller should bail. Otherwise, return `true`. +pub fn check_if_terminal_and_wait() -> bool { + use crossterm::tty::IsTty; + + let out = stdout(); + if !out.is_tty() { + println!( + "Warning: bottom is not being output to a terminal. Things might not work properly." + ); + println!("Press Ctrl-c or input q to exit, or any other key to continue."); + + match read().unwrap() { + Event::Key(event) => match event.modifiers { + KeyModifiers::NONE => !matches!(event.code, KeyCode::Char('q')), + KeyModifiers::CONTROL => !matches!(event.code, KeyCode::Char('c')), + _ => true, + }, + _ => true, + } + } else { + true + } +} + /// A panic hook to properly restore the terminal in the case of a panic. /// Based on [spotify-tui's implementation](https://github.com/Rigellute/spotify-tui/blob/master/src/main.rs). pub fn panic_hook(panic_info: &PanicInfo<'_>) { From 76d492226188c503f11651fcb3d6be6d29f34260 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Tue, 28 Jun 2022 22:38:25 -0400 Subject: [PATCH 2/4] add changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f84c68ef9..9ab8a259b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#676](https://github.com/ClementTsang/bottom/pull/676): Adds support for NVIDIA GPU temperature sensors. -- [](): Adds a check for whether bottom is being run in a terminal. +- [#760](https://github.com/ClementTsang/bottom/pull/760): Adds a check for whether bottom is being run in a terminal. ## [0.6.8] - 2022-02-01 From b786fdf6d41fd4df8d810e61480706cfe13ea23b Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Tue, 28 Jun 2022 22:44:56 -0400 Subject: [PATCH 3/4] simplify and just output message --- src/bin/main.rs | 10 ++++------ src/lib.rs | 17 ++--------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index effe49136..4e274ca0d 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -33,6 +33,10 @@ fn main() -> Result<()> { utils::logging::init_logger(log::LevelFilter::Debug, std::ffi::OsStr::new("debug.log"))?; } + // Check if the current environment is in a terminal. + check_if_terminal(); + + // Read from config file. let config_path = read_config(matches.value_of("config_location")) .context("Unable to access the given config file location.")?; let mut config: Config = create_or_get_config(&config_path) @@ -57,11 +61,6 @@ fn main() -> Result<()> { let mut painter = canvas::Painter::init(widget_layout, &config, get_color_scheme(&matches, &config)?)?; - // Check if the current environment is in a terminal. - if !check_if_terminal_and_wait() { - return Ok(()); - } - // Create termination mutex and cvar #[allow(clippy::mutex_atomic)] let thread_termination_lock = Arc::new(Mutex::new(false)); @@ -114,7 +113,6 @@ fn main() -> Result<()> { enable_raw_mode()?; let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?; - terminal.clear()?; terminal.hide_cursor()?; diff --git a/src/lib.rs b/src/lib.rs index 6f0c83463..d783983b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -274,9 +274,8 @@ pub fn cleanup_terminal( Ok(()) } -/// Check and report to the user if the current environment is not a terminal. If it isn't a terminal, wait -/// for user input and return `false` if the caller should bail. Otherwise, return `true`. -pub fn check_if_terminal_and_wait() -> bool { +/// Check and report to the user if the current environment is not a terminal. +pub fn check_if_terminal() { use crossterm::tty::IsTty; let out = stdout(); @@ -284,18 +283,6 @@ pub fn check_if_terminal_and_wait() -> bool { println!( "Warning: bottom is not being output to a terminal. Things might not work properly." ); - println!("Press Ctrl-c or input q to exit, or any other key to continue."); - - match read().unwrap() { - Event::Key(event) => match event.modifiers { - KeyModifiers::NONE => !matches!(event.code, KeyCode::Char('q')), - KeyModifiers::CONTROL => !matches!(event.code, KeyCode::Char('c')), - _ => true, - }, - _ => true, - } - } else { - true } } From 43ae0d1b53061d6fbcc29b7a45f098669b42d941 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Tue, 28 Jun 2022 22:54:59 -0400 Subject: [PATCH 4/4] write to stderr instead --- src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d783983b8..6befaf157 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,7 @@ extern crate log; use std::{ boxed::Box, fs, - io::{stdout, Write}, + io::{stderr, stdout, Write}, panic::PanicInfo, path::PathBuf, sync::Arc, @@ -278,11 +278,12 @@ pub fn cleanup_terminal( pub fn check_if_terminal() { use crossterm::tty::IsTty; - let out = stdout(); - if !out.is_tty() { - println!( + if !stdout().is_tty() { + eprintln!( "Warning: bottom is not being output to a terminal. Things might not work properly." ); + stderr().flush().unwrap(); + thread::sleep(Duration::from_secs(1)); } }