forked from extern/nushell
Create Record
type (#10103)
# Description This PR creates a new `Record` type to reduce duplicate code and possibly bugs as well. (This is an edited version of #9648.) - `Record` implements `FromIterator` and `IntoIterator` and so can be iterated over or collected into. For example, this helps with conversions to and from (hash)maps. (Also, no more `cols.iter().zip(vals)`!) - `Record` has a `push(col, val)` function to help insure that the number of columns is equal to the number of values. I caught a few potential bugs thanks to this (e.g. in the `ls` command). - Finally, this PR also adds a `record!` macro that helps simplify record creation. It is used like so: ```rust record! { "key1" => some_value, "key2" => Value::string("text", span), "key3" => Value::int(optional_int.unwrap_or(0), span), "key4" => Value::bool(config.setting, span), } ``` Since macros hinder formatting, etc., the right hand side values should be relatively short and sweet like the examples above. Where possible, prefer `record!` or `.collect()` on an iterator instead of multiple `Record::push`s, since the first two automatically set the record capacity and do less work overall. # User-Facing Changes Besides the changes in `nu-protocol` the only other breaking changes are to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use nu_ansi_term::*;
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::engine::{EngineState, Stack};
|
||||
use nu_protocol::record;
|
||||
use nu_protocol::{
|
||||
ast::Call, engine::Command, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData,
|
||||
PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
@@ -763,7 +764,7 @@ Operating system commands:
|
||||
attr: None,
|
||||
};
|
||||
// Iterate and populate NuStyle with real values
|
||||
for (k, v) in record.0.iter().zip(record.1) {
|
||||
for (k, v) in record {
|
||||
match k.as_str() {
|
||||
"fg" => nu_style.fg = Some(v.as_string()?),
|
||||
"bg" => nu_style.bg = Some(v.as_string()?),
|
||||
@@ -799,17 +800,7 @@ fn generate_ansi_code_list(
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(move |(i, ansi_code)| {
|
||||
let cols = if use_ansi_coloring {
|
||||
vec![
|
||||
"name".into(),
|
||||
"preview".into(),
|
||||
"short name".into(),
|
||||
"code".into(),
|
||||
]
|
||||
} else {
|
||||
vec!["name".into(), "short name".into(), "code".into()]
|
||||
};
|
||||
let name: Value = Value::string(String::from(ansi_code.long_name), call_span);
|
||||
let name = Value::string(ansi_code.long_name, call_span);
|
||||
let short_name = Value::string(ansi_code.short_name.unwrap_or(""), call_span);
|
||||
// The first 102 items in the ansi array are colors
|
||||
let preview = if i < 389 {
|
||||
@@ -817,18 +808,24 @@ fn generate_ansi_code_list(
|
||||
} else {
|
||||
Value::string("\u{1b}[0m", call_span)
|
||||
};
|
||||
let code_string = String::from(&ansi_code.code.replace('\u{1b}', "\\e"));
|
||||
let code = Value::string(code_string, call_span);
|
||||
let vals = if use_ansi_coloring {
|
||||
vec![name, preview, short_name, code]
|
||||
let code = Value::string(ansi_code.code.replace('\u{1b}', "\\e"), call_span);
|
||||
|
||||
let record = if use_ansi_coloring {
|
||||
record! {
|
||||
"name" => name,
|
||||
"preview" => preview,
|
||||
"short name" => short_name,
|
||||
"code" => code,
|
||||
}
|
||||
} else {
|
||||
vec![name, short_name, code]
|
||||
record! {
|
||||
"name" => name,
|
||||
"short name" => short_name,
|
||||
"code" => code,
|
||||
}
|
||||
};
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: call_span,
|
||||
}
|
||||
|
||||
Value::record(record, call_span)
|
||||
})
|
||||
.into_pipeline_data(engine_state.ctrlc.clone()));
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use filesize::file_real_size_fast;
|
||||
use nu_glob::Pattern;
|
||||
use nu_protocol::{ShellError, Span, Value};
|
||||
use nu_protocol::{record, ShellError, Span, Value};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::Arc;
|
||||
@@ -185,30 +185,6 @@ impl DirInfo {
|
||||
|
||||
impl From<DirInfo> for Value {
|
||||
fn from(d: DirInfo) -> Self {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("path".into());
|
||||
vals.push(Value::string(d.path.display().to_string(), d.tag));
|
||||
|
||||
cols.push("apparent".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: d.size as i64,
|
||||
span: d.tag,
|
||||
});
|
||||
|
||||
cols.push("physical".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: d.blocks as i64,
|
||||
span: d.tag,
|
||||
});
|
||||
|
||||
cols.push("directories".into());
|
||||
vals.push(value_from_vec(d.dirs, d.tag));
|
||||
|
||||
cols.push("files".into());
|
||||
vals.push(value_from_vec(d.files, d.tag));
|
||||
|
||||
// if !d.errors.is_empty() {
|
||||
// let v = d
|
||||
// .errors
|
||||
@@ -223,51 +199,34 @@ impl From<DirInfo> for Value {
|
||||
// })
|
||||
// }
|
||||
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: d.tag,
|
||||
}
|
||||
Value::record(
|
||||
record! {
|
||||
"path" => Value::string(d.path.display().to_string(), d.tag),
|
||||
"apparent" => Value::filesize(d.size as i64, d.tag),
|
||||
"physical" => Value::filesize(d.blocks as i64, d.tag),
|
||||
"directories" => value_from_vec(d.dirs, d.tag),
|
||||
"files" => value_from_vec(d.files, d.tag)
|
||||
},
|
||||
d.tag,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FileInfo> for Value {
|
||||
fn from(f: FileInfo) -> Self {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
|
||||
cols.push("path".into());
|
||||
vals.push(Value::string(f.path.display().to_string(), f.tag));
|
||||
|
||||
cols.push("apparent".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: f.size as i64,
|
||||
span: f.tag,
|
||||
});
|
||||
|
||||
cols.push("physical".into());
|
||||
vals.push(Value::Filesize {
|
||||
val: match f.blocks {
|
||||
Some(b) => b as i64,
|
||||
None => 0i64,
|
||||
},
|
||||
span: f.tag,
|
||||
});
|
||||
|
||||
cols.push("directories".into());
|
||||
vals.push(Value::nothing(Span::unknown()));
|
||||
|
||||
cols.push("files".into());
|
||||
vals.push(Value::nothing(Span::unknown()));
|
||||
|
||||
// cols.push("errors".into());
|
||||
// vals.push(Value::nothing(Span::unknown()));
|
||||
|
||||
Value::Record {
|
||||
cols,
|
||||
vals,
|
||||
span: f.tag,
|
||||
}
|
||||
Value::record(
|
||||
record! {
|
||||
"path" => Value::string(f.path.display().to_string(), f.tag),
|
||||
"apparent" => Value::filesize(f.size as i64, f.tag),
|
||||
"physical" => Value::filesize(f.blocks.unwrap_or(0) as i64, f.tag),
|
||||
"directories" => Value::nothing(Span::unknown()),
|
||||
"files" => Value::nothing(Span::unknown()),
|
||||
},
|
||||
f.tag,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -7,7 +7,8 @@ use nu_engine::CallExt;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||
record, Category, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
|
||||
Type, Value,
|
||||
};
|
||||
use num_traits::AsPrimitive;
|
||||
use std::io::stdout;
|
||||
@@ -273,13 +274,13 @@ fn create_focus_event(
|
||||
event_type: FocusEventType,
|
||||
) -> Option<Value> {
|
||||
if filter.listen_focus {
|
||||
let cols = vec!["type".to_string(), "event".to_string()];
|
||||
let vals = vec![
|
||||
Value::string("focus", head),
|
||||
Value::string(event_type.string(), head),
|
||||
];
|
||||
|
||||
Some(Value::record(cols, vals, head))
|
||||
Some(Value::record(
|
||||
record! {
|
||||
"type" => Value::string("focus", head),
|
||||
"event" => Value::string(event_type.string(), head)
|
||||
},
|
||||
head,
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -308,28 +309,26 @@ fn create_key_event(
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut cols = vec![
|
||||
"type".to_string(),
|
||||
"key_type".to_string(),
|
||||
"code".to_string(),
|
||||
"modifiers".to_string(),
|
||||
];
|
||||
|
||||
let typ = Value::string("key".to_string(), head);
|
||||
let (key, code) = get_keycode_name(head, raw_code);
|
||||
let modifiers = parse_modifiers(head, raw_modifiers);
|
||||
let mut vals = vec![typ, key, code, modifiers];
|
||||
|
||||
let mut record = record! {
|
||||
"type" => Value::string("key", head),
|
||||
"key_type" => key,
|
||||
"code" => code,
|
||||
"modifiers" => parse_modifiers(head, raw_modifiers),
|
||||
};
|
||||
|
||||
if add_raw {
|
||||
if let KeyCode::Char(c) = raw_code {
|
||||
cols.push("raw_code".to_string());
|
||||
vals.push(Value::int(c.as_(), head));
|
||||
record.push("raw_code", Value::int(c.as_(), head));
|
||||
}
|
||||
cols.push("raw_modifiers".to_string());
|
||||
vals.push(Value::int(raw_modifiers.bits() as i64, head));
|
||||
record.push(
|
||||
"raw_modifiers",
|
||||
Value::int(raw_modifiers.bits() as i64, head),
|
||||
);
|
||||
}
|
||||
|
||||
Some(Value::record(cols, vals, head))
|
||||
Some(Value::record(record, head))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -373,18 +372,6 @@ fn create_mouse_event(
|
||||
add_raw: bool,
|
||||
) -> Option<Value> {
|
||||
if filter.listen_mouse {
|
||||
let mut cols = vec![
|
||||
"type".to_string(),
|
||||
"col".to_string(),
|
||||
"row".to_string(),
|
||||
"kind".to_string(),
|
||||
"modifiers".to_string(),
|
||||
];
|
||||
|
||||
let typ = Value::string("mouse".to_string(), head);
|
||||
let col = Value::int(event.column as i64, head);
|
||||
let row = Value::int(event.row as i64, head);
|
||||
|
||||
let kind = match event.kind {
|
||||
MouseEventKind::Down(btn) => format!("{btn:?}_down"),
|
||||
MouseEventKind::Up(btn) => format!("{btn:?}_up"),
|
||||
@@ -393,17 +380,23 @@ fn create_mouse_event(
|
||||
MouseEventKind::ScrollDown => "scroll_down".to_string(),
|
||||
MouseEventKind::ScrollUp => "scroll_up".to_string(),
|
||||
};
|
||||
let kind = Value::string(kind, head);
|
||||
let modifiers = parse_modifiers(head, &event.modifiers);
|
||||
|
||||
let mut vals = vec![typ, col, row, kind, modifiers];
|
||||
let mut record = record! {
|
||||
"type" => Value::string("mouse", head),
|
||||
"col" => Value::int(event.column as i64, head),
|
||||
"row" => Value::int(event.row as i64, head),
|
||||
"kind" => Value::string(kind, head),
|
||||
"modifiers" => parse_modifiers(head, &event.modifiers),
|
||||
};
|
||||
|
||||
if add_raw {
|
||||
cols.push("raw_modifiers".to_string());
|
||||
vals.push(Value::int(event.modifiers.bits() as i64, head));
|
||||
record.push(
|
||||
"raw_modifiers",
|
||||
Value::int(event.modifiers.bits() as i64, head),
|
||||
);
|
||||
}
|
||||
|
||||
Some(Value::record(cols, vals, head))
|
||||
Some(Value::record(record, head))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -411,10 +404,12 @@ fn create_mouse_event(
|
||||
|
||||
fn create_paste_event(head: Span, filter: &EventTypeFilter, content: &str) -> Option<Value> {
|
||||
if filter.listen_paste {
|
||||
let cols = vec!["type".to_string(), "content".to_string()];
|
||||
let vals = vec![Value::string("paste", head), Value::string(content, head)];
|
||||
let record = record! {
|
||||
"type" => Value::string("paste", head),
|
||||
"content" => Value::string(content, head),
|
||||
};
|
||||
|
||||
Some(Value::record(cols, vals, head))
|
||||
Some(Value::record(record, head))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -427,14 +422,13 @@ fn create_resize_event(
|
||||
rows: u16,
|
||||
) -> Option<Value> {
|
||||
if filter.listen_resize {
|
||||
let cols = vec!["type".to_string(), "col".to_string(), "row".to_string()];
|
||||
let vals = vec![
|
||||
Value::string("resize", head),
|
||||
Value::int(columns as i64, head),
|
||||
Value::int(rows as i64, head),
|
||||
];
|
||||
let record = record! {
|
||||
"type" => Value::string("resize", head),
|
||||
"col" => Value::int(columns as i64, head),
|
||||
"row" => Value::int(rows as i64, head),
|
||||
};
|
||||
|
||||
Some(Value::record(cols, vals, head))
|
||||
Some(Value::record(record, head))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@@ -88,8 +88,8 @@ impl Command for InputList {
|
||||
let rows = input.into_iter().collect::<Vec<_>>();
|
||||
rows.iter().for_each(|row| {
|
||||
if let Ok(record) = row.as_record() {
|
||||
let columns = record.1.len();
|
||||
for (i, (col, val)) in record.0.iter().zip(record.1.iter()).enumerate() {
|
||||
let columns = record.len();
|
||||
for (i, (col, val)) in record.iter().enumerate() {
|
||||
if i == columns - 1 {
|
||||
break;
|
||||
}
|
||||
@@ -116,9 +116,8 @@ impl Command for InputList {
|
||||
})
|
||||
} else if let Ok(record) = x.as_record() {
|
||||
let mut options = Vec::new();
|
||||
let columns = record.1.len();
|
||||
for (i, (col, val)) in record.0.iter().zip(record.1.iter()).enumerate()
|
||||
{
|
||||
let columns = record.len();
|
||||
for (i, (col, val)) in record.iter().enumerate() {
|
||||
if let Ok(val) = val.as_string() {
|
||||
let len = nu_utils::strip_ansi_likely(&val).len()
|
||||
+ nu_utils::strip_ansi_likely(col).len();
|
||||
|
@@ -1,7 +1,7 @@
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
||||
record, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Type, Value,
|
||||
};
|
||||
use terminal_size::{terminal_size, Height, Width};
|
||||
|
||||
@@ -63,14 +63,13 @@ impl Command for TermSize {
|
||||
None => (Width(0), Height(0)),
|
||||
};
|
||||
|
||||
Ok(Value::Record {
|
||||
cols: vec!["columns".into(), "rows".into()],
|
||||
vals: vec![
|
||||
Value::int(cols.0 as i64, head),
|
||||
Value::int(rows.0 as i64, head),
|
||||
],
|
||||
span: head,
|
||||
}
|
||||
Ok(Value::record(
|
||||
record! {
|
||||
"columns" => Value::int(cols.0 as i64, head),
|
||||
"rows" => Value::int(rows.0 as i64, head),
|
||||
},
|
||||
head,
|
||||
)
|
||||
.into_pipeline_data())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user