add --table_mode -m parameter (#5513)

* add `--table_mode` `-m` parameter

* underscores to dashes
This commit is contained in:
Darren Schroeder 2022-05-11 16:15:31 -05:00 committed by GitHub
parent ec804f4568
commit 0b95465ea1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 12 deletions

View file

@ -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<Value>,
) -> 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);

View file

@ -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(

View file

@ -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<i64> = 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)]
{

View file

@ -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)

View file

@ -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<Expression> = call.get_flag_expr("env-config");
let log_level: Option<Expression> = call.get_flag_expr("log-level");
let threads: Option<Value> = call.get_flag(engine_state, &mut stack, "threads")?;
let table_mode: Option<Value> =
call.get_flag(engine_state, &mut stack, "table-mode")?;
fn extract_contents(
expression: Option<Expression>,
@ -384,6 +388,7 @@ fn parse_commandline_args(
log_level,
perf,
threads,
table_mode,
});
}
}
@ -406,6 +411,7 @@ struct NushellCliArgs {
log_level: Option<Spanned<String>>,
perf: bool,
threads: Option<Value>,
table_mode: Option<Value>,
}
#[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;