mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 01:54:57 +02:00
highlight trailing spaces in tables in darkgray (#2794)
* highlight trailing spaces in tables in darkgray * added leading spaces highlighting * added config point to change the color * Trigger Build
This commit is contained in:
@ -778,7 +778,6 @@ impl WrappedTable {
|
||||
}
|
||||
|
||||
println!("{}", output);
|
||||
// println!("{:#?}", output);
|
||||
}
|
||||
|
||||
fn print_cell_contents(&self, cells: &[WrappedCell], color_hm: &HashMap<String, Style>) {
|
||||
@ -804,7 +803,6 @@ impl WrappedTable {
|
||||
let remainder = self.column_widths[column.0] - line.width;
|
||||
output.push(' ');
|
||||
|
||||
// println!("Column1: [{:?}]", column.1.style);
|
||||
match column.1.style.alignment {
|
||||
Alignment::Left => {
|
||||
if let Some(color) = column.1.style.color_style {
|
||||
@ -869,7 +867,7 @@ impl WrappedTable {
|
||||
}
|
||||
}
|
||||
|
||||
fn new_print_table(&self, color_hm: &HashMap<String, Style>) {
|
||||
fn print_table(&self, color_hm: &HashMap<String, Style>) {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let _ = ansi_term::enable_ansi_support();
|
||||
@ -1015,12 +1013,16 @@ pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap<String, St
|
||||
// This should give us the final max column width
|
||||
let max_column_width = column_space.max_width(termwidth);
|
||||
|
||||
let wrapped_table = wrap_cells(processed_table, max_column_width);
|
||||
let wrapped_table = wrap_cells(processed_table, max_column_width, &color_hm);
|
||||
|
||||
wrapped_table.new_print_table(&color_hm);
|
||||
wrapped_table.print_table(&color_hm);
|
||||
}
|
||||
|
||||
fn wrap_cells(processed_table: ProcessedTable, max_column_width: usize) -> WrappedTable {
|
||||
fn wrap_cells(
|
||||
processed_table: ProcessedTable,
|
||||
max_column_width: usize,
|
||||
color_hm: &HashMap<String, Style>,
|
||||
) -> WrappedTable {
|
||||
let mut column_widths = vec![
|
||||
0;
|
||||
std::cmp::max(
|
||||
@ -1041,7 +1043,8 @@ fn wrap_cells(processed_table: ProcessedTable, max_column_width: usize) -> Wrapp
|
||||
};
|
||||
|
||||
for contents in header.1.contents.into_iter() {
|
||||
let (mut lines, inner_max_width) = wrap(max_column_width, contents.into_iter());
|
||||
let (mut lines, inner_max_width) =
|
||||
wrap(max_column_width, contents.into_iter(), &color_hm);
|
||||
wrapped.lines.append(&mut lines);
|
||||
if inner_max_width > wrapped.max_width {
|
||||
wrapped.max_width = inner_max_width;
|
||||
@ -1063,7 +1066,8 @@ fn wrap_cells(processed_table: ProcessedTable, max_column_width: usize) -> Wrapp
|
||||
style: column.1.style,
|
||||
};
|
||||
for contents in column.1.contents.into_iter() {
|
||||
let (mut lines, inner_max_width) = wrap(max_column_width, contents.into_iter());
|
||||
let (mut lines, inner_max_width) =
|
||||
wrap(max_column_width, contents.into_iter(), &color_hm);
|
||||
wrapped.lines.append(&mut lines);
|
||||
if inner_max_width > wrapped.max_width {
|
||||
wrapped.max_width = inner_max_width;
|
||||
|
@ -1,4 +1,6 @@
|
||||
use crate::table::TextStyle;
|
||||
use ansi_term::Style;
|
||||
use std::collections::HashMap;
|
||||
use std::{fmt::Display, iter::Iterator};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
@ -136,12 +138,18 @@ fn split_word<'a>(cell_width: usize, word: &'a str) -> Vec<Subline<'a>> {
|
||||
pub fn wrap<'a>(
|
||||
cell_width: usize,
|
||||
mut input: impl Iterator<Item = Subline<'a>>,
|
||||
color_hm: &HashMap<String, Style>,
|
||||
) -> (Vec<WrappedLine>, usize) {
|
||||
let mut lines = vec![];
|
||||
let mut current_line: Vec<Subline> = vec![];
|
||||
let mut current_width = 0;
|
||||
let mut first = true;
|
||||
let mut max_width = 0;
|
||||
let lead_trail_space_bg_color = color_hm
|
||||
.get("leading_trailing_space_bg")
|
||||
.unwrap_or(&Style::default())
|
||||
.to_owned();
|
||||
|
||||
loop {
|
||||
match input.next() {
|
||||
Some(item) => {
|
||||
@ -233,6 +241,28 @@ pub fn wrap<'a>(
|
||||
current_max = current_line_width;
|
||||
}
|
||||
|
||||
// highlight leading and trailing spaces so they stand out.
|
||||
let mut bg_color_string = Style::default().prefix().to_string();
|
||||
// right now config settings can only set foreground colors so, in this
|
||||
// instance we take the foreground color and make it a background color
|
||||
if let Some(bg) = lead_trail_space_bg_color.foreground {
|
||||
bg_color_string = Style::default().on(bg).prefix().to_string()
|
||||
};
|
||||
|
||||
let re_leading =
|
||||
regex::Regex::new(r"(?P<beginsp>^\s+)").expect("error with leading space regex");
|
||||
if let Some(leading_match) = re_leading.find(¤t_line.clone()) {
|
||||
String::insert_str(&mut current_line, leading_match.end(), "\x1b[0m");
|
||||
String::insert_str(&mut current_line, leading_match.start(), &bg_color_string);
|
||||
}
|
||||
|
||||
let re_trailing =
|
||||
regex::Regex::new(r"(?P<endsp>\s+$)").expect("error with trailing space regex");
|
||||
if let Some(trailing_match) = re_trailing.find(¤t_line.clone()) {
|
||||
String::insert_str(&mut current_line, trailing_match.start(), &bg_color_string);
|
||||
current_line += "\x1b[0m";
|
||||
}
|
||||
|
||||
output.push(WrappedLine {
|
||||
line: current_line,
|
||||
width: current_line_width,
|
||||
|
Reference in New Issue
Block a user