lsmem: Fix specified --output columns with pairs print

This commit is contained in:
Foorack
2025-02-09 12:05:39 +01:00
parent 924537e792
commit 8fa6345470
2 changed files with 25 additions and 30 deletions

View File

@@ -236,12 +236,6 @@ impl TableRow {
Column::Zones => self.zones.clone(), Column::Zones => self.zones.clone(),
} }
} }
fn to_pairs_string(&self) -> String {
format!(
r#"RANGE="{}" SIZE="{}" STATE="{}" REMOVABLE="{}" BLOCK="{}""#,
self.range, self.size, self.state, self.removable, self.block
)
}
} }
#[derive(Serialize)] #[derive(Serialize)]
@@ -554,29 +548,29 @@ fn print_table(lsmem: &Lsmem, opts: &Options) {
} }
if !opts.noheadings { if !opts.noheadings {
let mut output = vec![]; let mut column_names = vec![];
for (i, column) in opts.columns.iter().enumerate() { for (i, column) in opts.columns.iter().enumerate() {
let formatted = if column.get_float_right() { let formatted = if column.get_float_right() {
format!("{:>width$}", column.get_name(), width = col_widths[i]) format!("{:>width$}", column.get_name(), width = col_widths[i])
} else { } else {
format!("{:<width$}", column.get_name(), width = col_widths[i]) format!("{:<width$}", column.get_name(), width = col_widths[i])
}; };
output.push(formatted); column_names.push(formatted);
} }
println!("{}", output.join(" ")); println!("{}", column_names.join(" "));
} }
for row in table_rows { for row in table_rows {
let mut output = vec![]; let mut column_values = vec![];
for (i, column) in opts.columns.iter().enumerate() { for (i, column) in opts.columns.iter().enumerate() {
let formatted = if column.get_float_right() { let formatted = if column.get_float_right() {
format!("{:>width$}", row.get_value(column), width = col_widths[i]) format!("{:>width$}", row.get_value(column), width = col_widths[i])
} else { } else {
format!("{:<width$}", row.get_value(column), width = col_widths[i]) format!("{:<width$}", row.get_value(column), width = col_widths[i])
}; };
output.push(formatted); column_values.push(formatted);
} }
println!("{}", output.join(" ")); println!("{}", column_values.join(" "));
} }
} }
@@ -596,33 +590,35 @@ fn print_json(lsmem: &Lsmem, opts: &Options) {
fn print_pairs(lsmem: &Lsmem, opts: &Options) { fn print_pairs(lsmem: &Lsmem, opts: &Options) {
let table_rows = create_table_rows(lsmem, opts); let table_rows = create_table_rows(lsmem, opts);
let table_pairs_string = table_rows
.into_iter() for row in table_rows {
.map(|row| row.to_pairs_string()) let mut pairs = Vec::new();
.collect::<Vec<_>>() for col in &opts.columns {
.join("\n"); pairs.push(format!("{}=\"{}\"", col.get_name(), row.get_value(col)));
println!("{table_pairs_string}"); }
println!("{}", pairs.join(" "));
}
} }
fn print_raw(lsmem: &Lsmem, opts: &Options) { fn print_raw(lsmem: &Lsmem, opts: &Options) {
let table_rows = create_table_rows(lsmem, opts); let table_rows = create_table_rows(lsmem, opts);
if !opts.noheadings { if !opts.noheadings {
let headings = opts let column_names = opts
.columns .columns
.iter() .iter()
.map(|col| col.get_name().to_string()) .map(|col| col.get_name().to_string())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
println!("{}", headings.join(" ")); println!("{}", column_names.join(" "));
} }
for row in table_rows { for row in table_rows {
let columns = opts let column_values = opts
.columns .columns
.iter() .iter()
.map(|col| row.get_value(col).to_string()) .map(|col| row.get_value(col).to_string())
.collect::<Vec<String>>(); .collect::<Vec<String>>();
println!("{}", columns.join(" ")); println!("{}", column_values.join(" "));
} }
} }

View File

@@ -48,14 +48,13 @@ fn test_columns_raw() {
// ); // );
// } // }
// FAILS, COMMENT FOR NOW - TODO #[test]
// #[test] fn test_columns_pairs() {
// fn test_columns_pairs() { sysroot_test_with_args(
// sysroot_test_with_args( "test_lsmem_columns_pairs.expected",
// "test_lsmem_columns_pairs.expected", &["-o", "block,size", "-P"],
// &["-o", "block,size", "-P"], );
// ); }
// }
#[test] #[test]
fn test_json() { fn test_json() {