Skip to content

Commit

Permalink
feature: add check for whether the output is to a terminal (#760)
Browse files Browse the repository at this point in the history
Adds a warning if the user is calling bottom from an environment where the output is not a terminal.
  • Loading branch information
ClementTsang committed Jun 29, 2022
1 parent 2a183c6 commit baf8442
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 14 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<'_>) {
Expand Down

0 comments on commit baf8442

Please sign in to comment.