lsmem: Remove tabled for fine-grained display

This commit is contained in:
Foorack
2025-02-08 20:04:39 +01:00
parent 165d1ed5fa
commit 6fbd0cd072
4 changed files with 128 additions and 207 deletions

107
Cargo.lock generated
View File

@@ -100,12 +100,6 @@ version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "bytecount"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce"
[[package]] [[package]]
name = "byteorder" name = "byteorder"
version = "1.5.0" version = "1.5.0"
@@ -114,9 +108,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.2.12" version = "1.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "755717a7de9ec452bf7f3f1a3099085deabd7f2962b861dae91ecd7a365903d2" checksum = "c7777341816418c02e033934a09f20dc0ccaf65a5201ef8a450ae0105a573fda"
dependencies = [ dependencies = [
"shlex", "shlex",
] ]
@@ -280,12 +274,6 @@ version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
[[package]]
name = "fnv"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.3.1" version = "0.3.1"
@@ -304,12 +292,6 @@ version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2" checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.3.9" version = "0.3.9"
@@ -489,9 +471,9 @@ checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.20.2" version = "1.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e"
[[package]] [[package]]
name = "os_display" name = "os_display"
@@ -499,18 +481,7 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6229bad892b46b0dcfaaeb18ad0d2e56400f5aaea05b768bde96e73676cf75" checksum = "7a6229bad892b46b0dcfaaeb18ad0d2e56400f5aaea05b768bde96e73676cf75"
dependencies = [ dependencies = [
"unicode-width 0.1.14", "unicode-width",
]
[[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",
] ]
[[package]] [[package]]
@@ -587,28 +558,6 @@ dependencies = [
"yansi", "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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.93" version = "1.0.93"
@@ -666,7 +615,7 @@ checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94"
dependencies = [ dependencies = [
"rand_chacha", "rand_chacha",
"rand_core 0.9.0", "rand_core 0.9.0",
"zerocopy 0.8.16", "zerocopy 0.8.17",
] ]
[[package]] [[package]]
@@ -692,7 +641,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff"
dependencies = [ dependencies = [
"getrandom", "getrandom",
"zerocopy 0.8.16", "zerocopy 0.8.17",
] ]
[[package]] [[package]]
@@ -889,29 +838,6 @@ dependencies = [
"windows", "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]] [[package]]
name = "tempfile" name = "tempfile"
version = "3.16.0" version = "3.16.0"
@@ -955,7 +881,7 @@ dependencies = [
"smawk", "smawk",
"terminal_size 0.2.6", "terminal_size 0.2.6",
"unicode-linebreak", "unicode-linebreak",
"unicode-width 0.1.14", "unicode-width",
] ]
[[package]] [[package]]
@@ -1009,12 +935,6 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "unicode-width"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
[[package]] [[package]]
name = "utf8parse" name = "utf8parse"
version = "0.2.2" version = "0.2.2"
@@ -1127,7 +1047,6 @@ dependencies = [
"clap", "clap",
"serde", "serde",
"serde_json", "serde_json",
"tabled",
"uucore", "uucore",
] ]
@@ -1539,11 +1458,11 @@ dependencies = [
[[package]] [[package]]
name = "zerocopy" name = "zerocopy"
version = "0.8.16" version = "0.8.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b8c07a70861ce02bad1607b5753ecb2501f67847b9f9ada7c160fff0ec6300c" checksum = "aa91407dacce3a68c56de03abe2760159582b846c6a4acd2f456618087f12713"
dependencies = [ dependencies = [
"zerocopy-derive 0.8.16", "zerocopy-derive 0.8.17",
] ]
[[package]] [[package]]
@@ -1559,9 +1478,9 @@ dependencies = [
[[package]] [[package]]
name = "zerocopy-derive" name = "zerocopy-derive"
version = "0.8.16" version = "0.8.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5226bc9a9a9836e7428936cde76bb6b22feea1a8bfdbc0d241136e4d13417e25" checksum = "06718a168365cad3d5ff0bb133aad346959a2074bd4a85c121255a11304a8626"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View File

@@ -53,7 +53,6 @@ regex = "1.10.2"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.122" serde_json = "1.0.122"
sysinfo = "0.33" sysinfo = "0.33"
tabled = "0.18.0"
tempfile = "3.9.0" tempfile = "3.9.0"
textwrap = { version = "0.16.0", features = ["terminal_size"] } textwrap = { version = "0.16.0", features = ["terminal_size"] }
uucore = "0.0.29" uucore = "0.0.29"

View File

@@ -13,6 +13,5 @@ path = "src/main.rs"
[dependencies] [dependencies]
uucore = { workspace = true } uucore = { workspace = true }
clap = { workspace = true } clap = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
tabled = { workspace = true }

View File

@@ -15,15 +15,6 @@ use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use uucore::{error::UResult, format_usage, help_about, help_usage}; 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 ABOUT: &str = help_about!("lsmem.md");
const USAGE: &str = help_usage!("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 PATH_SUB_STATE: &str = "state";
const NAME_MEMORY: &str = "memory"; const NAME_MEMORY: &str = "memory";
// struct ColDesc { #[derive(Debug, PartialEq, Serialize, Deserialize)]
// 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)]
enum Columns { enum Columns {
#[serde(rename = "RANGE")] #[serde(rename = "RANGE")]
Range, Range,
@@ -69,52 +53,59 @@ enum Columns {
#[serde(rename = "ZONES")] #[serde(rename = "ZONES")]
Zones, Zones,
} }
// const SCOLS_FL_RIGHT: i32 = 1;
// static COLDESCS: [ColDesc; 7] = [ const ALL_COLUMNS: &[Columns] = &[
// ColDesc { Columns::Range,
// name: "RANGE", Columns::Size,
// whint: 0.0, Columns::State,
// flags: 0, Columns::Removable,
// help: "start and end address of the memory range", Columns::Block,
// }, Columns::Node,
// ColDesc { Columns::Zones,
// name: "SIZE", ];
// whint: 5.0,
// flags: SCOLS_FL_RIGHT, impl Columns {
// help: "size of the memory range", fn get_name(&self) -> String {
// }, serde_json::to_string(self)
// ColDesc { .unwrap()
// name: "STATE", .trim_matches('"')
// whint: 0.0, .to_string()
// flags: SCOLS_FL_RIGHT, }
// help: "online status of the memory range",
// }, #[allow(dead_code)]
// ColDesc { fn get_float_direction(&self) -> &'static str {
// name: "REMOVABLE", match self {
// whint: 0.0, Columns::Range => "<",
// flags: SCOLS_FL_RIGHT, Columns::Size => ">",
// help: "memory is removable", Columns::State => ">",
// }, Columns::Removable => ">",
// ColDesc { Columns::Block => ">",
// name: "BLOCK", Columns::Node => ">",
// whint: 0.0, Columns::Zones => ">",
// flags: SCOLS_FL_RIGHT, }
// help: "memory block number or blocks range", }
// },
// ColDesc { #[allow(dead_code)]
// name: "NODE", fn get_width_hint(&self) -> i8 {
// whint: 0.0, if self == &Columns::Size {
// flags: SCOLS_FL_RIGHT, 5
// help: "numa node of memory", } else {
// }, self.get_name().len() as i8
// ColDesc { }
// name: "ZONES", }
// whint: 0.0,
// flags: SCOLS_FL_RIGHT, fn get_help(&self) -> &'static str {
// help: "valid zones for the memory range", 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)] #[derive(Debug, Deserialize, PartialEq, Clone, Copy)]
enum ZoneId { enum ZoneId {
@@ -195,22 +186,15 @@ impl MemoryBlock {
} }
} }
#[derive(Tabled, Default, Serialize)] #[derive(Default, Serialize)]
struct TableRow { struct TableRow {
#[tabled(rename = "RANGE")]
range: String, range: String,
#[tabled(rename = "SIZE")]
size: String, size: String,
#[tabled(rename = "STATE")]
state: String, state: String,
#[tabled(rename = "REMOVABLE")]
removable: String, removable: String,
#[tabled(rename = "BLOCK")]
block: String, block: String,
#[tabled(rename = "NODE")]
#[serde(skip_serializing)]
node: String, node: String,
#[tabled(rename = "ZONES")] #[allow(unused)]
#[serde(skip_serializing)] #[serde(skip_serializing)]
zones: String, zones: String,
} }
@@ -237,20 +221,20 @@ struct TableRowJson {
struct Options { struct Options {
all: bool, all: bool,
have_nodes: bool,
raw: bool,
export: bool,
json: bool,
noheadings: bool,
list_all: bool,
bytes: 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_summary: bool,
want_table: 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 { struct Lsmem {
@@ -457,14 +441,11 @@ fn create_table_rows(lsmem: &Lsmem, opts: &Options) -> Vec<TableRow> {
let size = blk.count * lsmem.block_size; let size = blk.count * lsmem.block_size;
row.range = format!("0x{:016x}-0x{:016x}", start, start + size - 1); row.range = format!("0x{:016x}-0x{:016x}", start, start + size - 1);
// Size (always at least 5 long) // Size
row.size = if opts.bytes { row.size = if opts.bytes {
format!("{:>5}", blk.count * lsmem.block_size) format!("{}", blk.count * lsmem.block_size)
} else { } else {
format!( utils::size_to_human_string(blk.count * lsmem.block_size)
"{:>5}",
utils::size_to_human_string(blk.count * lsmem.block_size)
)
}; };
// State // State
@@ -500,31 +481,46 @@ fn create_table_rows(lsmem: &Lsmem, opts: &Options) -> Vec<TableRow> {
} }
fn print_table(lsmem: &Lsmem, opts: &Options) { fn print_table(lsmem: &Lsmem, opts: &Options) {
let mut table = Table::new(create_table_rows(lsmem, opts)); let table_rows = create_table_rows(lsmem, opts);
table let mut col_widths = vec![5, 5, 6, 9, 5]; // Initialize with default minimum widths
.with(Style::blank())
.with(Modify::new(object::Columns::new(1..)).with(Alignment::right()));
// Remove padding in beginning and end of table // Calculate minimum column widths based on the actual data
table.get_config_mut().set_padding( for row in &table_rows {
tabled::grid::config::Entity::Global, col_widths[0] = col_widths[0].max(row.range.len());
tabled::grid::config::Sides::new( col_widths[1] = col_widths[1].max(row.size.len());
tabled::grid::config::Indent::default(), col_widths[2] = col_widths[2].max(row.state.len());
tabled::grid::config::Indent::default(), col_widths[3] = col_widths[3].max(row.removable.len());
tabled::grid::config::Indent::default(), col_widths[4] = col_widths[4].max(row.block.len());
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()));
} }
println!("{table}"); if !opts.noheadings {
println!(
"{:<col0$} {:>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!("{:<col0$}", row.range, col0 = col_widths[0]));
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) { fn print_json(lsmem: &Lsmem, opts: &Options) {
@@ -685,4 +681,12 @@ pub fn uu_app() -> Command {
.action(ArgAction::SetTrue) .action(ArgAction::SetTrue)
.conflicts_with_all([options::JSON, options::PAIRS]), .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::<Vec<_>>()
.join("\n")
))
} }