From d6e24cceb42754e220f9ad5ee22d02e08133af44 Mon Sep 17 00:00:00 2001 From: JT Date: Fri, 1 Oct 2021 19:01:22 +1300 Subject: [PATCH] add a vertical record view --- crates/nu-command/src/formats/from/json.rs | 2 +- crates/nu-command/src/viewers/table.rs | 29 ++++++++++++++++++++++ crates/nu-protocol/src/value/mod.rs | 29 ++++++++++++++++++++++ crates/nu-protocol/src/value/stream.rs | 6 +++++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/formats/from/json.rs b/crates/nu-command/src/formats/from/json.rs index a7e01036aa..fa206131f7 100644 --- a/crates/nu-command/src/formats/from/json.rs +++ b/crates/nu-command/src/formats/from/json.rs @@ -28,7 +28,7 @@ impl Command for FromJson { input: Value, ) -> Result { let span = input.span(); - let mut string_input = input.into_string(); + let mut string_input = input.collect_string(); string_input.push('\n'); // TODO: turn this into a structured underline of the nu_json error diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index d1c7353d15..2a43bcee26 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -56,6 +56,35 @@ impl Command for Table { Ok(Value::Nothing { span: call.head }) } } + Value::Record { cols, vals, .. } => { + let mut output = vec![]; + + for (c, v) in cols.into_iter().zip(vals.into_iter()) { + output.push(vec![ + StyledString { + contents: c, + style: nu_table::TextStyle::default_header(), + }, + StyledString { + contents: v.into_string(), + style: nu_table::TextStyle::default(), + }, + ]) + } + + let table = nu_table::Table { + headers: vec![], + data: output, + theme: nu_table::Theme::rounded(), + }; + + let result = nu_table::draw_table(&table, 80, &HashMap::new()); + + Ok(Value::String { + val: result, + span: call.head, + }) + } x => Ok(x), } } diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index 76f5ab35ae..5a3c0bdb0f 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -171,6 +171,35 @@ impl Value { } } + pub fn collect_string(self) -> String { + match self { + Value::Bool { val, .. } => val.to_string(), + Value::Int { val, .. } => val.to_string(), + Value::Float { val, .. } => val.to_string(), + Value::Range { val, .. } => val + .into_iter() + .map(|x| x.into_string()) + .collect::>() + .join(", "), + Value::String { val, .. } => val, + Value::Stream { stream, .. } => stream.collect_string(), + Value::List { vals: val, .. } => val + .into_iter() + .map(|x| x.collect_string()) + .collect::>() + .join("\n"), + Value::Record { vals, .. } => vals + .into_iter() + .map(|y| y.collect_string()) + .collect::>() + .join("\n"), + Value::Block { val, .. } => format!("", val), + Value::Nothing { .. } => String::new(), + Value::Error { error } => format!("{:?}", error), + Value::Binary { val, .. } => format!("{:?}", val), + } + } + /// Create a new `Nothing` value pub fn nothing() -> Value { Value::Nothing { diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index f42349bf94..4ac557247f 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -16,6 +16,12 @@ impl ValueStream { ) } + pub fn collect_string(self) -> String { + self.map(|x: Value| x.collect_string()) + .collect::>() + .join("\n") + } + pub fn from_stream(input: impl Iterator + 'static) -> ValueStream { ValueStream(Rc::new(RefCell::new(input))) }