From f589d3c795de3c7805bc3d842ba6b57b54ba11b4 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 3 Nov 2019 07:49:28 +1300 Subject: [PATCH 1/2] Fix 907 and improve substring --- src/commands/get.rs | 15 +++------------ src/plugins/str.rs | 10 +++++++--- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/commands/get.rs b/src/commands/get.rs index cda637495e..08474ac2c1 100644 --- a/src/commands/get.rs +++ b/src/commands/get.rs @@ -10,7 +10,6 @@ pub struct Get; #[derive(Deserialize)] pub struct GetArgs { member: ColumnPath, - rest: Vec, } impl WholeStreamCommand for Get { @@ -118,13 +117,10 @@ pub fn get_column_path( } pub fn get( - GetArgs { - member, - rest: fields, - }: GetArgs, + GetArgs { member }: GetArgs, RunnableContext { input, .. }: RunnableContext, ) -> Result { - trace!("get {:?} {:?}", member, fields); + trace!("get {:?}", member); let stream = input .values @@ -133,12 +129,7 @@ pub fn get( let member = vec![member.clone()]; - let column_paths = vec![&member, &fields] - .into_iter() - .flatten() - .collect::>(); - - for path in column_paths { + for path in member { let res = get_column_path(&path, &item); match res { diff --git a/src/plugins/str.rs b/src/plugins/str.rs index cab9c6a96a..7fb694d3a5 100644 --- a/src/plugins/str.rs +++ b/src/plugins/str.rs @@ -41,9 +41,13 @@ impl Str { if start > input.len() - 1 { Value::string("") } else { - // Index operator isn't perfect: - // https://users.rust-lang.org/t/how-to-get-a-substring-of-a-string/1351 - Value::string(&input[start..end]) + Value::string( + &input + .chars() + .skip(start) + .take(end - start) + .collect::(), + ) } } Some(Action::ToInteger) => match input.trim() { From cd30fac050220e84bfb36e61decb4f509ffa8472 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 3 Nov 2019 08:57:28 +1300 Subject: [PATCH 2/2] Approach fix differently --- src/commands/get.rs | 15 ++++++-- src/format/table.rs | 87 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 24 deletions(-) diff --git a/src/commands/get.rs b/src/commands/get.rs index 08474ac2c1..cda637495e 100644 --- a/src/commands/get.rs +++ b/src/commands/get.rs @@ -10,6 +10,7 @@ pub struct Get; #[derive(Deserialize)] pub struct GetArgs { member: ColumnPath, + rest: Vec, } impl WholeStreamCommand for Get { @@ -117,10 +118,13 @@ pub fn get_column_path( } pub fn get( - GetArgs { member }: GetArgs, + GetArgs { + member, + rest: fields, + }: GetArgs, RunnableContext { input, .. }: RunnableContext, ) -> Result { - trace!("get {:?}", member); + trace!("get {:?} {:?}", member, fields); let stream = input .values @@ -129,7 +133,12 @@ pub fn get( let member = vec![member.clone()]; - for path in member { + let column_paths = vec![&member, &fields] + .into_iter() + .flatten() + .collect::>(); + + for path in column_paths { let res = get_column_path(&path, &item); match res { diff --git a/src/format/table.rs b/src/format/table.rs index a59e1adafb..3ed5937b4b 100644 --- a/src/format/table.rs +++ b/src/format/table.rs @@ -23,11 +23,20 @@ enum TableMode { impl TableView { fn merge_descriptors(values: &[Tagged]) -> Vec { - let mut ret = vec![]; + let mut ret: Vec = vec![]; + let value_column = "".to_string(); for value in values { - for desc in value.data_descriptors() { - if !ret.contains(&desc) { - ret.push(desc); + let descs = value.data_descriptors(); + + if descs.len() == 0 { + if !ret.contains(&value_column) { + ret.push("".to_string()); + } + } else { + for desc in value.data_descriptors() { + if !ret.contains(&desc) { + ret.push(desc); + } } } } @@ -48,23 +57,59 @@ impl TableView { let mut entries = vec![]; for (idx, value) in values.iter().enumerate() { - let mut row: Vec<(String, &'static str)> = match value { - Tagged { - item: Value::Row(..), - .. - } => headers - .iter() - .enumerate() - .map(|(i, d)| { - let data = value.get_data(d); - return ( - data.borrow().format_leaf(Some(&headers[i])), - data.borrow().style_leaf(), - ); - }) - .collect(), - x => vec![(x.format_leaf(None), x.style_leaf())], - }; + // let mut row: Vec<(String, &'static str)> = match value { + // Tagged { + // item: Value::Row(..), + // .. + // } => headers + // .iter() + // .enumerate() + // .map(|(i, d)| { + // let data = value.get_data(d); + // return ( + // data.borrow().format_leaf(Some(&headers[i])), + // data.borrow().style_leaf(), + // ); + // }) + // .collect(), + // x => vec![(x.format_leaf(None), x.style_leaf())], + // }; + + let mut row: Vec<(String, &'static str)> = headers + .iter() + .enumerate() + .map(|(i, d)| { + if d == "" { + match value { + Tagged { + item: Value::Row(..), + .. + } => ( + Value::nothing().format_leaf(None), + Value::nothing().style_leaf(), + ), + _ => (value.format_leaf(None), value.style_leaf()), + } + } else { + match value { + Tagged { + item: Value::Row(..), + .. + } => { + let data = value.get_data(d); + ( + data.borrow().format_leaf(Some(&headers[i])), + data.borrow().style_leaf(), + ) + } + _ => ( + Value::nothing().format_leaf(None), + Value::nothing().style_leaf(), + ), + } + } + }) + .collect(); if values.len() > 1 { // Indices are black, bold, right-aligned: