Skip to content

Commit 457226f

Browse files
committed
feat: handle more exit signal
1 parent 769ea15 commit 457226f

File tree

3 files changed

+36
-82
lines changed

3 files changed

+36
-82
lines changed

Cargo.lock

Lines changed: 1 addition & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ authors = ["eatradish <sakiiily@aosc.io>"]
1212
# Cli
1313
clap = { version = "4.5.19", features = ["cargo", "wrap_help", "color", "derive", "env", "string"] }
1414
anyhow = "1.0.89"
15-
ctrlc = "3.5"
1615
dialoguer = "0.12.0"
1716
tabled = { version = "0.20", features = ["ansi"] }
1817
tokio = "1.46.0"
@@ -52,6 +51,7 @@ once_cell = "1.21"
5251
itertools = "0.14.0"
5352
clap-i18n-richformatter = "0.1.0"
5453
debversion = "0.5"
54+
signal-hook = "0.3.18"
5555

5656
# oma crates
5757
oma-utils = { path = "./oma-utils", features = ["dbus", "human-bytes"] }

src/main.rs

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
1-
use std::env::{self, args};
2-
use std::ffi::CString;
3-
use std::fs::{create_dir_all, read_dir, remove_file};
4-
use std::io::{self, IsTerminal, stderr, stdin};
5-
use std::path::{Path, PathBuf};
6-
7-
use std::process::{Command, exit};
8-
use std::sync::{LazyLock, OnceLock};
9-
use std::thread;
10-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
11-
12-
mod args;
13-
mod config;
14-
mod error;
15-
mod install_progress;
16-
mod lang;
17-
mod path_completions;
18-
mod pb;
19-
mod subcommand;
20-
mod table;
21-
mod tui;
22-
mod utils;
23-
1+
use crate::config::Config;
2+
use crate::error::Chain;
3+
use crate::install_progress::osc94_progress;
4+
use crate::subcommand::*;
245
use args::{CliExecuter, OhManagerAilurus};
256
use clap::builder::FalseyValueParser;
267
use clap::{ArgAction, ArgMatches, Args, ColorChoice, CommandFactory, FromArgMatches, arg};
@@ -30,13 +11,25 @@ use error::OutputError;
3011
use i18n_embed::{DesktopLanguageRequester, Localizer};
3112
use lang::LANGUAGE_LOADER;
3213
use oma_console::OmaLayer;
14+
use oma_console::console;
3315
use oma_console::print::{OmaColorFormat, termbg};
3416
use oma_console::writer::{MessageType, Writer, writeln_inner};
3517
use oma_pm::apt::AptConfig;
3618
use oma_utils::dbus::{create_dbus_connection, get_another_oma_status};
3719
use oma_utils::{OsRelease, is_termux};
3820
use reqwest::Client;
39-
use rustix::stdio::stdout;
21+
use signal_hook::consts::{SIGINT, SIGTERM};
22+
use signal_hook::iterator::Signals;
23+
use std::env::{self, args};
24+
use std::ffi::CString;
25+
use std::fs::{create_dir_all, read_dir, remove_file};
26+
use std::io::{self, IsTerminal, stderr, stdin, stdout};
27+
use std::path::{Path, PathBuf};
28+
use std::process::{Command, exit};
29+
use std::sync::atomic::{AtomicBool, Ordering};
30+
use std::sync::{LazyLock, OnceLock};
31+
use std::thread;
32+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
4033
use subcommand::utils::{LockError, is_terminal};
4134
use tokio::runtime::Runtime;
4235
use tracing::{debug, error, info, warn};
@@ -47,14 +40,17 @@ use tracing_subscriber::{EnvFilter, Layer, fmt};
4740
use tui::Tui;
4841
use utils::{is_root, is_ssh_from_loginctl};
4942

50-
use std::sync::atomic::{AtomicBool, Ordering};
51-
52-
use oma_console::console;
53-
54-
use crate::config::Config;
55-
use crate::error::Chain;
56-
use crate::install_progress::osc94_progress;
57-
use crate::subcommand::*;
43+
mod args;
44+
mod config;
45+
mod error;
46+
mod install_progress;
47+
mod lang;
48+
mod path_completions;
49+
mod pb;
50+
mod subcommand;
51+
mod table;
52+
mod tui;
53+
mod utils;
5854

5955
static NOT_DISPLAY_ABORT: AtomicBool = AtomicBool::new(false);
6056
static LOCKED: AtomicBool = AtomicBool::new(false);
@@ -172,7 +168,12 @@ fn main() {
172168
.completer("oma")
173169
.complete();
174170

175-
ctrlc::set_handler(single_handler).expect("oma could not initialize SIGINT handler.");
171+
thread::spawn(|| {
172+
let mut sigs = Signals::new([SIGTERM, SIGINT]).expect("Failed to set signal handler");
173+
for _ in &mut sigs {
174+
single_handler();
175+
}
176+
});
176177

177178
// 要适配额外的插件子命令,所以这里要保留 matches
178179
let (matches, oma) = parse_args();

0 commit comments

Comments
 (0)