Skip to content

Commit

Permalink
lsmem: Add json
Browse files Browse the repository at this point in the history
  • Loading branch information
howjmay committed Aug 25, 2024
1 parent f83d22b commit 14ca543
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/uu/lsmem/src/lsmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod utils;

use clap::{crate_version, Command};
use clap::{Arg, ArgAction};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::borrow::Borrow;
use std::fs;
use std::io::{self, BufRead, BufReader};
Expand All @@ -30,6 +30,7 @@ const USAGE: &str = help_usage!("lsmem.md");
mod options {
pub const BYTES: &str = "bytes";
pub const NOHEADINGS: &str = "noheadings";
pub const JSON: &str = "json";
}

// const BUFSIZ: usize = 1024;
Expand Down Expand Up @@ -191,7 +192,7 @@ impl MemoryBlock {
}
}

#[derive(Tabled, Default)]
#[derive(Tabled, Default, Serialize)]
struct TableRow {
#[tabled(rename = "RANGE")]
range: String,
Expand All @@ -209,11 +210,16 @@ struct TableRow {
zones: String,
}

#[derive(Serialize)]
struct TableRowJson {
memory: Vec<TableRow>,
}

struct Options {
have_nodes: bool,
// raw: bool,
// export: bool,
// json: bool,
json: bool,
noheadings: bool,
// summary: bool,
list_all: bool,
Expand Down Expand Up @@ -257,7 +263,7 @@ impl Options {
have_nodes: false,
// raw: false,
// export: false,
// json: false,
json: false,
noheadings: false,
// summary: false,
list_all: false,
Expand Down Expand Up @@ -418,8 +424,8 @@ fn memory_block_read_attrs(opts: &Options, path: &PathBuf) -> MemoryBlock {
blk
}

fn create_table(lsmem: &Lsmem, opts: &Options) -> tabled::Table {
let mut table = Vec::<TableRow>::new();
fn create_table_rows(lsmem: &Lsmem, opts: &Options) -> Vec<TableRow> {
let mut table_rows = Vec::<TableRow>::new();

for i in 0..lsmem.nblocks {
let mut row = TableRow::default();
Expand Down Expand Up @@ -465,13 +471,13 @@ fn create_table(lsmem: &Lsmem, opts: &Options) -> tabled::Table {
row.node = format!("{}", blk.node);
}

table.push(row);
table_rows.push(row);
}
Table::new(table)
table_rows
}

fn print_table(lsmem: &Lsmem, opts: &Options) {
let mut table = create_table(lsmem, opts);
let mut table = Table::new(create_table_rows(lsmem, opts));
table
.with(Style::blank())
.with(Modify::new(object::Columns::new(1..)).with(Alignment::right()));
Expand All @@ -487,6 +493,15 @@ fn print_table(lsmem: &Lsmem, opts: &Options) {
println!("{table}");
}

fn print_json(lsmem: &Lsmem, opts: &Options) {
let table_json = TableRowJson {
memory: create_table_rows(lsmem, opts),
};

let table_json_string = serde_json::to_string(&table_json).unwrap();
println!("{table_json_string}");
}

fn print_summary(lsmem: &Lsmem, opts: &Options) {
if opts.bytes {
println!("{:<23} {:>15}", "Memory block size:", lsmem.block_size);
Expand Down Expand Up @@ -534,10 +549,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
read_info(&mut lsmem, &mut opts);

if opts.want_table {
print_table(&lsmem, &opts);
if opts.json {
print_json(&lsmem, &opts);
} else {
print_table(&lsmem, &opts);
}
}

if opts.want_summary {
if opts.want_summary && !opts.json {
print_summary(&lsmem, &opts);
}

Expand All @@ -564,4 +583,11 @@ pub fn uu_app() -> Command {
.help("don't print headings")
.action(ArgAction::SetTrue),
)
.arg(
Arg::new(options::JSON)
.short('J')
.long("json")
.help("use JSON output format")
.action(ArgAction::SetTrue),
)
}

0 comments on commit 14ca543

Please sign in to comment.