Merge pull request #281 from jonathandturner/improve_arrays

Add from_array and improve array viewing
This commit is contained in:
Jonathan Turner 2019-08-12 18:16:08 +12:00 committed by GitHub
commit 4411a5b72c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 5 deletions

View File

@ -158,6 +158,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
| nth row-number | Return only the selected row |
| str (field) | Apply string function. Optional use the field of a table |
| tags | Read the tags (metadata) for values |
| from-array | Expand an array/list into rows |
| to-array | Collapse rows into a single list |
| to-json | Convert table into .json text |
| to-toml | Convert table into .toml text |

View File

@ -147,6 +147,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
context.add_commands(vec![
command("first", Box::new(first::first)),
command("pick", Box::new(pick::pick)),
command("from-array", Box::new(from_array::from_array)),
command("from-ini", Box::new(from_ini::from_ini)),
command("from-csv", Box::new(from_csv::from_csv)),
command("from-json", Box::new(from_json::from_json)),
@ -161,6 +162,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
command("nth", Box::new(nth::nth)),
command("n", Box::new(next::next)),
command("p", Box::new(prev::prev)),
command("debug", Box::new(debug::debug)),
command("lines", Box::new(lines::lines)),
command("pick", Box::new(pick::pick)),
command("shells", Box::new(shells::shells)),

View File

@ -10,9 +10,11 @@ crate mod command;
crate mod config;
crate mod cp;
crate mod date;
crate mod debug;
crate mod enter;
crate mod exit;
crate mod first;
crate mod from_array;
crate mod from_csv;
crate mod from_ini;
crate mod from_json;

14
src/commands/debug.rs Normal file
View File

@ -0,0 +1,14 @@
use crate::errors::ShellError;
use crate::prelude::*;
pub fn debug(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let input = args.input;
Ok(input
.values
.map(|v| {
println!("{:?}", v);
ReturnSuccess::value(v)
})
.to_output_stream())
}

View File

@ -0,0 +1,21 @@
use crate::object::Value;
use crate::prelude::*;
pub fn from_array(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let stream = args
.input
.values
.map(|item| match item {
Tagged {
item: Value::List(vec),
..
} => VecDeque::from(vec),
x => VecDeque::from(vec![x]),
})
.flatten();
Ok(stream.to_output_stream())
}

View File

@ -40,11 +40,17 @@ impl TableView {
let mut entries = vec![];
for (idx, value) in values.iter().enumerate() {
let mut row: Vec<String> = headers
.iter()
.enumerate()
.map(|(i, d)| value.get_data(d).borrow().format_leaf(Some(&headers[i])))
.collect();
let mut row: Vec<String> = match value {
Tagged {
item: Value::Object(..),
..
} => headers
.iter()
.enumerate()
.map(|(i, d)| value.get_data(d).borrow().format_leaf(Some(&headers[i])))
.collect(),
x => vec![x.format_leaf(None)],
};
if values.len() > 1 {
row.insert(0, format!("{}", Color::Black.bold().paint(idx.to_string())));