Skip to content

Commit

Permalink
utils: table format boolean as X or ``
Browse files Browse the repository at this point in the history
Allows for commands to put booleans into ResponseTable while preserving
the formatted table and at the same time improving the JSON output to
contain booleans instead of `X` or `` strings.
  • Loading branch information
kamirr committed Aug 8, 2024
1 parent 2b6daee commit 9ab634a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
6 changes: 3 additions & 3 deletions core/identity/src/cli/identity/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ pub async fn list() -> Result<CommandOutput> {
.into_iter()
.map(|identity| {
serde_json::json! {[
if identity.is_default { "X" } else { "" },
if identity.is_locked { "X" } else { "" },
if identity.deleted { "X" } else { "" },
identity.is_default,
identity.is_locked,
identity.deleted,
identity.alias,
identity.node_id
]}
Expand Down
4 changes: 2 additions & 2 deletions core/payment/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ Typically operation should take less than 1 minute.
account.driver,
account.network,
account.token,
if account.send { "X" } else { "" },
if account.receive { "X" } else { "" }
account.send,
account.receive
]}
})
.collect(),
Expand Down
34 changes: 16 additions & 18 deletions utils/cli/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ use crate::cmd::CommandOutput;
use prettytable::{color, format, format::TableFormat, Attr, Cell, Row, Table};
use std::collections::HashMap;

fn json_value_table_string(value: &serde_json::Value) -> String {
use serde_json::Value;

match value {
Value::String(s) => s.to_string(),
Value::Bool(true) => "X".to_string(),
Value::Bool(false) => "".to_string(),
Value::Null => "".into(),
v => v.to_string(),
}
}

pub fn print_table(
columns: &[String],
values: &[serde_json::Value],
Expand All @@ -25,16 +37,7 @@ pub fn print_table(
}
for row in values {
if let Some(row_items) = row.as_array() {
use serde_json::Value;

let row_strings = row_items
.iter()
.map(|v| match v {
Value::String(s) => s.to_string(),
Value::Null => "".into(),
v => v.to_string(),
})
.collect();
let row_strings = row_items.iter().map(json_value_table_string).collect();
table.add_row(row_strings);
}
}
Expand All @@ -44,17 +47,12 @@ pub fn print_table(
let l = summary.len();
for (idx, row) in summary.iter().enumerate() {
if let Some(row_items) = row.as_array() {
use serde_json::Value;

let row_strings = Row::new(
row_items
.iter()
.map(|v| {
let c = Cell::new(&match v {
Value::String(s) => s.to_string(),
Value::Null => "".into(),
v => v.to_string(),
});
.map(json_value_table_string)
.map(|s| {
let c = Cell::new(&s);

if idx == l - 1 {
c.with_style(Attr::Bold)
Expand Down

0 comments on commit 9ab634a

Please sign in to comment.