Skip to content

Commit

Permalink
[eclipse-iceoryx#98] Change console output to more closely resemble c…
Browse files Browse the repository at this point in the history
…argo
  • Loading branch information
orecham committed May 22, 2024
1 parent 725966d commit 526fd91
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion iceoryx2-cli/iox2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 12 additions & 6 deletions iceoryx2-cli/iox2/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clap::Parser;
use colored::*;

#[derive(Parser, Debug)]
#[command(
Expand All @@ -21,10 +22,15 @@ pub struct Cli {
pub external_command: Vec<String>,
}

fn help_template() -> &'static str {
"\
USAGE: iox2 [OPTIONS] <COMMAND>\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"
)
}
61 changes: 35 additions & 26 deletions iceoryx2-cli/iox2/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandInfo> {
Expand All @@ -45,10 +53,9 @@ fn find() -> Vec<CommandInfo> {
}

fn find_command_binaries_in_development_dirs() -> Vec<CommandInfo> {
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"
Expand All @@ -64,23 +71,25 @@ fn find_command_binaries_in_development_dirs() -> Vec<CommandInfo> {
.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<CommandInfo> {
Expand Down

0 comments on commit 526fd91

Please sign in to comment.