summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClement Tsang <34804052+ClementTsang@users.noreply.github.com>2020-08-21 13:00:14 -0700
committerGitHub <noreply@github.com>2020-08-21 16:00:14 -0400
commit7475f24a4ebc8c40ba9418684cae08667e1a4a79 (patch)
treed0575985cbf869f09f88835d2e7b8cb3b4d2d901
parenta1766961fe4eb866d013db2c89de0b8ac0a2313a (diff)
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.
-rw-r--r--Cargo.lock59
-rw-r--r--Cargo.toml13
-rw-r--r--src/bin/main.rs16
-rw-r--r--src/lib.rs40
4 files changed, 79 insertions, 49 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6b8219ca..cb50bc78 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -24,7 +24,7 @@ version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -66,7 +66,7 @@ checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -108,7 +108,7 @@ dependencies = [
"nix 0.15.0",
"num-traits",
"uom 0.26.0",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -139,6 +139,7 @@ dependencies = [
"chrono",
"clap",
"crossterm",
+ "ctrlc",
"dirs",
"fern",
"futures",
@@ -156,7 +157,7 @@ dependencies = [
"typed-builder",
"unicode-segmentation",
"unicode-width",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -185,9 +186,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
-version = "0.4.13"
+version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c74d84029116787153e02106bf53e66828452a4b325cc8652b788b5967c0a0b6"
+checksum = "942f72db697d8767c22d46a598e01f2d3b475501ea43d0db4f16d90259182d0b"
dependencies = [
"num-integer",
"num-traits",
@@ -316,7 +317,7 @@ dependencies = [
"mio",
"parking_lot",
"signal-hook",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -325,7 +326,17 @@ version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "057b7146d02fb50175fd7dbe5158f6097f33d02831f43b4ee8ae4ddf67b68f5c"
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]]
@@ -371,7 +382,7 @@ checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
dependencies = [
"libc",
"redox_users",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -566,7 +577,7 @@ dependencies = [
"nix 0.17.0",
"pin-utils",
"uom 0.27.0",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -581,7 +592,7 @@ dependencies = [
"lazy_static",
"libc",
"mach 0.3.2",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -598,7 +609,7 @@ dependencies = [
"libc",
"mach 0.3.2",
"widestring",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -614,7 +625,7 @@ dependencies = [
"libc",
"mach 0.3.2",
"platforms",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -629,7 +640,7 @@ dependencies = [
"lazy_static",
"libc",
"mach 0.3.2",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -667,7 +678,7 @@ dependencies = [
"memchr",
"ntapi",
"ordered-float",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -778,9 +789,9 @@ dependencies = [
[[package]]
name = "log"
-version = "0.4.8"
+version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
dependencies = [
"cfg-if",
]
@@ -869,7 +880,7 @@ checksum = "2ba7c918ac76704fb42afcbbb43891e72731f3dcca3bef2a19786297baf14af7"
dependencies = [
"cfg-if",
"libc",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -910,7 +921,7 @@ version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a31937dea023539c72ddae0e3571deadc1414b300483fa7aaec176168cfa9d2"
dependencies = [
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -995,7 +1006,7 @@ dependencies = [
"libc",
"redox_syscall",
"smallvec",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -1289,7 +1300,7 @@ dependencies = [
"ntapi",
"once_cell",
"rayon",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -1326,7 +1337,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
- "winapi 0.3.8",
+ "winapi 0.3.9",
]
[[package]]
@@ -1456,9 +1467,9 @@ checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]]
name = "winapi"
-version = "0.3.8"
+version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
diff --git a/Cargo.toml b/Cargo.toml
index 4ba087e8..ca04c114 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,8 +25,8 @@ codegen-units = 1
[dependencies]
battery = "0.7.5"
crossterm = "0.17"
-chrono = "0.4.13"
-clap = "2.33.2"
+chrono = "0.4.15"
+clap = "2.33"
dirs = "3.0.1"
futures = "0.3.5"
heim = "0.0.10"
@@ -39,17 +39,18 @@ lazy_static = "1.4.0"
backtrace = "0.3"
serde = {version = "1.0", features = ["derive"] }
unicode-segmentation = "1.6.0"
-unicode-width = "0.1.7"
-libc = "0.2.74"
+unicode-width = "0.1"
+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.9.5", features = ["crossterm"], default-features = false }
# For debugging only...
fern = "0.6.0"
-log = "0.4.8"
+log = "0.4.11"
[target.'cfg(windows)'.dependencies]
-winapi = "0.3.8"
+winapi = "0.3.9"
[dev-dependencies]
assert_cmd = "1.0"
diff --git a/src/bin/main.rs b/src/bin/main.rs
index fdbe7b51..c443bb1f 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -9,7 +9,10 @@ use std::{
boxed::Box,
io::{stdout, Write},
panic,
- sync::mpsc,
+ sync::{
+ atomic::{AtomicBool, Ordering},
+ mpsc, Arc,
+ },
thread,
time::Duration,
};
@@ -89,7 +92,16 @@ fn main() -> error::Result<()> {
// Set panic hook
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)) {
match recv {
BottomEvent::KeyInput(event) => {
diff --git a/src/lib.rs b/src/lib.rs
index 6929e095..f199a705 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -248,20 +248,6 @@ pub fn try_drawing(
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(
config: &Config, painter: &mut canvas::Painter,
) -> error::Result<()> {
@@ -352,6 +338,26 @@ pub fn generate_config_colours(
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
pub fn panic_hook(panic_info: &PanicInfo<'_>) {
let mut stdout = stdout();
@@ -367,7 +373,7 @@ pub fn panic_hook(panic_info: &PanicInfo<'_>) {
let stacktrace: String = format!("{:?}", backtrace::Backtrace::new());
disable_raw_mode().unwrap();
- execute!(stdout, LeaveAlternateScreen, DisableMouseCapture).unwrap();
+ execute!(stdout, DisableMouseCapture, LeaveAlternateScreen).unwrap();
// Print stack trace. Must be done after!
execute!(
@@ -617,14 +623,14 @@ pub fn create_input_thread(
if let Event::Key(key) = event {
if Instant::now().duration_since(keyboard_timer).as_millis() >= 20 {
if sender.send(BottomEvent::KeyInput(key)).is_err() {
- return;
+ break;
}
keyboard_timer = Instant::now();
}
} else if let Event::Mouse(mouse) = event {
if Instant::now().duration_since(mouse_timer).as_millis() >= 20 {
if sender.send(BottomEvent::MouseInput(mouse)).is_err() {
- return;
+ break;
}
mouse_timer = Instant::now();
}