Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: optimize string splitting #182

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(' ')
orhun marked this conversation as resolved.
Show resolved Hide resolved
.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")
Integral-Tech marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading