Bat config (#2010)

* WIP - changes to support bat config

* added bat configuration

* removed debug info

* clippy fix

* changed [bat] to [textview]

Co-authored-by: Darren Schroeder <fdncred@hotmail.com>
This commit is contained in:
Darren Schroeder 2020-06-19 15:08:59 -05:00 committed by GitHub
parent bc9cc75c8a
commit 6bfd8532e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 184 additions and 53 deletions

1
Cargo.lock generated
View file

@ -2767,6 +2767,7 @@ dependencies = [
"bat",
"crossterm",
"nu-build",
"nu-cli",
"nu-errors",
"nu-plugin",
"nu-protocol",

View file

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

View file

@ -103,7 +103,7 @@ pub fn read(
}
}
pub(crate) fn config(tag: impl Into<Tag>) -> Result<IndexMap<String, Value>, ShellError> {
pub fn config(tag: impl Into<Tag>) -> Result<IndexMap<String, Value>, ShellError> {
read(tag, &None)
}

View file

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

View file

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

View file

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