diff --git a/Cargo.lock b/Cargo.lock index 0695a6d..119d39d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -100,12 +100,6 @@ version = "3.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" -[[package]] -name = "bytecount" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce" - [[package]] name = "byteorder" version = "1.5.0" @@ -114,9 +108,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cc" -version = "1.2.12" +version = "1.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2" +checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda" dependencies = [ "shlex", ] @@ -280,12 +274,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" -[[package]] -name = "fnv" -version = "1.0.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" - [[package]] name = "getrandom" version = "0.3.1" @@ -304,12 +292,6 @@ version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" -[[package]] -name = "heck" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" - [[package]] name = "hermit-abi" version = "0.3.9" @@ -489,9 +471,9 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" [[package]] name = "once_cell" -version = "1.20.2" +version = "1.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" [[package]] name = "os_display" @@ -499,18 +481,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a6229bad892b46b0dcfaaeb18ad0d2e56400f5aaea05b768bde96e73676cf75" dependencies = [ - "unicode-width 0.1.14", -] - -[[package]] -name = "papergrid" -version = "0.14.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b915f831b85d984193fdc3d3611505871dc139b2534530fa01c1a6a6707b6723" -dependencies = [ - "bytecount", - "fnv", - "unicode-width 0.2.0", + "unicode-width", ] [[package]] @@ -587,28 +558,6 @@ dependencies = [ "yansi", ] -[[package]] -name = "proc-macro-error-attr2" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" -dependencies = [ - "proc-macro2", - "quote", -] - -[[package]] -name = "proc-macro-error2" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" -dependencies = [ - "proc-macro-error-attr2", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "proc-macro2" version = "1.0.93" @@ -666,7 +615,7 @@ checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" dependencies = [ "rand_chacha", "rand_core 0.9.0", - "zerocopy 0.8.16", + "zerocopy 0.8.17", ] [[package]] @@ -692,7 +641,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" dependencies = [ "getrandom", - "zerocopy 0.8.16", + "zerocopy 0.8.17", ] [[package]] @@ -889,29 +838,6 @@ dependencies = [ "windows", ] -[[package]] -name = "tabled" -version = "0.18.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121d8171ee5687a4978d1b244f7d99c43e7385a272185a2f1e1fa4dc0979d444" -dependencies = [ - "papergrid", - "tabled_derive", -] - -[[package]] -name = "tabled_derive" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52d9946811baad81710ec921809e2af67ad77719418673b2a3794932d57b7538" -dependencies = [ - "heck", - "proc-macro-error2", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "tempfile" version = "3.16.0" @@ -955,7 +881,7 @@ dependencies = [ "smawk", "terminal_size 0.2.6", "unicode-linebreak", - "unicode-width 0.1.14", + "unicode-width", ] [[package]] @@ -1009,12 +935,6 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" -[[package]] -name = "unicode-width" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" - [[package]] name = "utf8parse" version = "0.2.2" @@ -1127,7 +1047,6 @@ dependencies = [ "clap", "serde", "serde_json", - "tabled", "uucore", ] @@ -1539,11 +1458,11 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.16" +version = "0.8.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b8c07a70861ce02bad1607b5753ecb2501f67847b9f9ada7c160fff0ec6300c" +checksum = "aa91407dacce3a68c56de03abe2760159582b846c6a4acd2f456618087f12713" dependencies = [ - "zerocopy-derive 0.8.16", + "zerocopy-derive 0.8.17", ] [[package]] @@ -1559,9 +1478,9 @@ dependencies = [ [[package]] name = "zerocopy-derive" -version = "0.8.16" +version = "0.8.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5226bc9a9a9836e7428936cde76bb6b22feea1a8bfdbc0d241136e4d13417e25" +checksum = "06718a168365cad3d5ff0bb133aad346959a2074bd4a85c121255a11304a8626" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index f2864ca..d5520b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,6 @@ regex = "1.10.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.122" sysinfo = "0.33" -tabled = "0.18.0" tempfile = "3.9.0" textwrap = { version = "0.16.0", features = ["terminal_size"] } uucore = "0.0.29" diff --git a/src/uu/lsmem/Cargo.toml b/src/uu/lsmem/Cargo.toml index 516aa1d..47cd8d0 100644 --- a/src/uu/lsmem/Cargo.toml +++ b/src/uu/lsmem/Cargo.toml @@ -13,6 +13,5 @@ path = "src/main.rs" [dependencies] uucore = { workspace = true } clap = { workspace = true } -serde = { workspace = true } +serde = { workspace = true } serde_json = { workspace = true } -tabled = { workspace = true } diff --git a/src/uu/lsmem/src/lsmem.rs b/src/uu/lsmem/src/lsmem.rs index 1af7e33..c0b051e 100644 --- a/src/uu/lsmem/src/lsmem.rs +++ b/src/uu/lsmem/src/lsmem.rs @@ -15,15 +15,6 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use uucore::{error::UResult, format_usage, help_about, help_usage}; -use tabled::{ - settings::{ - location::ByColumnName, - object::{self, Rows}, - Alignment, Modify, Remove, Style, - }, - Table, Tabled, -}; - const ABOUT: &str = help_about!("lsmem.md"); const USAGE: &str = help_usage!("lsmem.md"); @@ -45,14 +36,7 @@ const PATH_SUB_REMOVABLE: &str = "removable"; const PATH_SUB_STATE: &str = "state"; const NAME_MEMORY: &str = "memory"; -// struct ColDesc { -// name: &'static str, // Rust's equivalent to `const char *` -// whint: f64, // Rust uses `f64` for double precision floating-point numbers -// flags: i32, // Using `i32` for integers -// help: &'static str, // Rust's equivalent to `const char *` -// } - -#[derive(Debug, Deserialize)] +#[derive(Debug, PartialEq, Serialize, Deserialize)] enum Columns { #[serde(rename = "RANGE")] Range, @@ -69,52 +53,59 @@ enum Columns { #[serde(rename = "ZONES")] Zones, } -// const SCOLS_FL_RIGHT: i32 = 1; -// static COLDESCS: [ColDesc; 7] = [ -// ColDesc { -// name: "RANGE", -// whint: 0.0, -// flags: 0, -// help: "start and end address of the memory range", -// }, -// ColDesc { -// name: "SIZE", -// whint: 5.0, -// flags: SCOLS_FL_RIGHT, -// help: "size of the memory range", -// }, -// ColDesc { -// name: "STATE", -// whint: 0.0, -// flags: SCOLS_FL_RIGHT, -// help: "online status of the memory range", -// }, -// ColDesc { -// name: "REMOVABLE", -// whint: 0.0, -// flags: SCOLS_FL_RIGHT, -// help: "memory is removable", -// }, -// ColDesc { -// name: "BLOCK", -// whint: 0.0, -// flags: SCOLS_FL_RIGHT, -// help: "memory block number or blocks range", -// }, -// ColDesc { -// name: "NODE", -// whint: 0.0, -// flags: SCOLS_FL_RIGHT, -// help: "numa node of memory", -// }, -// ColDesc { -// name: "ZONES", -// whint: 0.0, -// flags: SCOLS_FL_RIGHT, -// help: "valid zones for the memory range", -// }, -// ]; +const ALL_COLUMNS: &[Columns] = &[ + Columns::Range, + Columns::Size, + Columns::State, + Columns::Removable, + Columns::Block, + Columns::Node, + Columns::Zones, +]; + +impl Columns { + fn get_name(&self) -> String { + serde_json::to_string(self) + .unwrap() + .trim_matches('"') + .to_string() + } + + #[allow(dead_code)] + fn get_float_direction(&self) -> &'static str { + match self { + Columns::Range => "<", + Columns::Size => ">", + Columns::State => ">", + Columns::Removable => ">", + Columns::Block => ">", + Columns::Node => ">", + Columns::Zones => ">", + } + } + + #[allow(dead_code)] + fn get_width_hint(&self) -> i8 { + if self == &Columns::Size { + 5 + } else { + self.get_name().len() as i8 + } + } + + fn get_help(&self) -> &'static str { + match self { + Columns::Range => "start and end address of the memory range", + Columns::Size => "size of the memory range", + Columns::State => "online status of the memory range", + Columns::Removable => "memory is removable", + Columns::Block => "memory block number or blocks range", + Columns::Node => "numa node of memory", + Columns::Zones => "valid zones for the memory range", + } + } +} #[derive(Debug, Deserialize, PartialEq, Clone, Copy)] enum ZoneId { @@ -195,22 +186,15 @@ impl MemoryBlock { } } -#[derive(Tabled, Default, Serialize)] +#[derive(Default, Serialize)] struct TableRow { - #[tabled(rename = "RANGE")] range: String, - #[tabled(rename = "SIZE")] size: String, - #[tabled(rename = "STATE")] state: String, - #[tabled(rename = "REMOVABLE")] removable: String, - #[tabled(rename = "BLOCK")] block: String, - #[tabled(rename = "NODE")] - #[serde(skip_serializing)] node: String, - #[tabled(rename = "ZONES")] + #[allow(unused)] #[serde(skip_serializing)] zones: String, } @@ -237,20 +221,20 @@ struct TableRowJson { struct Options { all: bool, - have_nodes: bool, - raw: bool, - export: bool, - json: bool, - noheadings: bool, - list_all: bool, bytes: bool, + export: bool, + have_nodes: bool, + have_zones: bool, + json: bool, + list_all: bool, + noheadings: bool, + raw: bool, + split_by_node: bool, + split_by_removable: bool, + split_by_state: bool, + split_by_zones: bool, want_summary: bool, want_table: bool, - split_by_node: bool, - split_by_state: bool, - split_by_removable: bool, - split_by_zones: bool, - have_zones: bool, } struct Lsmem { @@ -457,14 +441,11 @@ fn create_table_rows(lsmem: &Lsmem, opts: &Options) -> Vec { let size = blk.count * lsmem.block_size; row.range = format!("0x{:016x}-0x{:016x}", start, start + size - 1); - // Size (always at least 5 long) + // Size row.size = if opts.bytes { - format!("{:>5}", blk.count * lsmem.block_size) + format!("{}", blk.count * lsmem.block_size) } else { - format!( - "{:>5}", - utils::size_to_human_string(blk.count * lsmem.block_size) - ) + utils::size_to_human_string(blk.count * lsmem.block_size) }; // State @@ -500,31 +481,46 @@ fn create_table_rows(lsmem: &Lsmem, opts: &Options) -> Vec { } fn print_table(lsmem: &Lsmem, opts: &Options) { - let mut table = Table::new(create_table_rows(lsmem, opts)); - table - .with(Style::blank()) - .with(Modify::new(object::Columns::new(1..)).with(Alignment::right())); + let table_rows = create_table_rows(lsmem, opts); + let mut col_widths = vec![5, 5, 6, 9, 5]; // Initialize with default minimum widths - // Remove padding in beginning and end of table - table.get_config_mut().set_padding( - tabled::grid::config::Entity::Global, - tabled::grid::config::Sides::new( - tabled::grid::config::Indent::default(), - tabled::grid::config::Indent::default(), - tabled::grid::config::Indent::default(), - tabled::grid::config::Indent::default(), - ), - ); - - // the default version - table.with(Remove::column(ByColumnName::new("NODE"))); - table.with(Remove::column(ByColumnName::new("ZONES"))); - - if opts.noheadings { - table.with(Remove::row(Rows::first())); + // Calculate minimum column widths based on the actual data + for row in &table_rows { + col_widths[0] = col_widths[0].max(row.range.len()); + col_widths[1] = col_widths[1].max(row.size.len()); + col_widths[2] = col_widths[2].max(row.state.len()); + col_widths[3] = col_widths[3].max(row.removable.len()); + col_widths[4] = col_widths[4].max(row.block.len()); } - println!("{table}"); + if !opts.noheadings { + println!( + "{:col1$} {:>col2$} {:>col3$} {:>col4$}", + "RANGE", + "SIZE", + "STATE", + "REMOVABLE", + "BLOCK", + col0 = col_widths[0], + col1 = col_widths[1], + col2 = col_widths[2], + col3 = col_widths[3], + col4 = col_widths[4], + ); + } + + for row in table_rows { + let mut columns = vec![]; + columns.push(format!("{:col1$}", row.size, col1 = col_widths[1])); + columns.push(format!("{:>col2$}", row.state, col2 = col_widths[2])); + columns.push(format!("{:>col3$}", row.removable, col3 = col_widths[3])); + columns.push(format!("{:>col4$}", row.block, col4 = col_widths[4])); + // Default version skips NODE and ZONES + // columns.push(format!("{:>col5$}", row.node, col5 = col_widths[5])); + // columns.push(format!("{:>col6$}", row.zones, col6 = col_widths[6])); + println!("{}", columns.join(" ")); + } } fn print_json(lsmem: &Lsmem, opts: &Options) { @@ -685,4 +681,12 @@ pub fn uu_app() -> Command { .action(ArgAction::SetTrue) .conflicts_with_all([options::JSON, options::PAIRS]), ) + .after_help(&format!( + "Available output columns:\n{}", + ALL_COLUMNS + .iter() + .map(|col| format!("{:>11} {}", col.get_name(), col.get_help())) + .collect::>() + .join("\n") + )) }