From b93bcf821d4df595b6165eddfbffabf7f124928c Mon Sep 17 00:00:00 2001 From: Yang Hau Date: Mon, 26 Aug 2024 01:58:59 +0800 Subject: [PATCH] lsmem: Add option raw --- src/uu/lsmem/src/lsmem.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/uu/lsmem/src/lsmem.rs b/src/uu/lsmem/src/lsmem.rs index da309a1..89c2f28 100644 --- a/src/uu/lsmem/src/lsmem.rs +++ b/src/uu/lsmem/src/lsmem.rs @@ -32,6 +32,7 @@ mod options { pub const NOHEADINGS: &str = "noheadings"; pub const JSON: &str = "json"; pub const PAIRS: &str = "pairs"; + pub const RAW: &str = "raw"; } // const BUFSIZ: usize = 1024; @@ -220,6 +221,12 @@ impl TableRow { self.range, self.size, self.state, self.removable, self.block ) } + fn to_raw_string(&self) -> String { + format!( + r#"{} {} {} {} {}"#, + self.range, self.size, self.state, self.removable, self.block + ) + } } #[derive(Serialize)] @@ -229,7 +236,7 @@ struct TableRowJson { struct Options { have_nodes: bool, - // raw: bool, + raw: bool, export: bool, json: bool, noheadings: bool, @@ -273,7 +280,7 @@ impl Options { fn new() -> Options { Options { have_nodes: false, - // raw: false, + raw: false, export: false, json: false, noheadings: false, @@ -526,6 +533,18 @@ fn print_pairs(lsmem: &Lsmem, opts: &Options) { println!("{table_pairs_string}"); } +fn print_raw(lsmem: &Lsmem, opts: &Options) { + let table_rows = create_table_rows(lsmem, opts); + let mut table_raw_string = String::new(); + for row in table_rows { + table_raw_string += &row.to_raw_string(); + table_raw_string += "\n"; + } + // remove the last newline + table_raw_string.pop(); + println!("{table_raw_string}"); +} + fn print_summary(lsmem: &Lsmem, opts: &Options) { if opts.bytes { println!("{:<23} {:>15}", "Memory block size:", lsmem.block_size); @@ -571,8 +590,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { opts.noheadings = matches.get_flag(options::NOHEADINGS); opts.json = matches.get_flag(options::JSON); opts.export = matches.get_flag(options::PAIRS); + opts.raw = matches.get_flag(options::RAW); - if opts.json || opts.export { + if opts.json || opts.export || opts.raw { opts.want_summary = false; } @@ -583,6 +603,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { print_json(&lsmem, &opts); } else if opts.export { print_pairs(&lsmem, &opts); + } else if opts.raw { + print_raw(&lsmem, &opts); } else { print_table(&lsmem, &opts); } @@ -629,4 +651,11 @@ pub fn uu_app() -> Command { .help("use key=\"value\" output format") .action(ArgAction::SetTrue), ) + .arg( + Arg::new(options::RAW) + .short('r') + .long("raw") + .help("use raw output format") + .action(ArgAction::SetTrue), + ) }