diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 5971b70bf..7af47b10b 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -1156,7 +1156,7 @@ pub async fn process_line( }; if let Ok(mut output_stream) = - crate::commands::autoview::autoview(context).await + crate::commands::autoview::command::autoview(context).await { loop { match output_stream.try_next().await { diff --git a/crates/nu-cli/src/commands/autoview.rs b/crates/nu-cli/src/commands/autoview/command.rs similarity index 96% rename from crates/nu-cli/src/commands/autoview.rs rename to crates/nu-cli/src/commands/autoview/command.rs index c222c981c..9ba19caba 100644 --- a/crates/nu-cli/src/commands/autoview.rs +++ b/crates/nu-cli/src/commands/autoview/command.rs @@ -1,9 +1,6 @@ -use crate::commands::UnevaluatedCallInfo; -use crate::commands::WholeStreamCommand; +use crate::commands::autoview::options::{ConfigExtensions, NuConfig as AutoViewConfiguration}; +use crate::commands::{UnevaluatedCallInfo, WholeStreamCommand}; use crate::prelude::*; -use nu_data::config::table::AutoPivotMode; -use nu_data::config::table::HasTableProperties; -use nu_data::config::NuConfig as Configuration; use nu_data::value::format_leaf; use nu_errors::ShellError; use nu_protocol::hir::{self, Expression, ExternalRedirection, Literal, SpannedExpression}; @@ -11,10 +8,10 @@ use nu_protocol::{Primitive, Scope, Signature, UntaggedValue, Value}; use parking_lot::Mutex; use std::sync::atomic::AtomicBool; -pub struct Autoview; +pub struct Command; #[async_trait] -impl WholeStreamCommand for Autoview { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "autoview" } @@ -85,7 +82,7 @@ impl RunnableContextWithoutInput { } pub async fn autoview(context: RunnableContext) -> Result { - let configuration = Configuration::new(); + let configuration = AutoViewConfiguration::new(); let binary = context.get_command("binaryview"); let text = context.get_command("textview"); @@ -242,8 +239,8 @@ pub async fn autoview(context: RunnableContext) -> Result RawComm #[cfg(test)] mod tests { - use super::Autoview; + use super::Command; #[test] fn examples_work_as_expected() { use crate::examples::test as test_examples; - test_examples(Autoview {}) + test_examples(Command {}) } } diff --git a/crates/nu-cli/src/commands/autoview/mod.rs b/crates/nu-cli/src/commands/autoview/mod.rs new file mode 100644 index 000000000..5cb35ceb1 --- /dev/null +++ b/crates/nu-cli/src/commands/autoview/mod.rs @@ -0,0 +1,4 @@ +pub mod command; +mod options; + +pub use command::Command as Autoview; diff --git a/crates/nu-cli/src/commands/autoview/options.rs b/crates/nu-cli/src/commands/autoview/options.rs new file mode 100644 index 000000000..bbcea6f19 --- /dev/null +++ b/crates/nu-cli/src/commands/autoview/options.rs @@ -0,0 +1,60 @@ +pub use nu_data::config::NuConfig; +use std::fmt::Debug; + +#[derive(PartialEq, Debug)] +pub enum AutoPivotMode { + Auto, + Always, + Never, +} + +impl AutoPivotMode { + pub fn is_auto(&self) -> bool { + match &self { + AutoPivotMode::Auto => true, + _ => false, + } + } + + pub fn is_always(&self) -> bool { + match &self { + AutoPivotMode::Always => true, + _ => false, + } + } + + #[allow(unused)] + pub fn is_never(&self) -> bool { + match &self { + AutoPivotMode::Never => true, + _ => false, + } + } +} + +pub trait ConfigExtensions: Debug + Send { + fn pivot_mode(&self) -> AutoPivotMode; +} + +pub fn pivot_mode(config: &NuConfig) -> AutoPivotMode { + let vars = config.vars.lock(); + + if let Some(mode) = vars.get("pivot_mode") { + let mode = match mode.as_string() { + Ok(m) if m.to_lowercase() == "auto" => AutoPivotMode::Auto, + Ok(m) if m.to_lowercase() == "always" => AutoPivotMode::Always, + Ok(m) if m.to_lowercase() == "never" => AutoPivotMode::Never, + _ => AutoPivotMode::Always, + }; + + return mode; + } + + AutoPivotMode::Always +} + +impl ConfigExtensions for NuConfig { + fn pivot_mode(&self) -> AutoPivotMode { + pivot_mode(self) + } +} diff --git a/crates/nu-cli/src/commands/table.rs b/crates/nu-cli/src/commands/table/command.rs similarity index 97% rename from crates/nu-cli/src/commands/table.rs rename to crates/nu-cli/src/commands/table/command.rs index a57e56b72..6aeceb064 100644 --- a/crates/nu-cli/src/commands/table.rs +++ b/crates/nu-cli/src/commands/table/command.rs @@ -1,7 +1,6 @@ +use crate::commands::table::options::{ConfigExtensions, NuConfig as TableConfiguration}; use crate::commands::WholeStreamCommand; use crate::prelude::*; -use nu_data::config::table::HasTableProperties; -use nu_data::config::NuConfig as TableConfiguration; use nu_data::value::{format_leaf, style_leaf}; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; @@ -11,10 +10,10 @@ use std::time::Instant; const STREAM_PAGE_SIZE: usize = 1000; const STREAM_TIMEOUT_CHECK_INTERVAL: usize = 100; -pub struct Table; +pub struct Command; #[async_trait] -impl WholeStreamCommand for Table { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "table" } diff --git a/crates/nu-cli/src/commands/table/mod.rs b/crates/nu-cli/src/commands/table/mod.rs new file mode 100644 index 000000000..96f007aac --- /dev/null +++ b/crates/nu-cli/src/commands/table/mod.rs @@ -0,0 +1,4 @@ +pub mod command; +mod options; + +pub use command::Command as Table; diff --git a/crates/nu-cli/src/commands/table/options.rs b/crates/nu-cli/src/commands/table/options.rs new file mode 100644 index 000000000..86c55e14c --- /dev/null +++ b/crates/nu-cli/src/commands/table/options.rs @@ -0,0 +1,112 @@ +pub use nu_data::config::NuConfig; +use std::fmt::Debug; + +pub trait ConfigExtensions: Debug + Send { + fn header_alignment(&self) -> nu_table::Alignment; + fn header_color(&self) -> Option; + fn header_bold(&self) -> bool; + fn table_mode(&self) -> nu_table::Theme; + fn disabled_indexes(&self) -> bool; + fn text_color(&self) -> Option; + fn line_color(&self) -> Option; +} + +pub fn header_alignment(config: &NuConfig) -> nu_table::Alignment { + let vars = config.vars.lock(); + + let alignment = vars.get("header_align"); + + if alignment.is_none() { + return nu_table::Alignment::Center; + } + + alignment.map_or(nu_table::Alignment::Left, |a| { + a.as_string().map_or(nu_table::Alignment::Center, |a| { + match a.to_lowercase().as_str() { + "center" | "c" => nu_table::Alignment::Center, + "right" | "r" => nu_table::Alignment::Right, + _ => nu_table::Alignment::Center, + } + }) + }) +} + +pub fn get_color_for_config_key(config: &NuConfig, key: &str) -> Option { + let vars = config.vars.lock(); + + Some(match vars.get(key) { + Some(c) => match c.as_string() { + Ok(color) => match color.to_lowercase().as_str() { + "g" | "green" => ansi_term::Color::Green, + "r" | "red" => ansi_term::Color::Red, + "u" | "blue" => ansi_term::Color::Blue, + "b" | "black" => ansi_term::Color::Black, + "y" | "yellow" => ansi_term::Color::Yellow, + "p" | "purple" => ansi_term::Color::Purple, + "c" | "cyan" => ansi_term::Color::Cyan, + "w" | "white" => ansi_term::Color::White, + _ => ansi_term::Color::Green, + }, + _ => ansi_term::Color::Green, + }, + _ => ansi_term::Color::Green, + }) +} + +pub fn header_bold(config: &NuConfig) -> bool { + let vars = config.vars.lock(); + + vars.get("header_bold") + .map(|x| x.as_bool().unwrap_or(true)) + .unwrap_or(true) +} + +pub fn table_mode(config: &NuConfig) -> nu_table::Theme { + let vars = config.vars.lock(); + + vars.get("table_mode") + .map_or(nu_table::Theme::compact(), |mode| match mode.as_string() { + Ok(m) if m == "basic" => nu_table::Theme::basic(), + Ok(m) if m == "compact" => nu_table::Theme::compact(), + Ok(m) if m == "light" => nu_table::Theme::light(), + Ok(m) if m == "thin" => nu_table::Theme::thin(), + _ => nu_table::Theme::compact(), + }) +} + +pub fn disabled_indexes(config: &NuConfig) -> bool { + let vars = config.vars.lock(); + + vars.get("disable_table_indexes") + .map_or(false, |x| x.as_bool().unwrap_or(false)) +} + +impl ConfigExtensions for NuConfig { + fn header_alignment(&self) -> nu_table::Alignment { + header_alignment(self) + } + + fn header_color(&self) -> Option { + get_color_for_config_key(self, "header_color") + } + + fn text_color(&self) -> Option { + get_color_for_config_key(self, "text_color") + } + + fn line_color(&self) -> Option { + get_color_for_config_key(self, "line_color") + } + + fn header_bold(&self) -> bool { + header_bold(self) + } + + fn table_mode(&self) -> nu_table::Theme { + table_mode(self) + } + + fn disabled_indexes(&self) -> bool { + disabled_indexes(self) + } +} diff --git a/crates/nu-data/src/config.rs b/crates/nu-data/src/config.rs index 4dfd95d25..71d4b376b 100644 --- a/crates/nu-data/src/config.rs +++ b/crates/nu-data/src/config.rs @@ -1,6 +1,5 @@ mod conf; mod nuconfig; -pub mod table; pub mod tests;