Command especific configuration extraction baseline.

This commit is contained in:
Andrés N. Robalino 2020-08-26 10:05:07 -05:00
parent 034c33c2b5
commit 6685f74e03
8 changed files with 193 additions and 18 deletions

View File

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

View File

@ -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<OutputStream, ShellError> {
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<OutputStream, ShellErr
Value {
value: UntaggedValue::Row(row),
..
} if pivot_mode == AutoPivotMode::Always
|| (pivot_mode == AutoPivotMode::Auto
} if pivot_mode.is_always()
|| (pivot_mode.is_auto()
&& (row
.entries
.iter()
@ -326,12 +323,12 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> 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 {})
}
}

View File

@ -0,0 +1,4 @@
pub mod command;
mod options;
pub use command::Command as Autoview;

View File

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

View File

@ -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"
}

View File

@ -0,0 +1,4 @@
pub mod command;
mod options;
pub use command::Command as Table;

View File

@ -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<ansi_term::Color>;
fn header_bold(&self) -> bool;
fn table_mode(&self) -> nu_table::Theme;
fn disabled_indexes(&self) -> bool;
fn text_color(&self) -> Option<ansi_term::Color>;
fn line_color(&self) -> Option<ansi_term::Color>;
}
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<ansi_term::Color> {
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<ansi_term::Color> {
get_color_for_config_key(self, "header_color")
}
fn text_color(&self) -> Option<ansi_term::Color> {
get_color_for_config_key(self, "text_color")
}
fn line_color(&self) -> Option<ansi_term::Color> {
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)
}
}

View File

@ -1,6 +1,5 @@
mod conf;
mod nuconfig;
pub mod table;
pub mod tests;