Add a config variable with engine support (#332)

* Add a config variable with engine support

* Add a config variable with engine support

* Oops, cleanup
This commit is contained in:
JT
2021-11-15 08:25:57 +13:00
committed by GitHub
parent e76451866d
commit 0f107b2830
30 changed files with 333 additions and 96 deletions

View File

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Config, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
// TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml)
@ -136,6 +136,7 @@ fn string_helper(
let head = call.head;
let decimals_value: Option<i64> = call.get_flag(engine_state, stack, "decimals")?;
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
let config = stack.get_config()?;
if decimals && decimals_value.is_some() && decimals_value.unwrap().is_negative() {
return Err(ShellError::UnsupportedInput(
@ -147,13 +148,16 @@ fn string_helper(
input.map(
move |v| {
if column_paths.is_empty() {
action(&v, head, decimals, decimals_value, false)
action(&v, head, decimals, decimals_value, false, &config)
} else {
let mut ret = v;
for path in &column_paths {
let config = config.clone();
let r = ret.update_cell_path(
&path.members,
Box::new(move |old| action(old, head, decimals, decimals_value, false)),
Box::new(move |old| {
action(old, head, decimals, decimals_value, false, &config)
}),
);
if let Err(error) = r {
return Value::Error { error };
@ -173,6 +177,7 @@ pub fn action(
decimals: bool,
digits: Option<i64>,
group_digits: bool,
config: &Config,
) -> Value {
match input {
Value::Int { val, .. } => {
@ -212,7 +217,7 @@ pub fn action(
},
Value::Filesize { val: _, .. } => Value::String {
val: input.clone().into_string(", "),
val: input.clone().into_string(", ", config),
span,
},
Value::Nothing { .. } => Value::String {

View File

@ -2,7 +2,7 @@ use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::{
engine::{Command, EngineState, Stack, StateWorkingSet},
PipelineData, Span,
PipelineData, Span, Value, CONFIG_VARIABLE_ID,
};
use crate::To;
@ -57,6 +57,16 @@ pub fn test_examples(cmd: impl Command + 'static) {
let mut stack = Stack::new();
// Set up our initial config to start from
stack.vars.insert(
CONFIG_VARIABLE_ID,
Value::Record {
cols: vec![],
vals: vec![],
span: Span::unknown(),
},
);
match eval_block(
&engine_state,
&mut stack,

View File

@ -78,6 +78,7 @@ fn from_csv(
let noheaders = call.has_flag("noheaders");
let separator: Option<Value> = call.get_flag(engine_state, stack, "separator")?;
let config = stack.get_config()?;
let sep = match separator {
Some(Value::String { val: s, span }) => {
@ -97,7 +98,7 @@ fn from_csv(
_ => ',',
};
from_delimited_data(noheaders, sep, input, name)
from_delimited_data(noheaders, sep, input, name, &config)
}
#[cfg(test)]

View File

@ -1,5 +1,5 @@
use csv::ReaderBuilder;
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Span, Value};
use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Span, Value};
fn from_delimited_string_to_value(
s: String,
@ -50,8 +50,9 @@ pub fn from_delimited_data(
sep: char,
input: PipelineData,
name: Span,
config: &Config,
) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("");
let concat_string = input.collect_string("", config);
Ok(
from_delimited_string_to_value(concat_string, noheaders, sep, name)

View File

@ -4,6 +4,7 @@ use indexmap::map::IndexMap;
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Config;
use nu_protocol::{
Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
@ -41,7 +42,8 @@ impl Command for FromEml {
let head = call.head;
let preview_body: Option<Spanned<i64>> =
call.get_flag(engine_state, stack, "preview-body")?;
from_eml(input, preview_body, head)
let config = stack.get_config()?;
from_eml(input, preview_body, head, &config)
}
fn examples(&self) -> Vec<Example> {
@ -176,8 +178,9 @@ fn from_eml(
input: PipelineData,
preview_body: Option<Spanned<i64>>,
head: Span,
config: &Config,
) -> Result<PipelineData, ShellError> {
let value = input.collect_string("");
let value = input.collect_string("", config);
let body_preview = preview_body
.map(|b| b.item as usize)

View File

@ -72,12 +72,13 @@ impl Command for FromJson {
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let span = call.head;
let mut string_input = input.collect_string("");
let config = stack.get_config()?;
let mut string_input = input.collect_string("", &config);
string_input.push('\n');
// TODO: turn this into a structured underline of the nu_json error

View File

@ -2,7 +2,7 @@ use super::delimited::from_delimited_data;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{PipelineData, ShellError, Signature};
use nu_protocol::{Config, PipelineData, ShellError, Signature};
#[derive(Clone)]
pub struct FromTsv;
@ -27,20 +27,21 @@ impl Command for FromTsv {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
from_tsv(call, input)
let config = stack.get_config()?;
from_tsv(call, input, &config)
}
}
fn from_tsv(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
fn from_tsv(call: &Call, input: PipelineData, config: &Config) -> Result<PipelineData, ShellError> {
let name = call.head;
let noheaders = call.has_flag("noheaders");
from_delimited_data(noheaders, '\t', input, name)
from_delimited_data(noheaders, '\t', input, name, config)
}
#[cfg(test)]

View File

@ -1,6 +1,6 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
use nu_protocol::{Config, Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct FromUrl;
@ -21,12 +21,13 @@ impl Command for FromUrl {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let head = call.head;
from_url(input, head)
let config = stack.get_config()?;
from_url(input, head, &config)
}
fn examples(&self) -> Vec<Example> {
@ -52,8 +53,8 @@ impl Command for FromUrl {
}
}
fn from_url(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("");
fn from_url(input: PipelineData, head: Span, config: &Config) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("", config);
let result = serde_urlencoded::from_str::<Vec<(String, String)>>(&concat_string);

View File

@ -2,7 +2,7 @@ use itertools::Itertools;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Value,
Config, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned, Value,
};
use serde::de::Deserialize;
use std::collections::HashMap;
@ -65,12 +65,13 @@ impl Command for FromYaml {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let head = call.head;
from_yaml(input, head)
let config = stack.get_config()?;
from_yaml(input, head, &config)
}
}
@ -93,12 +94,13 @@ impl Command for FromYml {
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let head = call.head;
from_yaml(input, head)
let config = stack.get_config()?;
from_yaml(input, head, &config)
}
}
@ -202,8 +204,8 @@ pub fn from_yaml_string_to_value(s: String, span: Span) -> Result<Value, ShellEr
}
}
fn from_yaml(input: PipelineData, head: Span) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("");
fn from_yaml(input: PipelineData, head: Span, config: &Config) -> Result<PipelineData, ShellError> {
let concat_string = input.collect_string("", config);
match from_yaml_string_to_value(concat_string, head) {
Ok(x) => Ok(x.into_pipeline_data()),
@ -248,6 +250,7 @@ mod test {
}),
},
];
let config = Config::default();
for tc in tt {
let actual = from_yaml_string_to_value(tc.input.to_owned(), Span::unknown());
if actual.is_err() {
@ -259,8 +262,8 @@ mod test {
);
} else {
assert_eq!(
actual.unwrap().into_string(""),
tc.expected.unwrap().into_string("")
actual.unwrap().into_string("", &config),
tc.expected.unwrap().into_string("", &config)
);
}
}

View File

@ -49,10 +49,13 @@ impl Command for BuildString {
call: &Call,
_input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let config = stack.get_config()?;
let output = call
.positional
.iter()
.map(|expr| eval_expression(engine_state, stack, expr).map(|val| val.into_string(", ")))
.map(|expr| {
eval_expression(engine_state, stack, expr).map(|val| val.into_string(", ", &config))
})
.collect::<Result<Vec<String>, ShellError>>()?;
Ok(Value::String {

View File

@ -34,12 +34,14 @@ impl Command for StrCollect {
) -> Result<PipelineData, ShellError> {
let separator: Option<String> = call.opt(engine_state, stack, 0)?;
let config = stack.get_config()?;
// Hmm, not sure what we actually want. If you don't use debug_string, Date comes out as human readable
// which feels funny
#[allow(clippy::needless_collect)]
let strings: Vec<String> = input
.into_iter()
.map(|value| value.debug_string("\n"))
.map(|value| value.debug_string("\n", &config))
.collect();
let output = if let Some(separator) = separator {

View File

@ -8,7 +8,7 @@ use std::sync::mpsc;
use nu_protocol::engine::{EngineState, Stack};
use nu_protocol::{ast::Call, engine::Command, ShellError, Signature, SyntaxShape, Value};
use nu_protocol::{IntoInterruptiblePipelineData, PipelineData, Span, Spanned};
use nu_protocol::{Config, IntoInterruptiblePipelineData, PipelineData, Span, Spanned};
use nu_engine::CallExt;
@ -44,13 +44,15 @@ impl Command for External {
let last_expression = call.has_flag("last_expression");
let env_vars = stack.get_env_vars();
let config = stack.get_config()?;
let command = ExternalCommand {
name,
args,
last_expression,
env_vars,
};
command.run_with_input(engine_state, input)
command.run_with_input(engine_state, input, config)
}
}
@ -66,6 +68,7 @@ impl ExternalCommand {
&self,
engine_state: &EngineState,
input: PipelineData,
config: Config,
) -> Result<PipelineData, ShellError> {
let mut process = self.create_command();
@ -112,7 +115,10 @@ impl ExternalCommand {
}
}
x => {
if stdin_write.write(x.into_string(", ").as_bytes()).is_err() {
if stdin_write
.write(x.into_string(", ", &config).as_bytes())
.is_err()
{
return Err(());
}
}

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, PathMember},
engine::{Command, EngineState, Stack},
IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value,
Config, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value,
};
use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::{Height, Width};
@ -57,10 +57,12 @@ prints out the list properly."#
let color_param: bool = call.has_flag("color");
let separator_param: Option<String> = call.get_flag(engine_state, stack, "separator")?;
let config = stack.get_config()?;
match input {
PipelineData::Value(Value::List { vals, .. }) => {
// dbg!("value::list");
let data = convert_to_list2(vals);
let data = convert_to_list2(vals, &config);
if let Some(items) = data {
Ok(create_grid_output2(
items,
@ -75,7 +77,7 @@ prints out the list properly."#
}
PipelineData::Stream(stream) => {
// dbg!("value::stream");
let data = convert_to_list2(stream);
let data = convert_to_list2(stream, &config);
if let Some(items) = data {
Ok(create_grid_output2(
items,
@ -94,7 +96,7 @@ prints out the list properly."#
let mut items = vec![];
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(", ", &config)))
}
Ok(create_grid_output2(
@ -171,7 +173,10 @@ fn create_grid_output2(
.into_pipeline_data()
}
fn convert_to_list2(iter: impl IntoIterator<Item = Value>) -> Option<Vec<(usize, String, String)>> {
fn convert_to_list2(
iter: impl IntoIterator<Item = Value>,
config: &Config,
) -> Option<Vec<(usize, String, String)>> {
let mut iter = iter.into_iter().peekable();
if let Some(first) = iter.peek() {
@ -187,7 +192,7 @@ fn convert_to_list2(iter: impl IntoIterator<Item = Value>) -> Option<Vec<(usize,
let mut row = vec![row_num.to_string()];
if headers.is_empty() {
row.push(item.into_string(", "))
row.push(item.into_string(", ", config))
} else {
for header in headers.iter().skip(1) {
let result = match item {
@ -201,7 +206,7 @@ fn convert_to_list2(iter: impl IntoIterator<Item = Value>) -> Option<Vec<(usize,
};
match result {
Ok(value) => row.push(value.into_string(", ")),
Ok(value) => row.push(value.into_string(", ", config)),
Err(_) => row.push(String::new()),
}
}

View File

@ -1,7 +1,7 @@
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, Span, Value};
use nu_table::StyledString;
use nu_protocol::{Config, IntoPipelineData, PipelineData, ShellError, Signature, Span, Value};
use nu_table::{StyledString, Theme};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
@ -27,11 +27,12 @@ impl Command for Table {
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let ctrlc = engine_state.ctrlc.clone();
let config = stack.get_config()?;
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
w as usize
@ -41,7 +42,7 @@ impl Command for Table {
match input {
PipelineData::Value(Value::List { vals, .. }) => {
let table = convert_to_table(vals, ctrlc)?;
let table = convert_to_table(vals, ctrlc, &config)?;
if let Some(table) = table {
let result = nu_table::draw_table(&table, term_width, &HashMap::new());
@ -56,7 +57,7 @@ impl Command for Table {
}
}
PipelineData::Stream(stream) => {
let table = convert_to_table(stream, ctrlc)?;
let table = convert_to_table(stream, ctrlc, &config)?;
if let Some(table) = table {
let result = nu_table::draw_table(&table, term_width, &HashMap::new());
@ -80,7 +81,7 @@ impl Command for Table {
style: nu_table::TextStyle::default_field(),
},
StyledString {
contents: v.into_string(", "),
contents: v.into_string(", ", &config),
style: nu_table::TextStyle::default(),
},
])
@ -89,7 +90,7 @@ impl Command for Table {
let table = nu_table::Table {
headers: vec![],
data: output,
theme: nu_table::Theme::rounded(),
theme: load_theme_from_config(&config),
};
let result = nu_table::draw_table(&table, term_width, &HashMap::new());
@ -109,6 +110,7 @@ impl Command for Table {
fn convert_to_table(
iter: impl IntoIterator<Item = Value>,
ctrlc: Option<Arc<AtomicBool>>,
config: &Config,
) -> Result<Option<nu_table::Table>, ShellError> {
let mut iter = iter.into_iter().peekable();
@ -133,7 +135,7 @@ fn convert_to_table(
let mut row = vec![row_num.to_string()];
if headers.is_empty() {
row.push(item.into_string(", "))
row.push(item.into_string(", ", config))
} else {
for header in headers.iter().skip(1) {
let result = match item {
@ -147,7 +149,7 @@ fn convert_to_table(
};
match result {
Ok(value) => row.push(value.into_string(", ")),
Ok(value) => row.push(value.into_string(", ", config)),
Err(_) => row.push(String::new()),
}
}
@ -185,9 +187,24 @@ fn convert_to_table(
.collect::<Vec<StyledString>>()
})
.collect(),
theme: nu_table::Theme::rounded(),
theme: load_theme_from_config(config),
}))
} else {
Ok(None)
}
}
fn load_theme_from_config(config: &Config) -> Theme {
match config.table_mode.as_str() {
"basic" => nu_table::Theme::basic(),
"compact" => nu_table::Theme::compact(),
"compact_double" => nu_table::Theme::compact_double(),
"light" => nu_table::Theme::light(),
"with_love" => nu_table::Theme::with_love(),
"rounded" => nu_table::Theme::rounded(),
"reinforced" => nu_table::Theme::reinforced(),
"heavy" => nu_table::Theme::heavy(),
"none" => nu_table::Theme::none(),
_ => nu_table::Theme::rounded(),
}
}