refactor: add fallback process signal (#1112)

* refactor: add fallback process signal

* also update dialog logic to be able to fall back in some cases
This commit is contained in:
Clement Tsang 2023-04-22 00:19:05 -04:00 committed by GitHub
parent 8b81dfba47
commit 4a86b1c21b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 21 deletions

View File

@ -143,16 +143,26 @@ pub struct App {
pub filters: DataFilters, pub filters: DataFilters,
} }
// TODO: Should probably set a fallback max signal/not supported for this. cfg_if::cfg_if! {
#[cfg(target_os = "windows")] if #[cfg(target_os = "linux")] {
const MAX_SIGNAL: usize = 1; /// The max signal we can send to a process on Linux.
#[cfg(target_os = "linux")] pub const MAX_PROCESS_SIGNAL: usize = 64;
const MAX_SIGNAL: usize = 64; } else if #[cfg(target_os = "macos")] {
#[cfg(target_os = "macos")] /// The max signal we can send to a process on macOS.
const MAX_SIGNAL: usize = 31; pub const MAX_PROCESS_SIGNAL: usize = 31;
// https://www.freebsd.org/cgi/man.cgi?query=signal&apropos=0&sektion=3&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html } else if #[cfg(target_os = "freebsd")] {
#[cfg(target_os = "freebsd")] /// The max signal we can send to a process on FreeBSD.
const MAX_SIGNAL: usize = 33; /// See [https://www.freebsd.org/cgi/man.cgi?query=signal&apropos=0&sektion=3&manpath=FreeBSD+13.1-RELEASE+and+Ports&arch=default&format=html]
/// for more details.
pub const MAX_PROCESS_SIGNAL: usize = 33;
} else if #[cfg(target_os = "windows")] {
/// The max signal we can send to a process. For Windows, we only have support for one signal (kill).
pub const MAX_PROCESS_SIGNAL: usize = 1;
} else {
/// The max signal we can send to a process. As a fallback, we only support one signal (kill).
pub const MAX_PROCESS_SIGNAL: usize = 1;
}
}
impl App { impl App {
pub fn reset(&mut self) { pub fn reset(&mut self) {
@ -811,7 +821,7 @@ impl App {
if self.delete_dialog_state.is_showing_dd { if self.delete_dialog_state.is_showing_dd {
let mut new_signal = match self.delete_dialog_state.selected_signal { let mut new_signal = match self.delete_dialog_state.selected_signal {
KillSignal::Cancel => 8, KillSignal::Cancel => 8,
KillSignal::Kill(signal) => min(signal + 8, MAX_SIGNAL), KillSignal::Kill(signal) => min(signal + 8, MAX_PROCESS_SIGNAL),
}; };
if new_signal > 31 && new_signal < 42 { if new_signal > 31 && new_signal < 42 {
new_signal += 2; new_signal += 2;
@ -1930,7 +1940,7 @@ impl App {
self.help_dialog_state.scroll_state.current_scroll_index = self.help_dialog_state.scroll_state.current_scroll_index =
self.help_dialog_state.scroll_state.max_scroll_index; self.help_dialog_state.scroll_state.max_scroll_index;
} else if self.delete_dialog_state.is_showing_dd { } else if self.delete_dialog_state.is_showing_dd {
self.delete_dialog_state.selected_signal = KillSignal::Kill(MAX_SIGNAL); self.delete_dialog_state.selected_signal = KillSignal::Kill(MAX_PROCESS_SIGNAL);
} }
} }

View File

@ -10,7 +10,7 @@ use tui::{
}; };
use crate::{ use crate::{
app::{App, KillSignal}, app::{App, KillSignal, MAX_PROCESS_SIGNAL},
canvas::Painter, canvas::Painter,
widgets::ProcWidgetMode, widgets::ProcWidgetMode,
}; };
@ -66,7 +66,7 @@ impl Painter {
fn draw_dd_confirm_buttons<B: Backend>( fn draw_dd_confirm_buttons<B: Backend>(
&self, f: &mut Frame<'_, B>, button_draw_loc: &Rect, app_state: &mut App, &self, f: &mut Frame<'_, B>, button_draw_loc: &Rect, app_state: &mut App,
) { ) {
if cfg!(target_os = "windows") || !app_state.app_config_fields.is_advanced_kill { if MAX_PROCESS_SIGNAL == 1 || !app_state.app_config_fields.is_advanced_kill {
let (yes_button, no_button) = match app_state.delete_dialog_state.selected_signal { let (yes_button, no_button) = match app_state.delete_dialog_state.selected_signal {
KillSignal::Kill(_) => ( KillSignal::Kill(_) => (
Span::styled("Yes", self.colours.currently_selected_text_style), Span::styled("Yes", self.colours.currently_selected_text_style),
@ -135,7 +135,7 @@ impl Painter {
} else { } else {
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
{ {
let signal_text; let signal_text: Vec<&str>;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
signal_text = vec![ signal_text = vec![
@ -401,11 +401,18 @@ impl Painter {
draw_loc, draw_loc,
); );
let btn_height = let btn_height = {
if cfg!(target_os = "windows") || !app_state.app_config_fields.is_advanced_kill { cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] {
3
} else {
if !app_state.app_config_fields.is_advanced_kill {
3 3
} else { } else {
20 20
}
}
}
}; };
// Now draw buttons if needed... // Now draw buttons if needed...