mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-31 01:24:31 +02:00
feature: Add hook to properly clean up in the case of a kill call
Adds a hook to properly clean up the program in case the program gets SIGTERM'd.
This commit is contained in:
parent
a1766961fe
commit
7475f24a4e
59
Cargo.lock
generated
59
Cargo.lock
generated
@ -24,7 +24,7 @@ version = "0.11.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -66,7 +66,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"hermit-abi",
|
"hermit-abi",
|
||||||
"libc",
|
"libc",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -108,7 +108,7 @@ dependencies = [
|
|||||||
"nix 0.15.0",
|
"nix 0.15.0",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"uom 0.26.0",
|
"uom 0.26.0",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -139,6 +139,7 @@ dependencies = [
|
|||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
"ctrlc",
|
||||||
"dirs",
|
"dirs",
|
||||||
"fern",
|
"fern",
|
||||||
"futures",
|
"futures",
|
||||||
@ -156,7 +157,7 @@ dependencies = [
|
|||||||
"typed-builder",
|
"typed-builder",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -185,9 +186,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.13"
|
version = "0.4.15"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
|
checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"num-integer",
|
"num-integer",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
@ -316,7 +317,7 @@ dependencies = [
|
|||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
"signal-hook",
|
"signal-hook",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -325,7 +326,17 @@ version = "0.6.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "057b7146d02fb50175fd7dbe5158f6097f33d02831f43b4ee8ae4ddf67b68f5c"
|
checksum = "057b7146d02fb50175fd7dbe5158f6097f33d02831f43b4ee8ae4ddf67b68f5c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ctrlc"
|
||||||
|
version = "3.1.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d0b676fa23f995faf587496dcd1c80fead847ed58d2da52ac1caca9a72790dd2"
|
||||||
|
dependencies = [
|
||||||
|
"nix 0.17.0",
|
||||||
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -371,7 +382,7 @@ checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"redox_users",
|
"redox_users",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -566,7 +577,7 @@ dependencies = [
|
|||||||
"nix 0.17.0",
|
"nix 0.17.0",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
"uom 0.27.0",
|
"uom 0.27.0",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -581,7 +592,7 @@ dependencies = [
|
|||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"mach 0.3.2",
|
"mach 0.3.2",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -598,7 +609,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"mach 0.3.2",
|
"mach 0.3.2",
|
||||||
"widestring",
|
"widestring",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -614,7 +625,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"mach 0.3.2",
|
"mach 0.3.2",
|
||||||
"platforms",
|
"platforms",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -629,7 +640,7 @@ dependencies = [
|
|||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"mach 0.3.2",
|
"mach 0.3.2",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -667,7 +678,7 @@ dependencies = [
|
|||||||
"memchr",
|
"memchr",
|
||||||
"ntapi",
|
"ntapi",
|
||||||
"ordered-float",
|
"ordered-float",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -778,9 +789,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.8"
|
version = "0.4.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
|
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
@ -869,7 +880,7 @@ checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -910,7 +921,7 @@ version = "0.3.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2"
|
checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -995,7 +1006,7 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1289,7 +1300,7 @@ dependencies = [
|
|||||||
"ntapi",
|
"ntapi",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rayon",
|
"rayon",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1326,7 +1337,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
|
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"winapi 0.3.8",
|
"winapi 0.3.9",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1456,9 +1467,9 @@ checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi"
|
name = "winapi"
|
||||||
version = "0.3.8"
|
version = "0.3.9"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"winapi-i686-pc-windows-gnu",
|
"winapi-i686-pc-windows-gnu",
|
||||||
"winapi-x86_64-pc-windows-gnu",
|
"winapi-x86_64-pc-windows-gnu",
|
||||||
|
13
Cargo.toml
13
Cargo.toml
@ -25,8 +25,8 @@ codegen-units = 1
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
battery = "0.7.5"
|
battery = "0.7.5"
|
||||||
crossterm = "0.17"
|
crossterm = "0.17"
|
||||||
chrono = "0.4.13"
|
chrono = "0.4.15"
|
||||||
clap = "2.33.2"
|
clap = "2.33"
|
||||||
dirs = "3.0.1"
|
dirs = "3.0.1"
|
||||||
futures = "0.3.5"
|
futures = "0.3.5"
|
||||||
heim = "0.0.10"
|
heim = "0.0.10"
|
||||||
@ -39,17 +39,18 @@ lazy_static = "1.4.0"
|
|||||||
backtrace = "0.3"
|
backtrace = "0.3"
|
||||||
serde = {version = "1.0", features = ["derive"] }
|
serde = {version = "1.0", features = ["derive"] }
|
||||||
unicode-segmentation = "1.6.0"
|
unicode-segmentation = "1.6.0"
|
||||||
unicode-width = "0.1.7"
|
unicode-width = "0.1"
|
||||||
libc = "0.2.74"
|
libc = "0.2"
|
||||||
|
ctrlc = {version = "3.1", features = ["termination"]}
|
||||||
# tui = {version = "0.10.0", features = ["crossterm"], default-features = false, git = "https://github.com/fdehau/tui-rs.git"}
|
# tui = {version = "0.10.0", features = ["crossterm"], default-features = false, git = "https://github.com/fdehau/tui-rs.git"}
|
||||||
tui = {version = "0.9.5", features = ["crossterm"], default-features = false }
|
tui = {version = "0.9.5", features = ["crossterm"], default-features = false }
|
||||||
|
|
||||||
# For debugging only...
|
# For debugging only...
|
||||||
fern = "0.6.0"
|
fern = "0.6.0"
|
||||||
log = "0.4.8"
|
log = "0.4.11"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = "0.3.8"
|
winapi = "0.3.9"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
assert_cmd = "1.0"
|
assert_cmd = "1.0"
|
||||||
|
@ -9,7 +9,10 @@ use std::{
|
|||||||
boxed::Box,
|
boxed::Box,
|
||||||
io::{stdout, Write},
|
io::{stdout, Write},
|
||||||
panic,
|
panic,
|
||||||
sync::mpsc,
|
sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
mpsc, Arc,
|
||||||
|
},
|
||||||
thread,
|
thread,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
@ -89,7 +92,16 @@ fn main() -> error::Result<()> {
|
|||||||
// Set panic hook
|
// Set panic hook
|
||||||
panic::set_hook(Box::new(|info| panic_hook(info)));
|
panic::set_hook(Box::new(|info| panic_hook(info)));
|
||||||
|
|
||||||
loop {
|
// Set termination hook
|
||||||
|
let is_terminated = Arc::new(AtomicBool::new(false));
|
||||||
|
let ist_clone = is_terminated.clone();
|
||||||
|
ctrlc::set_handler(move || {
|
||||||
|
ist_clone.store(true, Ordering::SeqCst);
|
||||||
|
termination_hook();
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
while !is_terminated.load(Ordering::SeqCst) {
|
||||||
if let Ok(recv) = receiver.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
|
if let Ok(recv) = receiver.recv_timeout(Duration::from_millis(TICK_RATE_IN_MILLISECONDS)) {
|
||||||
match recv {
|
match recv {
|
||||||
BottomEvent::KeyInput(event) => {
|
BottomEvent::KeyInput(event) => {
|
||||||
|
40
src/lib.rs
40
src/lib.rs
@ -248,20 +248,6 @@ pub fn try_drawing(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cleanup_terminal(
|
|
||||||
terminal: &mut tui::terminal::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>,
|
|
||||||
) -> error::Result<()> {
|
|
||||||
disable_raw_mode()?;
|
|
||||||
execute!(
|
|
||||||
terminal.backend_mut(),
|
|
||||||
DisableMouseCapture,
|
|
||||||
LeaveAlternateScreen
|
|
||||||
)?;
|
|
||||||
terminal.show_cursor()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn generate_config_colours(
|
pub fn generate_config_colours(
|
||||||
config: &Config, painter: &mut canvas::Painter,
|
config: &Config, painter: &mut canvas::Painter,
|
||||||
) -> error::Result<()> {
|
) -> error::Result<()> {
|
||||||
@ -352,6 +338,26 @@ pub fn generate_config_colours(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn cleanup_terminal(
|
||||||
|
terminal: &mut tui::terminal::Terminal<tui::backend::CrosstermBackend<std::io::Stdout>>,
|
||||||
|
) -> error::Result<()> {
|
||||||
|
disable_raw_mode()?;
|
||||||
|
execute!(
|
||||||
|
terminal.backend_mut(),
|
||||||
|
DisableMouseCapture,
|
||||||
|
LeaveAlternateScreen
|
||||||
|
)?;
|
||||||
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn termination_hook() {
|
||||||
|
let mut stdout = stdout();
|
||||||
|
disable_raw_mode().unwrap();
|
||||||
|
execute!(stdout, DisableMouseCapture, LeaveAlternateScreen).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
/// Based on https://github.com/Rigellute/spotify-tui/blob/master/src/main.rs
|
/// Based on https://github.com/Rigellute/spotify-tui/blob/master/src/main.rs
|
||||||
pub fn panic_hook(panic_info: &PanicInfo<'_>) {
|
pub fn panic_hook(panic_info: &PanicInfo<'_>) {
|
||||||
let mut stdout = stdout();
|
let mut stdout = stdout();
|
||||||
@ -367,7 +373,7 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) {
|
|||||||
let stacktrace: String = format!("{:?}", backtrace::Backtrace::new());
|
let stacktrace: String = format!("{:?}", backtrace::Backtrace::new());
|
||||||
|
|
||||||
disable_raw_mode().unwrap();
|
disable_raw_mode().unwrap();
|
||||||
execute!(stdout, LeaveAlternateScreen, DisableMouseCapture).unwrap();
|
execute!(stdout, DisableMouseCapture, LeaveAlternateScreen).unwrap();
|
||||||
|
|
||||||
// Print stack trace. Must be done after!
|
// Print stack trace. Must be done after!
|
||||||
execute!(
|
execute!(
|
||||||
@ -617,14 +623,14 @@ pub fn create_input_thread(
|
|||||||
if let Event::Key(key) = event {
|
if let Event::Key(key) = event {
|
||||||
if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 {
|
if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 {
|
||||||
if sender.send(BottomEvent::KeyInput(key)).is_err() {
|
if sender.send(BottomEvent::KeyInput(key)).is_err() {
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
keyboard_timer = Instant::now();
|
keyboard_timer = Instant::now();
|
||||||
}
|
}
|
||||||
} else if let Event::Mouse(mouse) = event {
|
} else if let Event::Mouse(mouse) = event {
|
||||||
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
|
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
|
||||||
if sender.send(BottomEvent::MouseInput(mouse)).is_err() {
|
if sender.send(BottomEvent::MouseInput(mouse)).is_err() {
|
||||||
return;
|
break;
|
||||||
}
|
}
|
||||||
mouse_timer = Instant::now();
|
mouse_timer = Instant::now();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user