some cleanup, extra_usage

This commit is contained in:
Darren Schroeder 2021-10-08 08:14:32 -05:00
parent c4977ae143
commit c4dabe8327
2 changed files with 24 additions and 25 deletions

View file

@ -15,7 +15,7 @@ impl Command for Griddle {
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
"Render the grid." "Renders the output to a textual terminal grid."
} }
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
@ -27,6 +27,15 @@ impl Command for Griddle {
) )
} }
fn extra_usage(&self) -> &str {
r#"grid was built to give a concise gridded layout for ls. however,
it determines what to put in the grid by looking for a column named
'name'. this works great for tables and records but for lists we
need to do something different. such as with '[one two three] | grid'
it creates a fake column called 'name' for these values so that it
prints out the list properly."#
}
fn run( fn run(
&self, &self,
context: &EvaluationContext, context: &EvaluationContext,
@ -57,21 +66,11 @@ impl Command for Griddle {
} }
Value::Record { cols, vals, .. } => { Value::Record { cols, vals, .. } => {
// dbg!("value::record"); // dbg!("value::record");
// let mut items = vec![];
// for (c, v) in cols.into_iter().zip(vals.into_iter()) {
// items.push(vec![c, v.into_string()])
// }
// dbg!(&items);
// Ok(create_grid_output(items, call, columns_param))
let mut items = vec![]; let mut items = vec![];
for (i, (c, v)) in cols.into_iter().zip(vals.into_iter()).enumerate() { for (i, (c, v)) in cols.into_iter().zip(vals.into_iter()).enumerate() {
items.push((i, c, v.into_string())) items.push((i, c, v.into_string()))
} }
// dbg!(&items);
Ok(create_grid_output2(items, call, columns_param)) Ok(create_grid_output2(items, call, columns_param))
} }
@ -213,8 +212,6 @@ fn convert_to_list2(iter: impl IntoIterator<Item = Value>) -> Option<Vec<(usize,
// let h: Vec<String> = headers.into_iter().map(|x| x.trim().to_string()).collect(); // let h: Vec<String> = headers.into_iter().map(|x| x.trim().to_string()).collect();
// let d: Vec<Vec<String>> = data.into_iter().map(|x| x.into_iter().collect()).collect(); // let d: Vec<Vec<String>> = data.into_iter().map(|x| x.into_iter().collect()).collect();
// dbg!(&headers);
// dbg!(&data);
let mut h: Vec<String> = headers.into_iter().collect(); let mut h: Vec<String> = headers.into_iter().collect();
// let d: Vec<Vec<String>> = data.into_iter().collect(); // let d: Vec<Vec<String>> = data.into_iter().collect();
@ -224,26 +221,22 @@ fn convert_to_list2(iter: impl IntoIterator<Item = Value>) -> Option<Vec<(usize,
h.push("#".to_string()); h.push("#".to_string());
h.push("name".to_string()); h.push("name".to_string());
} }
// dbg!(&h);
// dbg!(&d);
// this tuple is (row_index, header_name, value) // this tuple is (row_index, header_name, value)
let mut interleaved = vec![]; let mut interleaved = vec![];
for (i, v) in data.into_iter().enumerate() { for (i, v) in data.into_iter().enumerate() {
for (n, s) in v.into_iter().enumerate() { for (n, s) in v.into_iter().enumerate() {
// dbg!(n);
// dbg!(&s);
// dbg!(&h.len());
if h.len() == 1 { if h.len() == 1 {
// always get the first element since this is a simple list // always get the 1th element since this is a simple list
// and we hacked the header above because it was empty // and we hacked the header above because it was empty
// 0th element is an index, 1th element is the value
interleaved.push((i, h[1].clone(), s)) interleaved.push((i, h[1].clone(), s))
} else { } else {
interleaved.push((i, h[n].clone(), s)) interleaved.push((i, h[n].clone(), s))
} }
} }
} }
// dbg!(&interleaved);
Some(interleaved) Some(interleaved)
} else { } else {
None None

View file

@ -1,9 +1,9 @@
use std::collections::HashMap;
use nu_protocol::ast::{Call, PathMember}; use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EvaluationContext}; use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, Span, Value}; use nu_protocol::{Signature, Span, Value};
use nu_table::StyledString; use nu_table::StyledString;
use std::collections::HashMap;
use terminal_size::{Height, Width};
pub struct Table; pub struct Table;
@ -27,12 +27,18 @@ impl Command for Table {
call: &Call, call: &Call,
input: Value, input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> { ) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
w as usize
} else {
80usize
};
match input { match input {
Value::List { vals, .. } => { Value::List { vals, .. } => {
let table = convert_to_table(vals); let table = convert_to_table(vals);
if let Some(table) = table { if let Some(table) = table {
let result = nu_table::draw_table(&table, 80, &HashMap::new()); let result = nu_table::draw_table(&table, term_width, &HashMap::new());
Ok(Value::String { Ok(Value::String {
val: result, val: result,
@ -46,7 +52,7 @@ impl Command for Table {
let table = convert_to_table(stream); let table = convert_to_table(stream);
if let Some(table) = table { if let Some(table) = table {
let result = nu_table::draw_table(&table, 80, &HashMap::new()); let result = nu_table::draw_table(&table, term_width, &HashMap::new());
Ok(Value::String { Ok(Value::String {
val: result, val: result,
@ -78,7 +84,7 @@ impl Command for Table {
theme: nu_table::Theme::rounded(), theme: nu_table::Theme::rounded(),
}; };
let result = nu_table::draw_table(&table, 80, &HashMap::new()); let result = nu_table::draw_table(&table, term_width, &HashMap::new());
Ok(Value::String { Ok(Value::String {
val: result, val: result,