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

@ -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(),
}
}