diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index b70d71b37b..3fbcf3af6b 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -164,7 +164,7 @@ impl Command for Table { style: TextStyle::default_field(), }, StyledString { - contents: v.into_string(", ", &config), + contents: v.into_abbreviated_string(&config), style: TextStyle::default(), }, ]) @@ -225,7 +225,7 @@ fn convert_to_table( if headers.is_empty() { // if header row is empty, this is probably a list so format it that way - row.push(("list".to_string(), item.into_string(", ", config))) + row.push(("list".to_string(), item.into_abbreviated_string(config))) } else { for header in headers.iter().skip(1) { let result = match item { @@ -241,7 +241,7 @@ fn convert_to_table( match result { Ok(value) => row.push(( (&value.get_type()).to_string(), - value.into_string(", ", config), + value.into_abbreviated_string(config), )), Err(_) => row.push(("empty".to_string(), String::new())), } diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 501a4d4c02..1dacf68799 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -328,6 +328,34 @@ impl Value { } } + /// Convert Value into string. Note that Streams will be consumed. + pub fn into_abbreviated_string(self, config: &Config) -> String { + match self { + Value::Bool { val, .. } => val.to_string(), + Value::Int { val, .. } => val.to_string(), + Value::Float { val, .. } => val.to_string(), + Value::Filesize { val, .. } => format_filesize(val, config), + Value::Duration { val, .. } => format_duration(val), + Value::Date { val, .. } => HumanTime::from(val).to_string(), + Value::Range { val, .. } => { + format!( + "{}..{}", + val.from.into_string(", ", config), + val.to.into_string(", ", config) + ) + } + Value::String { val, .. } => val, + Value::List { vals: val, .. } => format!("[list {} items]", val.len()), + Value::Record { cols, .. } => format!("{{record {} fields}}", cols.len()), + Value::Block { val, .. } => format!("", val), + Value::Nothing { .. } => String::new(), + Value::Error { error } => format!("{:?}", error), + Value::Binary { val, .. } => format!("{:?}", val), + Value::CellPath { val, .. } => val.into_string(), + Value::CustomValue { val, .. } => val.value_string(), + } + } + /// Convert Value into a debug string pub fn debug_value(self) -> String { format!("{:#?}", self)