mirror of
https://github.com/nushell/nushell.git
synced 2025-02-02 11:39:55 +01:00
add --table_mode
-m
parameter (#5513)
* add `--table_mode` `-m` parameter * underscores to dashes
This commit is contained in:
parent
ec804f4568
commit
0b95465ea1
@ -6,7 +6,7 @@ use nu_parser::parse;
|
|||||||
use nu_protocol::engine::Stack;
|
use nu_protocol::engine::Stack;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, StateDelta, StateWorkingSet},
|
engine::{EngineState, StateDelta, StateWorkingSet},
|
||||||
PipelineData, Spanned,
|
PipelineData, Spanned, Value,
|
||||||
};
|
};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
@ -17,6 +17,7 @@ pub fn evaluate_commands(
|
|||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
is_perf_true: bool,
|
is_perf_true: bool,
|
||||||
|
table_mode: Option<Value>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Run a command (or commands) given to us by the user
|
// Run a command (or commands) given to us by the user
|
||||||
let (block, delta) = {
|
let (block, delta) = {
|
||||||
@ -37,7 +38,10 @@ pub fn evaluate_commands(
|
|||||||
report_error(&working_set, &err);
|
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
|
// Merge the delta in case env vars changed in the config
|
||||||
match nu_engine::env::current_dir(engine_state, stack) {
|
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) {
|
match eval_block(engine_state, stack, &block, input, false, false) {
|
||||||
Ok(pipeline_data) => {
|
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) => {
|
Err(err) => {
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
|
@ -61,16 +61,19 @@ pub fn evaluate_file(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_table_or_error(
|
pub fn print_table_or_error(
|
||||||
engine_state: &EngineState,
|
engine_state: &mut EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
mut pipeline_data: PipelineData,
|
mut pipeline_data: PipelineData,
|
||||||
config: &Config,
|
config: &mut Config,
|
||||||
) {
|
) {
|
||||||
let exit_code = match &mut pipeline_data {
|
let exit_code = match &mut pipeline_data {
|
||||||
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
|
PipelineData::ExternalStream { exit_code, .. } => exit_code.take(),
|
||||||
_ => None,
|
_ => 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(), &[]) {
|
match engine_state.find_decl("table".as_bytes(), &[]) {
|
||||||
Some(decl_id) => {
|
Some(decl_id) => {
|
||||||
let table = engine_state.get_decl(decl_id).run(
|
let table = engine_state.get_decl(decl_id).run(
|
||||||
|
@ -44,6 +44,7 @@ impl Command for Table {
|
|||||||
"row number to start viewing from",
|
"row number to start viewing from",
|
||||||
Some('n'),
|
Some('n'),
|
||||||
)
|
)
|
||||||
|
.switch("list", "list available table modes/themes", Some('l'))
|
||||||
.category(Category::Viewers)
|
.category(Category::Viewers)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,6 +61,7 @@ impl Command for Table {
|
|||||||
let color_hm = get_color_config(config);
|
let color_hm = get_color_config(config);
|
||||||
let start_num: Option<i64> = call.get_flag(engine_state, stack, "start-number")?;
|
let start_num: Option<i64> = call.get_flag(engine_state, stack, "start-number")?;
|
||||||
let row_offset = start_num.unwrap_or_default() as usize;
|
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() {
|
let term_width = if let Some((Width(w), Height(_h))) = terminal_size::terminal_size() {
|
||||||
(w - 1) as usize
|
(w - 1) as usize
|
||||||
@ -67,6 +69,27 @@ impl Command for Table {
|
|||||||
80usize
|
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
|
// reset vt processing, aka ansi because illbehaved externals can break it
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
{
|
{
|
||||||
|
@ -817,6 +817,10 @@ impl EngineState {
|
|||||||
&self.config
|
&self.config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_config(&mut self, conf: &Config) {
|
||||||
|
self.config = conf.clone();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_var(&self, var_id: VarId) -> &Variable {
|
pub fn get_var(&self, var_id: VarId) -> &Variable {
|
||||||
self.vars
|
self.vars
|
||||||
.get(var_id)
|
.get(var_id)
|
||||||
|
21
src/main.rs
21
src/main.rs
@ -86,7 +86,9 @@ fn main() -> Result<()> {
|
|||||||
} else if arg.starts_with('-') {
|
} else if arg.starts_with('-') {
|
||||||
// Cool, it's a flag
|
// Cool, it's a flag
|
||||||
let flag_value = match arg.as_ref() {
|
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)),
|
"--config" | "--env-config" => args.next().map(|a| escape_quote_string(&a)),
|
||||||
"--log-level" | "--testbin" | "--threads" | "-t" => args.next(),
|
"--log-level" | "--testbin" | "--threads" | "-t" => args.next(),
|
||||||
_ => None,
|
_ => None,
|
||||||
@ -200,11 +202,11 @@ fn main() -> Result<()> {
|
|||||||
&mut stack,
|
&mut stack,
|
||||||
input,
|
input,
|
||||||
is_perf_true(),
|
is_perf_true(),
|
||||||
|
binary_args.table_mode,
|
||||||
);
|
);
|
||||||
if is_perf_true() {
|
if is_perf_true() {
|
||||||
info!("-c command execution {}:{}:{}", file!(), line!(), column!());
|
info!("-c command execution {}:{}:{}", file!(), line!(), column!());
|
||||||
}
|
}
|
||||||
|
|
||||||
ret_val
|
ret_val
|
||||||
} else if !script_name.is_empty() && binary_args.interactive_shell.is_none() {
|
} else if !script_name.is_empty() && binary_args.interactive_shell.is_none() {
|
||||||
#[cfg(feature = "plugin")]
|
#[cfg(feature = "plugin")]
|
||||||
@ -322,6 +324,8 @@ fn parse_commandline_args(
|
|||||||
let env_file: Option<Expression> = call.get_flag_expr("env-config");
|
let env_file: Option<Expression> = call.get_flag_expr("env-config");
|
||||||
let log_level: Option<Expression> = call.get_flag_expr("log-level");
|
let log_level: Option<Expression> = call.get_flag_expr("log-level");
|
||||||
let threads: Option<Value> = call.get_flag(engine_state, &mut stack, "threads")?;
|
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(
|
fn extract_contents(
|
||||||
expression: Option<Expression>,
|
expression: Option<Expression>,
|
||||||
@ -384,6 +388,7 @@ fn parse_commandline_args(
|
|||||||
log_level,
|
log_level,
|
||||||
perf,
|
perf,
|
||||||
threads,
|
threads,
|
||||||
|
table_mode,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -406,6 +411,7 @@ struct NushellCliArgs {
|
|||||||
log_level: Option<Spanned<String>>,
|
log_level: Option<Spanned<String>>,
|
||||||
perf: bool,
|
perf: bool,
|
||||||
threads: Option<Value>,
|
threads: Option<Value>,
|
||||||
|
table_mode: Option<Value>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -464,6 +470,12 @@ impl Command for Nu {
|
|||||||
"threads to use for parallel commands",
|
"threads to use for parallel commands",
|
||||||
Some('t'),
|
Some('t'),
|
||||||
)
|
)
|
||||||
|
.named(
|
||||||
|
"table-mode",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the table mode to use. rounded is default.",
|
||||||
|
Some('m'),
|
||||||
|
)
|
||||||
.optional(
|
.optional(
|
||||||
"script file",
|
"script file",
|
||||||
SyntaxShape::Filepath,
|
SyntaxShape::Filepath,
|
||||||
@ -515,11 +527,6 @@ pub fn is_perf_true() -> bool {
|
|||||||
IS_PERF.with(|value| *value.borrow())
|
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) {
|
fn set_is_perf_value(value: bool) {
|
||||||
IS_PERF.with(|new_value| {
|
IS_PERF.with(|new_value| {
|
||||||
*new_value.borrow_mut() = value;
|
*new_value.borrow_mut() = value;
|
||||||
|
Loading…
Reference in New Issue
Block a user