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.
This commit is contained in:
Clement Tsang 2022-06-28 23:00:52 -04:00 committed by GitHub
parent 2a183c642b
commit baf844244d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 8 deletions

View File

@ -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.
- [#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
## Bug Fixes

View File

@ -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)

View File

@ -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<'_>) {