Skip to content

Commit

Permalink
refactor: optimize string splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Jan 30, 2025
1 parent b68e417 commit aad15e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 30 deletions.
7 changes: 2 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,8 @@ impl App {
) {
let dependent_modules_list = kernel_modules.default_list
[kernel_modules.index][2]
.split(' ')
.last()
.unwrap_or("-")
.split(',')
.collect::<Vec<&str>>();
.rsplit_once(' ')
.map_or(vec!["-"], |(_, modules)| modules.split(',').collect());
if !(dependent_modules_list[0] == "-"
|| kernel_modules.current_name.contains("Dependent modules"))
|| cfg!(test)
Expand Down
37 changes: 12 additions & 25 deletions src/kernel/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,15 @@ pub struct Command {

impl Command {
/// Create a new command instance.
fn new(
cmd: String,
desc: &'static str,
mut title: String,
symbol: Symbol,
) -> Self {
fn new(cmd: String, desc: &'static str, title: &str, symbol: Symbol) -> Self {
// Parse the command title if '!' is given.
if title.contains('!') {
title = (*title
.split('!')
.collect::<Vec<&str>>()
.last()
.unwrap_or(&""))
.to_string();
}
Self {
cmd,
desc,
title,
title: title
.rsplit_once('!')
.map_or(title, |(_, title)| title)
.to_string(),
symbol,
}
}
Expand Down Expand Up @@ -64,7 +54,7 @@ impl ModuleCommand {
/// Get Command struct from a enum element.
pub fn get(self, module_name: &str) -> Command {
match self {
Self::None => Command::new(String::from(""), "", format!("Module: {module_name}"), Symbol::None),
Self::None => Command::new(String::from(""), "", &format!("Module: {module_name}"), Symbol::None),
Self::Load => Command::new(
if Self::is_module_filename(module_name) {
format!("insmod {}", &module_name)
Expand All @@ -73,7 +63,7 @@ impl ModuleCommand {
},
"Add and remove modules from the Linux Kernel\n
This command inserts a module to the kernel.",
format!("Load: {module_name}"), Symbol::Anchor),
&format!("Load: {module_name}"), Symbol::Anchor),
Self::Unload => Command::new(
format!("modprobe -r {0} || rmmod {0}", &module_name),
"modprobe/rmmod: Add and remove modules from the Linux Kernel
Expand All @@ -85,14 +75,14 @@ impl ModuleCommand {
There is usually no reason to remove modules, but some buggy \
modules require it. Your distribution kernel may not have been \
built to support removal of modules at all.",
format!("Remove: {module_name}"), Symbol::CircleX),
&format!("Remove: {module_name}"), Symbol::CircleX),
Self::Reload => Command::new(
format!("{} && {}",
ModuleCommand::Unload.get(module_name).cmd,
ModuleCommand::Load.get(module_name).cmd),
"modprobe/insmod/rmmod: Add and remove modules from the Linux Kernel\n
This command reloads a module, removes and inserts to the kernel.",
format!("Reload: {module_name}"), Symbol::FuelPump),
&format!("Reload: {module_name}"), Symbol::FuelPump),
Self::Blacklist => Command::new(
format!("if ! grep -q {module} /etc/modprobe.d/blacklist.conf; then
echo 'blacklist {module}' >> /etc/modprobe.d/blacklist.conf
Expand All @@ -108,13 +98,13 @@ impl ModuleCommand {
this behaviour; the install command instructs modprobe to run a custom command \
instead of inserting the module in the kernel as normal, so the module will \
always fail to load.",
format!("Blacklist: {module_name}"), Symbol::SquareX),
&format!("Blacklist: {module_name}"), Symbol::SquareX),
Self::Clear => Command::new(
String::from("dmesg --clear"),
"dmesg: Print or control the kernel ring buffer
option: -C, --clear\n
Clear the ring buffer.",
String::from("Clear"), Symbol::Cloud),
"Clear", Symbol::Cloud),
}
}

Expand All @@ -125,10 +115,7 @@ impl ModuleCommand {

/// Check if module name is a filename with suffix 'ko'
pub fn is_module_filename(module_name: &str) -> bool {
match module_name.split('.').collect::<Vec<&str>>().last() {
Some(v) => *v == "ko",
None => false,
}
module_name.ends_with(".ko")
}
}

Expand Down

0 comments on commit aad15e6

Please sign in to comment.