forked from extern/nushell
Merge pull request #77 from nushell/record_view
add a vertical record view
This commit is contained in:
commit
000db46618
@ -28,7 +28,7 @@ impl Command for FromJson {
|
||||
input: Value,
|
||||
) -> Result<nu_protocol::Value, ShellError> {
|
||||
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
|
||||
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
@ -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::<Vec<String>>()
|
||||
.join(", "),
|
||||
Value::String { val, .. } => val,
|
||||
Value::Stream { stream, .. } => stream.collect_string(),
|
||||
Value::List { vals: val, .. } => val
|
||||
.into_iter()
|
||||
.map(|x| x.collect_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
Value::Record { vals, .. } => vals
|
||||
.into_iter()
|
||||
.map(|y| y.collect_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n"),
|
||||
Value::Block { val, .. } => format!("<Block {}>", 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 {
|
||||
|
@ -16,6 +16,12 @@ impl ValueStream {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn collect_string(self) -> String {
|
||||
self.map(|x: Value| x.collect_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
pub fn from_stream(input: impl Iterator<Item = Value> + 'static) -> ValueStream {
|
||||
ValueStream(Rc::new(RefCell::new(input)))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user