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 all commits
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
9 changes: 4 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,12 @@ impl App {
&mut self,
kernel_modules: &mut KernelModules<'_>,
) {
// If there is no space in "used_modules", return a vector with "-"
// Otherwise, split the modules by commas and collect them into a vector
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
42 changes: 15 additions & 27 deletions src/kernel/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::style::Symbol;
use std::{ffi::OsStr, path::Path};

/// Kernel module related command
#[derive(Debug)]
Expand All @@ -11,25 +12,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,16 +55,16 @@ 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) {
if Self::is_module_filename(Path::new(module_name)) {
format!("insmod {}", &module_name)
} else {
format!("modprobe {0} || insmod {0}.ko", &module_name)
},
"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 +76,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 +99,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 @@ -124,11 +115,8 @@ 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,
}
pub fn is_module_filename(module_name: &Path) -> bool {
module_name.extension() == Some(OsStr::new("ko"))
}
}

Expand Down
Loading