From 45b2522929360c6157593237bec9adbee27e93d3 Mon Sep 17 00:00:00 2001 From: Adarsh Das <59739923+Saphereye@users.noreply.github.com> Date: Mon, 17 Nov 2025 16:33:46 +0530 Subject: [PATCH] feature: add readonly mode (#1861) * init * Fixed code smells * Updated documentation as per feedback --- docs/content/configuration/command-line-options.md | 1 + docs/content/configuration/config-file/flags.md | 1 + sample_configs/default_config.toml | 3 +++ src/app.rs | 5 +++++ src/constants.rs | 3 +++ src/options.rs | 2 ++ src/options/args.rs | 10 ++++++++++ src/options/config/flags.rs | 1 + 8 files changed, 26 insertions(+) diff --git a/docs/content/configuration/command-line-options.md b/docs/content/configuration/command-line-options.md index b4b99b2d..4d97dc8c 100644 --- a/docs/content/configuration/command-line-options.md +++ b/docs/content/configuration/command-line-options.md @@ -33,6 +33,7 @@ see information on these options by running `btm -h`, or run `btm --help` to dis | `-S, --case_sensitive` | Enables case sensitivity by default when searching. | | `-u, --current_usage` | Calculates process CPU usage as a percentage of current usage rather than total usage. | | `--disable_advanced_kill` | Hides additional stopping options on Unix-like systems. | +| `--read_only` | Prevents performing any actions that affect the system (e.g. stopping processes). | | `--get_threads` | Also gather process thread information. | | `-g, --group_processes` | Groups processes with the same name by default. No effect if `--tree` is set. | | `--hide_k_threads` | Hide kernel threads by default. | diff --git a/docs/content/configuration/config-file/flags.md b/docs/content/configuration/config-file/flags.md index 3c9e056f..6db02c4f 100644 --- a/docs/content/configuration/config-file/flags.md +++ b/docs/content/configuration/config-file/flags.md @@ -41,6 +41,7 @@ each time: | `show_table_scroll_position` | Boolean | Shows the scroll position tracker in table widgets. | | `process_command` | Boolean | Show processes as their commands by default. | | `disable_advanced_kill` | Boolean | Disable being able to send signals to processes on supported Unix-like systems. Only available on Linux, macOS, and FreeBSD. | +| `read_only` | Boolean | Prevents performing any actions that affect the system (e.g. stopping processes). | | `network_use_binary_prefix` | Boolean | Displays the network widget with binary prefixes. | | `network_use_bytes` | Boolean | Displays the network widget using bytes. | | `network_use_log` | Boolean | Displays the network widget with a log scale. | diff --git a/sample_configs/default_config.toml b/sample_configs/default_config.toml index d9ec2773..a3609489 100644 --- a/sample_configs/default_config.toml +++ b/sample_configs/default_config.toml @@ -106,6 +106,9 @@ # Hides advanced options to stop a process on Unix-like systems. #disable_advanced_kill = false +# Prevents performing any actions that affect the system (e.g. stopping processes). +#read_only = false + # Hides the kernel threads #hide_k_threads = false diff --git a/src/app.rs b/src/app.rs index 076b2776..48318987 100644 --- a/src/app.rs +++ b/src/app.rs @@ -57,6 +57,7 @@ pub struct AppConfigFields { pub show_table_scroll_position: bool, #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] pub is_advanced_kill: bool, + pub is_read_only: bool, #[cfg(target_os = "linux")] pub hide_k_threads: bool, #[cfg(feature = "zfs")] @@ -991,6 +992,10 @@ impl App { /// /// TODO: This ideally gets abstracted out into a separate widget. pub(crate) fn kill_current_process(&mut self) { + if self.app_config_fields.is_read_only { + return; + } + if let Some(pws) = self .states .proc_state diff --git a/src/constants.rs b/src/constants.rs index 157b72aa..1282d64a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -352,6 +352,9 @@ pub(crate) const CONFIG_TEXT: &str = r#"# This is a default config file for bott # Hides advanced options to stop a process on Unix-like systems. #disable_advanced_kill = false +# Prevents performing any actions that affect the system (e.g. stopping processes). +#read_only = false + # Hides the kernel threads #hide_k_threads = false diff --git a/src/options.rs b/src/options.rs index 702515d6..94ebdf17 100644 --- a/src/options.rs +++ b/src/options.rs @@ -242,6 +242,7 @@ pub(crate) fn init_app(args: BottomArgs, config: Config) -> Result<(App, BottomL let is_default_command = is_flag_enabled!(process_command, args.process, config); #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] let is_advanced_kill = !(is_flag_enabled!(disable_advanced_kill, args.process, config)); + let is_read_only = is_flag_enabled!(read_only, args.process, config); #[cfg(target_os = "linux")] let hide_k_threads = is_flag_enabled!(hide_k_threads, args.process, config); @@ -321,6 +322,7 @@ pub(crate) fn init_app(args: BottomArgs, config: Config) -> Result<(App, BottomL ), #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] is_advanced_kill, + is_read_only, #[cfg(target_os = "linux")] hide_k_threads, memory_legend_position, diff --git a/src/options/args.rs b/src/options/args.rs index 8743d08d..4c849292 100644 --- a/src/options/args.rs +++ b/src/options/args.rs @@ -324,6 +324,16 @@ pub struct ProcessArgs { )] pub disable_advanced_kill: bool, + #[arg( + long, + action = ArgAction::SetTrue, + help = "Prevents performing any actions that affect the system.", + long_help = "Prevents performing any actions that affect the system. Disables operations such as stopping or sending signals \ + to processes.", + alias = "read-only" + )] + pub read_only: bool, + #[cfg(target_os = "linux")] #[arg( long, diff --git a/src/options/config/flags.rs b/src/options/config/flags.rs index 375db3d2..e2fb4354 100644 --- a/src/options/config/flags.rs +++ b/src/options/config/flags.rs @@ -40,6 +40,7 @@ pub(crate) struct GeneralConfig { pub(crate) process_command: Option, // #[cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd"))] pub(crate) disable_advanced_kill: Option, // This does nothing on Windows, but we leave it enabled to make the config file consistent across platforms. + pub(crate) read_only: Option, // #[cfg(target_os = "linux")] pub(crate) hide_k_threads: Option, // #[cfg(feature = "zfs")]