diff --git a/iceoryx2-cli/iox2/Cargo.toml b/iceoryx2-cli/iox2/Cargo.toml index a945b2bf1..bdf2de5d1 100644 --- a/iceoryx2-cli/iox2/Cargo.toml +++ b/iceoryx2-cli/iox2/Cargo.toml @@ -14,5 +14,5 @@ version = { workspace = true } human-panic = "1.2.3" better-panic = "0.3.0" clap = { version = "4.4.18", features = ["derive"] } -colored = "2.0" +colored = "2.1" thiserror = "1.0.56" diff --git a/iceoryx2-cli/iox2/src/cli.rs b/iceoryx2-cli/iox2/src/cli.rs index d891eb0e4..3250e671a 100644 --- a/iceoryx2-cli/iox2/src/cli.rs +++ b/iceoryx2-cli/iox2/src/cli.rs @@ -1,4 +1,5 @@ use clap::Parser; +use colored::*; #[derive(Parser, Debug)] #[command( @@ -21,10 +22,15 @@ pub struct Cli { pub external_command: Vec, } -fn help_template() -> &'static str { - "\ - USAGE: iox2 [OPTIONS] \n\n\ - OPTIONS:\n{options}\n\n\ - COMMANDS:\n{subcommands}\n\ - \u{00A0}\u{00A0}... See all installed commands with --list" +fn help_template() -> String { + format!( + "{}{}{}\n\n{}\n{{options}}\n\n{}\n{{subcommands}}{}{}", + "Usage: ".bright_green().bold(), + "iox2 ".bold(), + "[OPTIONS] [COMMAND]", + "Options:".bright_green().bold(), + "Commands:".bright_green().bold(), + " ... ".bold(), + "See all installed commands with --list" + ) } diff --git a/iceoryx2-cli/iox2/src/commands.rs b/iceoryx2-cli/iox2/src/commands.rs index 3336aad48..e022626b7 100644 --- a/iceoryx2-cli/iox2/src/commands.rs +++ b/iceoryx2-cli/iox2/src/commands.rs @@ -21,18 +21,26 @@ pub enum ExecutionError { } pub fn list() { - println!("Installed Commands:"); - let installed_commands = find(); - for command in installed_commands { - println!( - " {}", + println!("{}", "Installed Commands:".bright_green().bold()); + let mut installed_commands = find(); + installed_commands.sort_by_key(|command| command.name.clone()); + installed_commands + .iter() + .map(|command| { format!( - "{}{}", - if command.is_development { "(dev) " } else { "" }, - command.name.bold(), + " {}", + format!( + "{} {}", + command.name.bold(), + if command.is_development { + "(dev) ".italic() + } else { + "".italic() + }, + ) ) - ); - } + }) + .for_each(|formatted_command| println!("{}", formatted_command)); } fn find() -> Vec { @@ -45,10 +53,9 @@ fn find() -> Vec { } fn find_command_binaries_in_development_dirs() -> Vec { - let mut commands = Vec::new(); let current_exe = match env::current_exe() { Ok(exe) => exe, - Err(_) => return commands, + Err(_) => return Vec::new(), }; let build_type = if cfg!(debug_assertions) { "debug" @@ -64,23 +71,25 @@ fn find_command_binaries_in_development_dirs() -> Vec { .unwrap() .join(build_type); - if let Ok(entries) = fs::read_dir(&binary_dir) { - for entry in entries.filter_map(Result::ok) { - let path = entry.path(); - if is_valid_command_binary(&path) { - if let Some(command_name) = path.file_name().and_then(|n| n.to_str()) { + fs::read_dir(binary_dir) + .ok() + .into_iter() + .flat_map(|entries| entries.filter_map(Result::ok)) + .map(|entry| entry.path()) + .filter(|path| is_valid_command_binary(path)) + .filter_map(|path| { + path.file_name() + .and_then(|n| n.to_str()) + .map(|command_name| { let stripped = command_name.strip_prefix("iox2-").unwrap_or(command_name); - commands.push(CommandInfo { + CommandInfo { name: stripped.to_string(), - path, + path: path.clone(), is_development: true, - }); - } - } - } - } - - commands + } + }) + }) + .collect() } fn find_command_binaries_in_system_path() -> Vec {