From b96b5f7c33d7426891c2aed390d15f6d96b40db7 Mon Sep 17 00:00:00 2001 From: Bruce Rosier Date: Thu, 17 Oct 2024 23:36:49 +0200 Subject: [PATCH] [#432] Fix print_system_configuration for cross-platform coloring + implementation of user confirmation required before deleting current configuration file --- Cargo.toml | 33 ++++++-- iceoryx2-cli/Cargo.toml | 7 +- iceoryx2-cli/iox2-config/src/commands.rs | 99 ++++++++++++++---------- 3 files changed, 89 insertions(+), 50 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b38cc6ca6..a3b1d8f6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ members = [ "examples", "benchmarks/publish-subscribe", - "benchmarks/event" + "benchmarks/event", ] [workspace.package] @@ -35,7 +35,13 @@ categories = ["network-programming"] description = "iceoryx2: Lock-Free Zero-Copy Interprocess Communication" edition = "2021" homepage = "https://iceoryx.io" -keywords = ["zero-copy", "communication", "ipc", "publish-subscribe", "request-response"] +keywords = [ + "zero-copy", + "communication", + "ipc", + "publish-subscribe", + "request-response", +] license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/eclipse-iceoryx/iceoryx2" @@ -66,8 +72,7 @@ iceoryx2-ffi-macros = { version = "0.4.1", path = "iceoryx2-ffi/ffi-macros" } iceoryx2 = { version = "0.4.1", path = "iceoryx2/" } -iceoryx2-cli = { version = "0.4.1", path = "iceoryx2_cli/"} - +iceoryx2-cli = { version = "0.4.1", path = "iceoryx2_cli/" } anyhow = { version = "1.0.86" } @@ -103,8 +108,24 @@ tiny-fn = { version = "0.1.6" } toml = { version = "0.8.13" } tracing = { version = "0.1.40" } dirs = { version = "5.0" } -windows-sys = { version = "0.48.0", features = ["Win32_Security", "Win32_Security_Authorization", "Win32_System_Memory", "Win32_System_Threading", "Win32_Foundation", "Win32_System_WindowsProgramming", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Diagnostics_Debug", "Win32_System_SystemInformation", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Console", "Win32_Networking_WinSock", -"Win32_System_SystemServices", "Win32_System_ProcessStatus"] } +dialoguer = { version = "0.8.0" } +windows-sys = { version = "0.48.0", features = [ + "Win32_Security", + "Win32_Security_Authorization", + "Win32_System_Memory", + "Win32_System_Threading", + "Win32_Foundation", + "Win32_System_WindowsProgramming", + "Win32_Storage_FileSystem", + "Win32_System_IO", + "Win32_System_Diagnostics_Debug", + "Win32_System_SystemInformation", + "Win32_System_Diagnostics_ToolHelp", + "Win32_System_Console", + "Win32_Networking_WinSock", + "Win32_System_SystemServices", + "Win32_System_ProcessStatus", +] } [profile.release] strip = true diff --git a/iceoryx2-cli/Cargo.toml b/iceoryx2-cli/Cargo.toml index b2379e95f..5c4a10e7f 100644 --- a/iceoryx2-cli/Cargo.toml +++ b/iceoryx2-cli/Cargo.toml @@ -37,10 +37,10 @@ path = "lib/src/lib.rs" [dependencies] iceoryx2 = { workspace = true } iceoryx2-bb-log = { workspace = true } -iceoryx2-pal-posix = {workspace = true} -iceoryx2-bb-posix = {workspace = true} +iceoryx2-pal-posix = { workspace = true } +iceoryx2-bb-posix = { workspace = true } iceoryx2-bb-system-types = { workspace = true } -iceoryx2-bb-container ={ workspace = true } +iceoryx2-bb-container = { workspace = true } anyhow = { workspace = true } better-panic = { workspace = true } @@ -55,6 +55,7 @@ serde_json = { workspace = true } ron = { workspace = true } toml = { workspace = true } dirs = { workspace = true } +dialoguer = { workspace = true } [dev-dependencies] iceoryx2-bb-testing = { workspace = true } diff --git a/iceoryx2-cli/iox2-config/src/commands.rs b/iceoryx2-cli/iox2-config/src/commands.rs index f5bed05e7..adcd2a7f7 100644 --- a/iceoryx2-cli/iox2-config/src/commands.rs +++ b/iceoryx2-cli/iox2-config/src/commands.rs @@ -11,34 +11,33 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT use anyhow::Result; +use colored::Colorize; +use dialoguer::Confirm; use enum_iterator::all; use iceoryx2::config::Config; use iceoryx2_bb_posix::system_configuration::*; use std::fs::{self, File}; use std::io::Write; +use std::panic::catch_unwind; /// Prints the whole system configuration with all limits, features and details to the console. pub fn print_system_configuration() { - const HEADER_COLOR: &str = "\x1b[4;92m"; - const VALUE_COLOR: &str = "\x1b[0;94m"; - const DISABLED_VALUE_COLOR: &str = "\x1b[0;90m"; - const ENTRY_COLOR: &str = "\x1b[0;37m"; - const DISABLED_ENTRY_COLOR: &str = "\x1b[0;90m"; - const COLOR_RESET: &str = "\x1b[0m"; - - println!("{}posix system configuration{}", HEADER_COLOR, COLOR_RESET); + println!( + "{}", + "posix system configuration".underline().bright_green() + ); println!(); - println!(" {}system info{}", HEADER_COLOR, COLOR_RESET); + println!(" {}", "system info".underline().bright_green()); for i in all::().collect::>() { println!( - " {ENTRY_COLOR}{:<50}{COLOR_RESET} {VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - i.value(), + " {:<50} {}", + format!("{:?}", i).white(), + format!("{}", i.value()).bright_blue(), ); } println!(); - println!(" {}limits{}", HEADER_COLOR, COLOR_RESET); + println!(" {}", "limit".underline().bright_green()); for i in all::().collect::>() { let limit = i.value(); let limit = if limit == 0 { @@ -47,57 +46,63 @@ pub fn print_system_configuration() { limit.to_string() }; println!( - " {ENTRY_COLOR}{:<50}{COLOR_RESET} {VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - limit, + " {:<50} {}", + format!("{:?}", i).white(), + limit.bright_blue(), ); } println!(); - println!(" {}options{}", HEADER_COLOR, COLOR_RESET); + println!(" {}", "options".underline().bright_green()); for i in all::().collect::>() { if i.is_available() { println!( - " {ENTRY_COLOR}{:<50}{COLOR_RESET} {VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - i.is_available(), + " {:<50} {}", + format!("{:?}", i).white(), + format!("{}", i.is_available()).bright_blue() ); } else { - println!( - " {DISABLED_ENTRY_COLOR}{:<50}{COLOR_RESET} {DISABLED_VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - i.is_available(), - ); + println!(" {:<50} {}", format!("{:?}", i), i.is_available(),); } } println!(); - println!(" {}features{}", HEADER_COLOR, COLOR_RESET); + println!(" {}", "features".underline().bright_green()); for i in all::().collect::>() { if i.is_available() { println!( - " {ENTRY_COLOR}{:<50}{COLOR_RESET} {VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - i.is_available(), + " {:<50} {}", + format!("{:?}", i).white(), + format!("{}", i.is_available()).bright_blue(), ); } else { - println!( - " {DISABLED_ENTRY_COLOR}{:<50}{COLOR_RESET} {DISABLED_VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - i.is_available(), - ); + println!(" {:<50} {}", format!("{:?}", i), i.is_available(),); } } println!(); - println!(" {}process resource limits{}", HEADER_COLOR, COLOR_RESET); + println!(" {}", "process resource limits".underline().bright_green()); for i in all::().collect::>() { - println!( - " {ENTRY_COLOR}{:<43}{COLOR_RESET} soft: {VALUE_COLOR}{:<24}{COLOR_RESET} hard: {VALUE_COLOR}{}{COLOR_RESET}", - format!("{:?}", i), - i.soft_limit(), - i.hard_limit() - ); + let soft_limit_result = catch_unwind(|| i.soft_limit()); + let hard_limit_result = catch_unwind(|| i.hard_limit()); + + match (soft_limit_result, hard_limit_result) { + (Ok(soft), Ok(hard)) => { + println!( + " {:<43} soft: {:<24} hard: {}", + format!("{:?}", i).white(), + format!("{}", soft).bright_blue(), + format!("{}", hard).bright_blue() + ); + } + (Err(e), _) | (_, Err(e)) => { + println!( + " {:<43} Error: {}", + format!("{:?}", i).white(), + format!("Unable to acquire limit due to: {:?}", e).red() + ); + } + } } } @@ -113,6 +118,18 @@ pub fn generate() -> Result<()> { let default_file_path = config_dir.join("config.toml"); + if default_file_path.exists() { + let proceed = Confirm::new() + .with_prompt("Configuration file already exists. Do you want to overwrite it?") + .default(false) + .interact()?; + + if !proceed { + println!("Operation cancelled. Configuration file was not overwritten."); + return Ok(()); + } + } + let toml_string = toml::to_string_pretty(&Config::default())?; let mut file = File::create(&default_file_path)?;