From d35372b20a675562c579bed0ed18a66537a9e5a9 Mon Sep 17 00:00:00 2001 From: mokurin000 <1348292515a@gmail.com> Date: Wed, 30 Jul 2025 19:15:48 +0800 Subject: [PATCH] feat: correctly handle interrupt --- .gitignore | 4 +++- Cargo.lock | 23 +++++++++++++++++++++++ sdgb-cli/Cargo.toml | 1 + sdgb-cli/src/main.rs | 19 +++++++++++++++++-- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 03791df..4032def 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /target /*.txt -/players.redb \ No newline at end of file + +/players.redb +/players.json diff --git a/Cargo.lock b/Cargo.lock index b516731..15aeb61 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -500,6 +500,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctrlc" +version = "3.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46f93780a459b7d656ef7f071fe699c4d3d2cb201c4b24d085b6ddc505276e73" +dependencies = [ + "nix", + "windows-sys 0.59.0", +] + [[package]] name = "curl" version = "0.4.48" @@ -925,6 +935,18 @@ dependencies = [ "getrandom 0.2.16", ] +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -1428,6 +1450,7 @@ name = "sdgb-cli" version = "0.1.0" dependencies = [ "compio", + "ctrlc", "futures-util", "nyquest-preset", "palc", diff --git a/sdgb-cli/Cargo.toml b/sdgb-cli/Cargo.toml index a675a2b..8d99876 100644 --- a/sdgb-cli/Cargo.toml +++ b/sdgb-cli/Cargo.toml @@ -31,3 +31,4 @@ spdlog-rs = { version = "0.4.3", default-features = false, features = [ ] } futures-util = "0.3.31" redb = { version = "2.6.1", optional = true } +ctrlc = { version = "3.4.7", features = ["termination"] } diff --git a/sdgb-cli/src/main.rs b/sdgb-cli/src/main.rs index c58a97a..b9200ce 100644 --- a/sdgb-cli/src/main.rs +++ b/sdgb-cli/src/main.rs @@ -1,6 +1,7 @@ use std::{ fs::OpenOptions, - io::{BufRead, stdin}, + io::{self, BufRead}, + sync::atomic::{AtomicBool, Ordering}, }; use futures_util::StreamExt; @@ -28,10 +29,17 @@ use crate::commands::Cli; mod cache; mod commands; +static EARLY_QUIT: AtomicBool = AtomicBool::new(false); + #[cfg_attr(feature = "compio", compio::main)] #[cfg_attr(feature = "tokio", tokio::main)] async fn main() -> Result<(), Box> { nyquest_preset::register(); + + ctrlc::set_handler(|| { + EARLY_QUIT.store(true, Ordering::Relaxed); + })?; + let cmd = ::parse(); let client = ClientBuilder::default().build_async().await?; @@ -91,7 +99,7 @@ async fn main() -> Result<(), Box> { } commands::Commands::ListAllUser => { - let mut stdin = stdin().lock(); + let mut stdin = io::stdin().lock(); let mut buf = String::new(); let mut user_ids = Vec::new(); @@ -117,6 +125,10 @@ async fn main() -> Result<(), Box> { let players = futures_util::stream::iter(user_ids) .map(async |user_id| { + if EARLY_QUIT.load(Ordering::Relaxed) { + return Err("early skip due to ctrl-c")?; + } + #[cfg(feature = "cache")] { use redb::ReadableTable; @@ -170,6 +182,9 @@ async fn main() -> Result<(), Box> { .collect::>() .await; + #[cfg(feature = "cache")] + let _ = write.commit(); + let output = OpenOptions::new() .write(true) .truncate(true)