diff options
author | Clement Tsang <34804052+ClementTsang@users.noreply.github.com> | 2022-06-28 23:00:52 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-28 23:00:52 -0400 |
commit | baf844244dc35dfe42f248a999a93acdedc93a02 (patch) | |
tree | 0bb1046cc36ef2255c7d7cfa033a40e34ac0faef /src | |
parent | 2a183c642b0127921eb72e6a497eab54a92d3657 (diff) |
feature: add check for whether the output is to a terminal (#760)
Adds a warning if the user is calling bottom from an environment where the output is not a terminal.
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/main.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 21 |
2 files changed, 18 insertions, 7 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs index e3a86315..4e274ca0 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) @@ -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, @@ -271,15 +271,22 @@ 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. +pub fn check_if_terminal() { + use crossterm::tty::IsTty; + + 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)); + } +} + /// 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<'_>) { |