This commit is contained in:
ClementTsang 2024-01-10 23:35:49 -05:00
parent fa9580e47e
commit 017a1702b3
No known key found for this signature in database
GPG key ID: DC3B7867D8D97095

View file

@ -109,107 +109,79 @@ macro_rules! args {
};
}
/// Workaround trait to add builder methods.
trait CommandBuilder {
fn general_args(self) -> Self;
fn general_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("General Options");
fn style_args(self) -> Self;
fn temperature_args(self) -> Self;
fn process_args(self) -> Self;
fn cpu_args(self) -> Self;
fn mem_args(self) -> Self;
fn network_args(self) -> Self;
fn battery_args(self) -> Self;
fn gpu_args(self) -> Self;
fn other(self) -> Self;
fn add_args(self) -> Self;
}
impl CommandBuilder for Command {
fn general_args(self) -> Command {
let cmd = self.next_help_heading("General Options");
let autohide_time = Arg::new("autohide_time")
.long("autohide_time")
.action(ArgAction::SetTrue)
.help("Temporarily shows the time scale in graphs.")
.long_help(
"Automatically hides the time scale in graphs after being shown for \
let autohide_time = Arg::new("autohide_time")
.long("autohide_time")
.action(ArgAction::SetTrue)
.help("Temporarily shows the time scale in graphs.")
.long_help(
"Automatically hides the time scale in graphs after being shown for \
a brief moment when zoomed in/out. If time is disabled via --hide_time \
then this will have no effect.",
);
);
let basic = Arg::new("basic")
.short('b')
.long("basic")
.action(ArgAction::SetTrue)
.help("Hides graphs and uses a more basic look.")
.long_help(
"Hides graphs and uses a more basic look. Design is largely inspired by htop's.",
);
let basic = Arg::new("basic")
.short('b')
.long("basic")
.action(ArgAction::SetTrue)
.help("Hides graphs and uses a more basic look.")
.long_help(
"Hides graphs and uses a more basic look. Design is largely inspired by htop's.",
);
let disable_click = Arg::new("disable_click")
.long("disable_click")
.action(ArgAction::SetTrue)
.help("Disables mouse clicks.")
.long_help("Disables mouse clicks from interacting with the program.");
let disable_click = Arg::new("disable_click")
.long("disable_click")
.action(ArgAction::SetTrue)
.help("Disables mouse clicks.")
.long_help("Disables mouse clicks from interacting with the program.");
let dot_marker = Arg::new("dot_marker")
.short('m')
.long("dot_marker")
.action(ArgAction::SetTrue)
.help("Uses a dot marker for graphs.")
.long_help("Uses a dot marker for graphs as opposed to the default braille marker.");
let dot_marker = Arg::new("dot_marker")
.short('m')
.long("dot_marker")
.action(ArgAction::SetTrue)
.help("Uses a dot marker for graphs.")
.long_help("Uses a dot marker for graphs as opposed to the default braille marker.");
let hide_table_gap = Arg::new("hide_table_gap")
.long("hide_table_gap")
.action(ArgAction::SetTrue)
.help("Hides spacing between table headers and entries.")
.long_help("Hides the spacing between table headers and entries.");
let hide_table_gap = Arg::new("hide_table_gap")
.long("hide_table_gap")
.action(ArgAction::SetTrue)
.help("Hides spacing between table headers and entries.")
.long_help("Hides the spacing between table headers and entries.");
let hide_time = Arg::new("hide_time")
.long("hide_time")
.action(ArgAction::SetTrue)
.help("Hides the time scale.")
.long_help("Completely hides the time scale from being shown.");
let hide_time = Arg::new("hide_time")
.long("hide_time")
.action(ArgAction::SetTrue)
.help("Hides the time scale.")
.long_help("Completely hides the time scale from being shown.");
let left_legend = Arg::new("left_legend")
.short('l')
.long("left_legend")
.action(ArgAction::SetTrue)
.help("Puts the CPU chart legend to the left side.")
.long_help("Puts the CPU chart legend to the left side rather than the right side.");
let left_legend = Arg::new("left_legend")
.short('l')
.long("left_legend")
.action(ArgAction::SetTrue)
.help("Puts the CPU chart legend to the left side.")
.long_help("Puts the CPU chart legend to the left side rather than the right side.");
let show_table_scroll_position = Arg::new("show_table_scroll_position")
.long("show_table_scroll_position")
.action(ArgAction::SetTrue)
.help("Shows the scroll position tracker in table widgets.")
.long_help(
"Shows the list scroll position tracker in the widget title for table widgets.",
);
let show_table_scroll_position = Arg::new("show_table_scroll_position")
.long("show_table_scroll_position")
.action(ArgAction::SetTrue)
.help("Shows the scroll position tracker in table widgets.")
.long_help("Shows the list scroll position tracker in the widget title for table widgets.");
let config_location = Arg::new("config_location")
.short('C')
.long("config")
.action(ArgAction::Set)
.value_name("CONFIG PATH")
.help("Sets the location of the config file.")
.long_help(
"Sets the location of the config file. Expects a config file in the TOML format. \
let config_location = Arg::new("config_location")
.short('C')
.long("config")
.action(ArgAction::Set)
.value_name("CONFIG PATH")
.help("Sets the location of the config file.")
.long_help(
"Sets the location of the config file. Expects a config file in the TOML format. \
If it doesn't exist, one is created.",
)
.value_hint(ValueHint::AnyPath);
)
.value_hint(ValueHint::AnyPath);
let default_time_value = Arg::new("default_time_value")
let default_time_value = Arg::new("default_time_value")
.short('t')
.long("default_time_value")
.action(ArgAction::Set)
@ -219,15 +191,15 @@ impl CommandBuilder for Command {
"Default time value for graphs. Takes a number in milliseconds or a human duration (e.g. 60s). The minimum time is 30s, and the default is 60s.",
);
// TODO: Charts are broken in the manpage
let default_widget_count = Arg::new("default_widget_count")
.long("default_widget_count")
.action(ArgAction::Set)
.requires_all(["default_widget_type"])
.value_name("INT")
.help("Sets the n'th selected widget type as the default.")
.long_help(
"\
// TODO: Charts are broken in the manpage
let default_widget_count = Arg::new("default_widget_count")
.long("default_widget_count")
.action(ArgAction::Set)
.requires_all(["default_widget_type"])
.value_name("INT")
.help("Sets the n'th selected widget type as the default.")
.long_help(
"\
Sets the n'th selected widget type to use as the default widget.
Requires 'default_widget_type' to also be set, and defaults to 1.
@ -244,23 +216,23 @@ And we set our default widget type to 'CPU'. If we set
the default widget. If we set '--default_widget_count 3', it would
use CPU (3) as the default instead.
",
);
);
let default_widget_type = Arg::new("default_widget_type")
.long("default_widget_type")
.action(ArgAction::Set)
.value_name("WIDGET TYPE")
.help("Sets the default widget type, use --help for info.")
.long_help(DEFAULT_WIDGET_TYPE_STR);
let default_widget_type = Arg::new("default_widget_type")
.long("default_widget_type")
.action(ArgAction::Set)
.value_name("WIDGET TYPE")
.help("Sets the default widget type, use --help for info.")
.long_help(DEFAULT_WIDGET_TYPE_STR);
let expanded_on_startup = Arg::new("expanded_on_startup")
let expanded_on_startup = Arg::new("expanded_on_startup")
.short('e')
.long("expanded")
.action(ArgAction::SetTrue)
.help("Expand the default widget upon starting the app.")
.long_help("Expand the default widget upon starting the app. Same as pressing \"e\" inside the app. Use with \"default_widget_type\" and \"default_widget_count\" to select desired expanded widget. This flag has no effect in basic mode (--basic)");
let rate = Arg::new("rate")
let rate = Arg::new("rate")
.short('r')
.long("rate")
.action(ArgAction::Set)
@ -268,7 +240,7 @@ use CPU (3) as the default instead.
.help("Sets the data refresh rate.")
.long_help("Sets the data refresh rate. Takes a number in milliseconds or a human duration (e.g. 5s). The minimum is 250ms, and defaults to 1000ms. Smaller values may take more computer resources.");
let time_delta = Arg::new("time_delta")
let time_delta = Arg::new("time_delta")
.short('d')
.long("time_delta")
.action(ArgAction::Set)
@ -276,55 +248,53 @@ use CPU (3) as the default instead.
.help("The amount of time changed upon zooming.")
.long_help("The amount of time changed when zooming in/out. Takes a number in milliseconds or a human duration (e.g. 30s). The minimum is 1s, and defaults to 15s.");
let retention = Arg::new("retention")
let retention = Arg::new("retention")
.long("retention")
.action(ArgAction::Set)
.value_name("TIME")
.help("The timespan of data stored.")
.long_help("How much data is stored at once in terms of time. Takes a number in milliseconds or a human duration (e.g. 20m), with a minimum of 1 minute. Note higher values will take up more memory. Defaults to 10 minutes.");
let args = args![
autohide_time,
basic,
disable_click,
dot_marker,
hide_table_gap,
hide_time,
left_legend,
show_table_scroll_position,
config_location,
default_time_value,
default_widget_count,
default_widget_type,
expanded_on_startup,
rate,
time_delta,
retention,
];
cmd.args(args![
autohide_time,
basic,
disable_click,
dot_marker,
hide_table_gap,
hide_time,
left_legend,
show_table_scroll_position,
config_location,
default_time_value,
default_widget_count,
default_widget_type,
expanded_on_startup,
rate,
time_delta,
retention,
])
}
cmd.args(args)
}
fn style_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("Style Options");
fn style_args(self) -> Command {
let cmd = self.next_help_heading("Style Options");
// TODO: File an issue with manpage, it cannot render charts correctly.
let color = Arg::new("color")
.long("color")
.action(ArgAction::Set)
.value_name("COLOR SCHEME")
.value_parser(PossibleValuesParser::new([
"default",
"default-light",
"gruvbox",
"gruvbox-light",
"nord",
"nord-light",
]))
.hide_possible_values(true)
.help("Use a color scheme, use --help for info.")
.long_help(
"\
// TODO: File an issue with manpage, it cannot render charts correctly.
let color = Arg::new("color")
.long("color")
.action(ArgAction::Set)
.value_name("COLOR SCHEME")
.value_parser(PossibleValuesParser::new([
"default",
"default-light",
"gruvbox",
"gruvbox-light",
"nord",
"nord-light",
]))
.hide_possible_values(true)
.help("Use a color scheme, use --help for info.")
.long_help(
"\
Use a pre-defined color scheme. Currently supported values are:
+------------------------------------------------------------+
| default |
@ -341,62 +311,61 @@ Use a pre-defined color scheme. Currently supported values are:
+------------------------------------------------------------+
Defaults to \"default\".
",
);
);
cmd.arg(color)
}
cmd.arg(color)
}
fn temperature_args(self) -> Command {
let cmd = self.next_help_heading("Temperature Options");
fn temperature_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("Temperature Options");
let celsius = Arg::new("celsius")
.short('c')
.long("celsius")
.action(ArgAction::SetTrue)
.help("Use Celsius as the temperature unit.")
.long_help("Use Celsius as the temperature unit. This is the default option.");
let celsius = Arg::new("celsius")
.short('c')
.long("celsius")
.action(ArgAction::SetTrue)
.help("Use Celsius as the temperature unit.")
.long_help("Use Celsius as the temperature unit. This is the default option.");
let fahrenheit = Arg::new("fahrenheit")
.short('f')
.long("fahrenheit")
.action(ArgAction::SetTrue)
.help("Use Fahrenheit as the temperature unit.");
let fahrenheit = Arg::new("fahrenheit")
.short('f')
.long("fahrenheit")
.action(ArgAction::SetTrue)
.help("Use Fahrenheit as the temperature unit.");
let kelvin = Arg::new("kelvin")
.short('k')
.long("kelvin")
.action(ArgAction::SetTrue)
.help("Use Kelvin as the temperature unit.");
let kelvin = Arg::new("kelvin")
.short('k')
.long("kelvin")
.action(ArgAction::SetTrue)
.help("Use Kelvin as the temperature unit.");
let temperature_group = ArgGroup::new("TEMPERATURE_TYPE").args([
celsius.get_id(),
fahrenheit.get_id(),
kelvin.get_id(),
]);
let temperature_group = ArgGroup::new("TEMPERATURE_TYPE").args([
celsius.get_id(),
fahrenheit.get_id(),
kelvin.get_id(),
]);
let args = args![celsius, fahrenheit, kelvin];
cmd.args(args![celsius, fahrenheit, kelvin])
.group(temperature_group)
}
cmd.args(args).group(temperature_group)
}
fn process_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("Process Options");
fn process_args(self) -> Command {
let cmd = self.next_help_heading("Process Options");
let case_sensitive = Arg::new("case_sensitive")
.short('S')
.long("case_sensitive")
.action(ArgAction::SetTrue)
.help("Enables case sensitivity by default.")
.long_help("When searching for a process, enables case sensitivity by default.");
let case_sensitive = Arg::new("case_sensitive")
.short('S')
.long("case_sensitive")
.action(ArgAction::SetTrue)
.help("Enables case sensitivity by default.")
.long_help("When searching for a process, enables case sensitivity by default.");
let current_usage = Arg::new("current_usage")
let current_usage = Arg::new("current_usage")
.short('u')
.long("current_usage")
.action(ArgAction::SetTrue)
.help("Sets process CPU% to be based on current CPU%.")
.long_help("Sets process CPU% usage to be based on the current system CPU% usage rather than total CPU usage.");
let unnormalized_cpu = Arg::new("unnormalized_cpu")
let unnormalized_cpu = Arg::new("unnormalized_cpu")
.short('n')
.long("unnormalized_cpu")
.action(ArgAction::SetTrue)
@ -405,108 +374,106 @@ Defaults to \"default\".
"Shows all process CPU% usage without averaging over the number of CPU cores in the system.",
);
let group_processes = Arg::new("group_processes")
.short('g')
.long("group_processes")
.action(ArgAction::SetTrue)
.help("Groups processes with the same name by default.")
.long_help("Groups processes with the same name by default.");
let group_processes = Arg::new("group_processes")
.short('g')
.long("group_processes")
.action(ArgAction::SetTrue)
.help("Groups processes with the same name by default.")
.long_help("Groups processes with the same name by default.");
let process_command = Arg::new("process_command")
.long("process_command")
.action(ArgAction::SetTrue)
.help("Show processes as their commands by default.")
.long_help("Show processes as their commands by default in the process widget.");
let process_command = Arg::new("process_command")
.long("process_command")
.action(ArgAction::SetTrue)
.help("Show processes as their commands by default.")
.long_help("Show processes as their commands by default in the process widget.");
let regex = Arg::new("regex")
.short('R')
.long("regex")
.action(ArgAction::SetTrue)
.help("Enables regex by default.")
.long_help("When searching for a process, enables regex by default.");
let regex = Arg::new("regex")
.short('R')
.long("regex")
.action(ArgAction::SetTrue)
.help("Enables regex by default.")
.long_help("When searching for a process, enables regex by default.");
let disable_advanced_kill = Arg::new("disable_advanced_kill")
let disable_advanced_kill = Arg::new("disable_advanced_kill")
.long("disable_advanced_kill")
.action(ArgAction::SetTrue)
.help("Hides advanced process killing.")
.long_help("Hides advanced options to stop a process on Unix-like systems. The only option shown is 15 (TERM).");
let whole_word = Arg::new("whole_word")
.short('W')
.long("whole_word")
.action(ArgAction::SetTrue)
.help("Enables whole-word matching by default.")
.long_help(
"When searching for a process, return results that match the entire query by default.",
);
let whole_word = Arg::new("whole_word")
.short('W')
.long("whole_word")
.action(ArgAction::SetTrue)
.help("Enables whole-word matching by default.")
.long_help(
"When searching for a process, return results that match the entire query by default.",
);
let tree = Arg::new("tree")
.short('T')
.long("tree")
.action(ArgAction::SetTrue)
.help("Defaults the process widget be in tree mode.")
.long_help("Defaults to showing the process widget in tree mode.");
let tree = Arg::new("tree")
.short('T')
.long("tree")
.action(ArgAction::SetTrue)
.help("Defaults the process widget be in tree mode.")
.long_help("Defaults to showing the process widget in tree mode.");
let args = args![
case_sensitive,
current_usage,
unnormalized_cpu,
group_processes,
process_command,
regex,
whole_word,
disable_advanced_kill,
tree,
];
let args = args![
case_sensitive,
current_usage,
unnormalized_cpu,
group_processes,
process_command,
regex,
whole_word,
disable_advanced_kill,
tree,
];
cmd.args(args)
}
cmd.args(args)
}
fn cpu_args(self) -> Command {
let cmd = self.next_help_heading("CPU Options");
fn cpu_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("CPU Options");
let hide_avg_cpu = Arg::new("hide_avg_cpu")
.short('a')
.long("hide_avg_cpu")
.action(ArgAction::SetTrue)
.help("Hides the average CPU usage.")
.long_help("Hides the average CPU usage from being shown.");
let hide_avg_cpu = Arg::new("hide_avg_cpu")
.short('a')
.long("hide_avg_cpu")
.action(ArgAction::SetTrue)
.help("Hides the average CPU usage.")
.long_help("Hides the average CPU usage from being shown.");
// let default_avg_cpu = Arg::new("");
// let default_avg_cpu = Arg::new("");
let args = args![hide_avg_cpu];
cmd.args(args![hide_avg_cpu])
}
cmd.args(args)
}
fn mem_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("Memory Options");
fn mem_args(self) -> Command {
let cmd = self.next_help_heading("Memory Options");
let mem_as_value = Arg::new("mem_as_value")
let mem_as_value = Arg::new("mem_as_value")
.long("mem_as_value")
.action(ArgAction::SetTrue)
.help("Defaults to showing process memory usage by value.")
.long_help("Defaults to showing process memory usage by value. Otherwise, it defaults to showing it by percentage.");
#[cfg(not(target_os = "windows"))]
{
let enable_cache_memory = Arg::new("enable_cache_memory")
.long("enable_cache_memory")
.action(ArgAction::SetTrue)
.help("Enable collecting and displaying cache and buffer memory.");
#[cfg(not(target_os = "windows"))]
{
let enable_cache_memory = Arg::new("enable_cache_memory")
.long("enable_cache_memory")
.action(ArgAction::SetTrue)
.help("Enable collecting and displaying cache and buffer memory.");
cmd.args([mem_as_value, enable_cache_memory])
}
#[cfg(target_os = "windows")]
{
cmd.arg(mem_as_value)
}
cmd.args(args![mem_as_value, enable_cache_memory])
}
#[cfg(target_os = "windows")]
{
cmd.arg(mem_as_value)
}
}
fn network_args(self) -> Command {
let cmd = self.next_help_heading("Network Options");
fn network_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("Network Options");
let use_old_network_legend = Arg::new("use_old_network_legend")
let use_old_network_legend = Arg::new("use_old_network_legend")
.long("use_old_network_legend")
.action(ArgAction::SetTrue)
.help("DEPRECATED - uses a separate network legend.")
@ -515,21 +482,19 @@ Defaults to \"default\".
tested anymore and could be broken.",
);
let network_use_bytes = Arg::new("network_use_bytes")
.long("network_use_bytes")
.action(ArgAction::SetTrue)
.help("Displays the network widget using bytes.")
.long_help("Displays the network widget using bytes. Defaults to bits.");
let network_use_bytes = Arg::new("network_use_bytes")
.long("network_use_bytes")
.action(ArgAction::SetTrue)
.help("Displays the network widget using bytes.")
.long_help("Displays the network widget using bytes. Defaults to bits.");
let network_use_log = Arg::new("network_use_log")
.long("network_use_log")
.action(ArgAction::SetTrue)
.help("Displays the network widget with a log scale.")
.long_help(
"Displays the network widget with a log scale. Defaults to a non-log scale.",
);
let network_use_log = Arg::new("network_use_log")
.long("network_use_log")
.action(ArgAction::SetTrue)
.help("Displays the network widget with a log scale.")
.long_help("Displays the network widget with a log scale. Defaults to a non-log scale.");
let network_use_binary_prefix = Arg::new("network_use_binary_prefix")
let network_use_binary_prefix = Arg::new("network_use_binary_prefix")
.long("network_use_binary_prefix")
.action(ArgAction::SetTrue)
.help("Displays the network widget with binary prefixes.")
@ -537,22 +502,20 @@ Defaults to \"default\".
"Displays the network widget with binary prefixes (i.e. kibibits, mebibits) rather than a decimal prefix (i.e. kilobits, megabits). Defaults to decimal prefixes.",
);
let args = args![
use_old_network_legend,
network_use_bytes,
network_use_log,
network_use_binary_prefix,
];
cmd.args(args![
use_old_network_legend,
network_use_bytes,
network_use_log,
network_use_binary_prefix,
])
}
cmd.args(args)
}
fn battery_args(cmd: Command) -> Command {
#[cfg(feature = "battery")]
{
let cmd = cmd.next_help_heading("Battery Options");
fn battery_args(self) -> Command {
#[cfg(feature = "battery")]
{
let cmd = self.next_help_heading("Battery Options");
let battery = Arg::new("battery")
let battery = Arg::new("battery")
.long("battery")
.action(ArgAction::SetTrue)
.help("Shows the battery widget.")
@ -560,64 +523,50 @@ Defaults to \"default\".
"Shows the battery widget in default or basic mode. No effect on custom layouts.",
);
cmd.arg(battery)
}
#[cfg(not(feature = "battery"))]
{
self
}
cmd.arg(battery)
}
fn gpu_args(self) -> Command {
#[cfg(feature = "gpu")]
{
let cmd = self.next_help_heading("GPU Options");
let enable_gpu = Arg::new("enable_gpu")
.long("enable_gpu")
.action(ArgAction::SetTrue)
.help("Enable collecting and displaying GPU usage.");
cmd.arg(enable_gpu)
}
#[cfg(not(feature = "gpu"))]
{
self
}
#[cfg(not(feature = "battery"))]
{
cmd
}
}
fn other(self) -> Command {
let cmd = self.next_help_heading("Other Options");
fn gpu_args(cmd: Command) -> Command {
#[cfg(feature = "gpu")]
{
let cmd = cmd.next_help_heading("GPU Options");
let help = Arg::new("help")
.short('h')
.long("help")
.action(ArgAction::Help)
.help("Prints help (see more with '--help').");
let enable_gpu = Arg::new("enable_gpu")
.long("enable_gpu")
.action(ArgAction::SetTrue)
.help("Enable collecting and displaying GPU usage.");
let version = Arg::new("version")
.short('V')
.long("version")
.action(ArgAction::Version)
.help("Prints version information.");
cmd.args([help, version])
cmd.arg(enable_gpu)
}
fn add_args(self) -> Self {
self.general_args()
.style_args()
.temperature_args()
.process_args()
.cpu_args()
.mem_args()
.network_args()
.battery_args()
.gpu_args()
.other()
#[cfg(not(feature = "gpu"))]
{
cmd
}
}
fn other_args(cmd: Command) -> Command {
let cmd = cmd.next_help_heading("Other Options");
let help = Arg::new("help")
.short('h')
.long("help")
.action(ArgAction::Help)
.help("Prints help (see more with '--help').");
let version = Arg::new("version")
.short('V')
.long("version")
.action(ArgAction::Version)
.help("Prints version information.");
cmd.args([help, version])
}
pub fn build_app() -> Command {
const TEMPLATE: &str = include_str!("./args.template");
const USAGE: &str = "btm [OPTIONS]";
@ -626,7 +575,7 @@ pub fn build_app() -> Command {
None => crate_version!(),
};
Command::new(crate_name!())
let cmd = Command::new(crate_name!())
.author(crate_authors!())
.about(crate_description!())
.disable_help_flag(true)
@ -634,8 +583,21 @@ pub fn build_app() -> Command {
.color(ColorChoice::Auto)
.help_template(TEMPLATE)
.override_usage(USAGE)
.version(VERSION)
.add_args()
.version(VERSION);
let cmd = general_args(cmd);
let cmd = style_args(cmd);
let cmd = temperature_args(cmd);
let cmd = process_args(cmd);
let cmd = cpu_args(cmd);
let cmd = mem_args(cmd);
let cmd = network_args(cmd);
let cmd = battery_args(cmd);
let cmd = gpu_args(cmd);
let cmd = other_args(cmd);
#[allow(clippy::let_and_return)]
cmd
}
#[cfg(test)]