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 26, 2024
1 parent 422e211 commit d8240bc
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 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,11 +236,10 @@ struct TableRowJson {

struct Options {
have_nodes: bool,
// raw: bool,
raw: bool,
export: bool,
json: bool,
noheadings: bool,
// summary: bool,
list_all: bool,
bytes: bool,
want_summary: bool,
Expand Down Expand Up @@ -273,11 +279,10 @@ impl Options {
fn new() -> Options {
Options {
have_nodes: false,
// raw: false,
raw: false,
export: false,
json: false,
noheadings: false,
// summary: false,
list_all: false,
bytes: false,
want_summary: true, // default true
Expand Down Expand Up @@ -510,20 +515,32 @@ fn print_json(lsmem: &Lsmem, opts: &Options) {
memory: create_table_rows(lsmem, opts),
};

let table_json_string = serde_json::to_string_pretty(&table_json).unwrap();
let mut table_json_string = serde_json::to_string_pretty(&table_json).unwrap();
table_json_string = table_json_string.replace("\"yes\"", "true");
table_json_string = table_json_string.replace("\"no\"", "false");
println!("{table_json_string}");
}

fn print_pairs(lsmem: &Lsmem, opts: &Options) {
let table_rows = create_table_rows(lsmem, opts);
let mut table_pairs_string = String::new();
let table_pairs_string = table_rows
.into_iter()
.map(|row| row.to_pairs_string())
.collect::<Vec<_>>()
.join("\n");
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_pairs_string += &row.to_pairs_string();
table_pairs_string += "\n";
table_raw_string += &row.to_raw_string();
table_raw_string += "\n";
}
// remove the last newline
table_pairs_string.pop();
println!("{table_pairs_string}");
table_raw_string.pop();
println!("{table_raw_string}");
}

fn print_summary(lsmem: &Lsmem, opts: &Options) {
Expand Down Expand Up @@ -571,8 +588,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 +601,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 @@ -620,13 +640,23 @@ pub fn uu_app() -> Command {
.short('J')
.long("json")
.help("use JSON output format")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.conflicts_with_all(&[options::PAIRS, options::RAW]),
)
.arg(
Arg::new(options::PAIRS)
.short('P')
.long("pairs")
.help("use key=\"value\" output format")
.action(ArgAction::SetTrue),
.action(ArgAction::SetTrue)
.conflicts_with_all(&[options::JSON, options::RAW]),
)
.arg(
Arg::new(options::RAW)
.short('r')
.long("raw")
.help("use raw output format")
.action(ArgAction::SetTrue)
.conflicts_with_all(&[options::JSON, options::PAIRS]),
)
}

0 comments on commit d8240bc

Please sign in to comment.