diff --git a/Cargo.lock b/Cargo.lock index 6940bb757..bec211366 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2767,6 +2767,7 @@ dependencies = [ "bat", "crossterm", "nu-build", + "nu-cli", "nu-errors", "nu-plugin", "nu-protocol", diff --git a/crates/nu-cli/src/data.rs b/crates/nu-cli/src/data.rs index 5699e6848..4269aef91 100644 --- a/crates/nu-cli/src/data.rs +++ b/crates/nu-cli/src/data.rs @@ -1,6 +1,6 @@ pub(crate) mod base; pub(crate) mod command; -pub(crate) mod config; +pub mod config; pub(crate) mod dict; pub(crate) mod files; pub mod primitive; diff --git a/crates/nu-cli/src/data/config.rs b/crates/nu-cli/src/data/config.rs index fbd296f27..391b5db1b 100644 --- a/crates/nu-cli/src/data/config.rs +++ b/crates/nu-cli/src/data/config.rs @@ -103,7 +103,7 @@ pub fn read( } } -pub(crate) fn config(tag: impl Into) -> Result, ShellError> { +pub fn config(tag: impl Into) -> Result, ShellError> { read(tag, &None) } diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index cd923e2c1..0cb86aa50 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -16,7 +16,7 @@ extern crate quickcheck_macros; mod cli; mod commands; mod context; -mod data; +pub mod data; mod deserializer; mod env; mod evaluate; @@ -39,6 +39,7 @@ pub use crate::commands::command::{ }; pub use crate::commands::help::get_help; pub use crate::context::CommandRegistry; +pub use crate::data::config; pub use crate::data::dict::TaggedListBuilder; pub use crate::data::primitive; pub use crate::data::value; diff --git a/crates/nu_plugin_textview/Cargo.toml b/crates/nu_plugin_textview/Cargo.toml index 65e801964..7e4ae7941 100644 --- a/crates/nu_plugin_textview/Cargo.toml +++ b/crates/nu_plugin_textview/Cargo.toml @@ -14,7 +14,7 @@ nu-plugin = { path = "../nu-plugin", version = "0.15.1" } nu-protocol = { path = "../nu-protocol", version = "0.15.1" } nu-source = { path = "../nu-source", version = "0.15.1" } nu-errors = { path = "../nu-errors", version = "0.15.1" } -#nu-cli = { path = "../nu-cli", version = "0.15.1" } +nu-cli = { path = "../nu-cli", version = "0.15.1" } crossterm = "0.17.5" syntect = { version = "4.2", default-features = false, features = ["default-fancy"]} diff --git a/crates/nu_plugin_textview/src/textview.rs b/crates/nu_plugin_textview/src/textview.rs index 89e422a9b..5c8aec647 100644 --- a/crates/nu_plugin_textview/src/textview.rs +++ b/crates/nu_plugin_textview/src/textview.rs @@ -1,5 +1,5 @@ use nu_protocol::{Primitive, UntaggedValue, Value}; -use nu_source::AnchorLocation; +use nu_source::{AnchorLocation, Tag}; use std::path::Path; #[derive(Default)] @@ -11,7 +11,136 @@ impl TextView { } } +#[allow(clippy::cognitive_complexity)] pub fn view_text_value(value: &Value) { + let mut term_width: u64 = textwrap::termwidth() as u64; + let mut tab_width: u64 = 4; + let mut colored_output = true; + let mut true_color = true; + let mut header = true; + let mut line_numbers = true; + let mut grid = true; + let mut vcs_modification_markers = true; + let mut snip = true; + let mut wrapping_mode = bat::WrappingMode::NoWrapping; + let mut use_italics = true; + let mut paging_mode = bat::PagingMode::QuitIfOneScreen; + let mut pager = "less".to_string(); + let mut line_ranges = bat::line_range::LineRanges::all(); + let mut _highlight_range = "0,0"; + let highlight_range_from: u64 = 0; + let highlight_range_to: u64 = 0; + let mut theme = "OneHalfDark".to_string(); + + if let Ok(config) = nu_cli::data::config::config(Tag::unknown()) { + if let Some(batvars) = config.get("textview") { + for (idx, value) in batvars.row_entries() { + match idx { + x if x == "term_width" => { + term_width = match value.as_u64() { + Ok(n) => n, + _ => textwrap::termwidth() as u64, + } + } + x if x == "tab_width" => { + tab_width = match value.as_u64() { + Ok(n) => n, + _ => 4u64, + } + } + x if x == "colored_output" => { + colored_output = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "true_color" => { + true_color = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "header" => { + header = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "line_numbers" => { + line_numbers = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "grid" => { + grid = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "vcs_modification_markers" => { + vcs_modification_markers = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "snip" => { + snip = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "wrapping_mode" => { + wrapping_mode = match value.as_string() { + Ok(s) if s.to_lowercase() == "nowrapping" => { + bat::WrappingMode::NoWrapping + } + Ok(s) if s.to_lowercase() == "character" => { + bat::WrappingMode::Character + } + _ => bat::WrappingMode::NoWrapping, + } + } + x if x == "use_italics" => { + use_italics = match value.as_bool() { + Ok(b) => b, + _ => true, + } + } + x if x == "paging_mode" => { + paging_mode = match value.as_string() { + Ok(s) if s.to_lowercase() == "always" => bat::PagingMode::Always, + Ok(s) if s.to_lowercase() == "never" => bat::PagingMode::Never, + Ok(s) if s.to_lowercase() == "quitifonescreen" => { + bat::PagingMode::QuitIfOneScreen + } + _ => bat::PagingMode::QuitIfOneScreen, + } + } + x if x == "pager" => { + pager = match value.as_string() { + Ok(s) => s, + _ => "less".to_string(), + } + } + x if x == "line_ranges" => line_ranges = bat::line_range::LineRanges::all(), // not real sure what to do with this + x if x == "highlight_range" => _highlight_range = "0,0", //ignore config value for now + x if x == "theme" => { + theme = match value.as_string() { + Ok(s) => s, + _ => "OneDarkHalf".to_string(), + } + } + _ => (), + } + } + } else { + println!("Couldn't find bat section in config"); + } + } else { + println!("Error reading config!"); + } + let value_anchor = value.anchor(); if let UntaggedValue::Primitive(Primitive::String(ref s)) = &value.value { if let Some(source) = value_anchor { @@ -46,44 +175,44 @@ pub fn view_text_value(value: &Value) { // Let bat do it's thing bat::PrettyPrinter::new() .input_from_bytes_with_name(s.as_bytes(), file_path) - .term_width(textwrap::termwidth()) - .tab_width(Some(4)) - .colored_output(true) - .true_color(true) - .header(true) - .line_numbers(true) - .grid(true) - .vcs_modification_markers(true) - .snip(true) - .wrapping_mode(bat::WrappingMode::NoWrapping) - .use_italics(true) - .paging_mode(bat::PagingMode::QuitIfOneScreen) - .pager("less") - .line_ranges(bat::line_range::LineRanges::all()) - .highlight_range(0, 0) - .theme("OneHalfDark") + .term_width(term_width as usize) + .tab_width(Some(tab_width as usize)) + .colored_output(colored_output) + .true_color(true_color) + .header(header) + .line_numbers(line_numbers) + .grid(grid) + .vcs_modification_markers(vcs_modification_markers) + .snip(snip) + .wrapping_mode(wrapping_mode) + .use_italics(use_italics) + .paging_mode(paging_mode) + .pager(&pager) + .line_ranges(line_ranges) + .highlight_range(highlight_range_from as usize, highlight_range_to as usize) + .theme(&theme) .print() .expect("Error with bat PrettyPrint"); } _ => { bat::PrettyPrinter::new() .input_from_bytes(s.as_bytes()) - .term_width(textwrap::termwidth()) - .tab_width(Some(4)) - .colored_output(true) - .true_color(true) - .header(true) - .line_numbers(true) - .grid(true) - .vcs_modification_markers(true) - .snip(true) - .wrapping_mode(bat::WrappingMode::NoWrapping) - .use_italics(true) - .paging_mode(bat::PagingMode::QuitIfOneScreen) - .pager("less") - .line_ranges(bat::line_range::LineRanges::all()) - .highlight_range(0, 0) - .theme("OneHalfDark") + .term_width(term_width as usize) + .tab_width(Some(tab_width as usize)) + .colored_output(colored_output) + .true_color(true_color) + .header(header) + .line_numbers(line_numbers) + .grid(grid) + .vcs_modification_markers(vcs_modification_markers) + .snip(snip) + .wrapping_mode(wrapping_mode) + .use_italics(use_italics) + .paging_mode(paging_mode) + .pager(&pager) + .line_ranges(line_ranges) + .highlight_range(highlight_range_from as usize, highlight_range_to as usize) + .theme(&theme) .print() .expect("Error with bat PrettyPrint"); } @@ -91,22 +220,22 @@ pub fn view_text_value(value: &Value) { } else { bat::PrettyPrinter::new() .input_from_bytes(s.as_bytes()) - .term_width(textwrap::termwidth()) - .tab_width(Some(4)) - .colored_output(true) - .true_color(true) - .header(true) - .line_numbers(true) - .grid(true) - .vcs_modification_markers(true) - .snip(true) - .wrapping_mode(bat::WrappingMode::NoWrapping) - .use_italics(true) - .paging_mode(bat::PagingMode::QuitIfOneScreen) - .pager("less") - .line_ranges(bat::line_range::LineRanges::all()) - .highlight_range(0, 0) - .theme("OneHalfDark") + .term_width(term_width as usize) + .tab_width(Some(tab_width as usize)) + .colored_output(colored_output) + .true_color(true_color) + .header(header) + .line_numbers(line_numbers) + .grid(grid) + .vcs_modification_markers(vcs_modification_markers) + .snip(snip) + .wrapping_mode(wrapping_mode) + .use_italics(use_italics) + .paging_mode(paging_mode) + .pager(&pager) + .line_ranges(line_ranges) + .highlight_range(highlight_range_from as usize, highlight_range_to as usize) + .theme(&theme) .print() .expect("Error with bat PrettyPrint"); }