From 0b95465ea13d3299d016e76539fe4b8ae112321f Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Wed, 11 May 2022 16:15:31 -0500 Subject: [PATCH] add `--table_mode` `-m` parameter (#5513) * add `--table_mode` `-m` parameter * underscores to dashes --- crates/nu-cli/src/commands.rs | 10 +++++--- crates/nu-cli/src/eval_file.rs | 7 ++++-- crates/nu-command/src/viewers/table.rs | 23 +++++++++++++++++++ crates/nu-protocol/src/engine/engine_state.rs | 4 ++++ src/main.rs | 21 +++++++++++------ 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index 5e1764692..65093dcc2 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -6,7 +6,7 @@ use nu_parser::parse; use nu_protocol::engine::Stack; use nu_protocol::{ engine::{EngineState, StateDelta, StateWorkingSet}, - PipelineData, Spanned, + PipelineData, Spanned, Value, }; use std::path::Path; @@ -17,6 +17,7 @@ pub fn evaluate_commands( stack: &mut Stack, input: PipelineData, is_perf_true: bool, + table_mode: Option, ) -> Result<()> { // Run a command (or commands) given to us by the user let (block, delta) = { @@ -37,7 +38,10 @@ pub fn evaluate_commands( report_error(&working_set, &err); } - let config = engine_state.get_config().clone(); + let mut config = engine_state.get_config().clone(); + if let Some(t_mode) = table_mode { + config.table_mode = t_mode.as_string()?; + } // Merge the delta in case env vars changed in the config match nu_engine::env::current_dir(engine_state, stack) { @@ -66,7 +70,7 @@ pub fn evaluate_commands( match eval_block(engine_state, stack, &block, input, false, false) { Ok(pipeline_data) => { - crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &config) + crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config) } Err(err) => { let working_set = StateWorkingSet::new(engine_state); diff --git a/crates/nu-cli/src/eval_file.rs b/crates/nu-cli/src/eval_file.rs index d229b7845..2e8208c00 100644 --- a/crates/nu-cli/src/eval_file.rs +++ b/crates/nu-cli/src/eval_file.rs @@ -61,16 +61,19 @@ pub fn evaluate_file( } pub fn print_table_or_error( - engine_state: &EngineState, + engine_state: &mut EngineState, stack: &mut Stack, mut pipeline_data: PipelineData, - config: &Config, + config: &mut Config, ) { let exit_code = match &mut pipeline_data { PipelineData::ExternalStream { exit_code, .. } => exit_code.take(), _ => None, }; + // Change the engine_state config to use the passed in configuration + engine_state.set_config(config); + match engine_state.find_decl("table".as_bytes(), &[]) { Some(decl_id) => { let table = engine_state.get_decl(decl_id).run( diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 742afe317..96a7da128 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -44,6 +44,7 @@ impl Command for Table { "row number to start viewing from", Some('n'), ) + .switch("list", "list available table modes/themes", Some('l')) .category(Category::Viewers) } @@ -60,6 +61,7 @@ impl Command for Table { let color_hm = get_color_config(config); let start_num: Option = call.get_flag(engine_state, stack, "start-number")?; let row_offset = start_num.unwrap_or_default() as usize; + let list: bool = call.has_flag("list"); let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() { (w - 1) as usize @@ -67,6 +69,27 @@ impl Command for Table { 80usize }; + if list { + let table_modes = vec![ + Value::string("basic", Span::test_data()), + Value::string("compact", Span::test_data()), + Value::string("compact_double", Span::test_data()), + Value::string("default", Span::test_data()), + Value::string("heavy", Span::test_data()), + Value::string("light", Span::test_data()), + Value::string("none", Span::test_data()), + Value::string("reinforced", Span::test_data()), + Value::string("rounded", Span::test_data()), + Value::string("thin", Span::test_data()), + Value::string("with_love", Span::test_data()), + ]; + return Ok(Value::List { + vals: table_modes, + span: Span::test_data(), + } + .into_pipeline_data()); + } + // reset vt processing, aka ansi because illbehaved externals can break it #[cfg(windows)] { diff --git a/crates/nu-protocol/src/engine/engine_state.rs b/crates/nu-protocol/src/engine/engine_state.rs index 494b5a671..709ffc2f8 100644 --- a/crates/nu-protocol/src/engine/engine_state.rs +++ b/crates/nu-protocol/src/engine/engine_state.rs @@ -817,6 +817,10 @@ impl EngineState { &self.config } + pub fn set_config(&mut self, conf: &Config) { + self.config = conf.clone(); + } + pub fn get_var(&self, var_id: VarId) -> &Variable { self.vars .get(var_id) diff --git a/src/main.rs b/src/main.rs index 55da1c677..a6559dfda 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,9 @@ fn main() -> Result<()> { } else if arg.starts_with('-') { // Cool, it's a flag let flag_value = match arg.as_ref() { - "--commands" | "-c" => args.next().map(|a| escape_quote_string(&a)), + "--commands" | "-c" | "--table-mode" | "-m" => { + args.next().map(|a| escape_quote_string(&a)) + } "--config" | "--env-config" => args.next().map(|a| escape_quote_string(&a)), "--log-level" | "--testbin" | "--threads" | "-t" => args.next(), _ => None, @@ -200,11 +202,11 @@ fn main() -> Result<()> { &mut stack, input, is_perf_true(), + binary_args.table_mode, ); if is_perf_true() { info!("-c command execution {}:{}:{}", file!(), line!(), column!()); } - ret_val } else if !script_name.is_empty() && binary_args.interactive_shell.is_none() { #[cfg(feature = "plugin")] @@ -322,6 +324,8 @@ fn parse_commandline_args( let env_file: Option = call.get_flag_expr("env-config"); let log_level: Option = call.get_flag_expr("log-level"); let threads: Option = call.get_flag(engine_state, &mut stack, "threads")?; + let table_mode: Option = + call.get_flag(engine_state, &mut stack, "table-mode")?; fn extract_contents( expression: Option, @@ -384,6 +388,7 @@ fn parse_commandline_args( log_level, perf, threads, + table_mode, }); } } @@ -406,6 +411,7 @@ struct NushellCliArgs { log_level: Option>, perf: bool, threads: Option, + table_mode: Option, } #[derive(Clone)] @@ -464,6 +470,12 @@ impl Command for Nu { "threads to use for parallel commands", Some('t'), ) + .named( + "table-mode", + SyntaxShape::String, + "the table mode to use. rounded is default.", + Some('m'), + ) .optional( "script file", SyntaxShape::Filepath, @@ -515,11 +527,6 @@ pub fn is_perf_true() -> bool { IS_PERF.with(|value| *value.borrow()) } -// #[allow(dead_code)] -// fn is_perf_value() -> bool { -// IS_PERF.with(|value| *value.borrow()) -// } - fn set_is_perf_value(value: bool) { IS_PERF.with(|new_value| { *new_value.borrow_mut() = value;