Skip to content

Commit

Permalink
lsmem: Add option raw
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Aug 25, 2024
1 parent 422e211 commit b93bcf8
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/uu/lsmem/src/lsmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand All @@ -229,7 +236,7 @@ struct TableRowJson {

struct Options {
have_nodes: bool,
// raw: bool,
raw: bool,
export: bool,
json: bool,
noheadings: bool,
Expand Down Expand Up @@ -273,7 +280,7 @@ impl Options {
fn new() -> Options {
Options {
have_nodes: false,
// raw: false,
raw: false,
export: false,
json: false,
noheadings: false,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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),
)
}

0 comments on commit b93bcf8

Please sign in to comment.