forked from extern/nushell
Merge pull request #281 from jonathandturner/improve_arrays
Add from_array and improve array viewing
This commit is contained in:
commit
4411a5b72c
@ -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 |
|
||||
|
@ -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)),
|
||||
|
@ -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
14
src/commands/debug.rs
Normal 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())
|
||||
}
|
21
src/commands/from_array.rs
Normal file
21
src/commands/from_array.rs
Normal 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())
|
||||
}
|
@ -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())));
|
||||
|
Loading…
Reference in New Issue
Block a user