mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 06:30:08 +02:00
respect lscolors env var; measure width minus ansi
This commit is contained in:
@ -17,5 +17,6 @@ nu-term-grid = { path = "../nu-term-grid" }
|
||||
glob = "0.3.0"
|
||||
thiserror = "1.0.29"
|
||||
sysinfo = "0.20.4"
|
||||
chrono = { version="0.4.19", features=["serde"] }
|
||||
chrono = { version = "0.4.19", features = ["serde"] }
|
||||
terminal_size = "0.1.17"
|
||||
lscolors = { version = "0.8.0", features = ["crossterm"] }
|
||||
|
@ -1,3 +1,4 @@
|
||||
use lscolors::{LsColors, Style};
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::{
|
||||
ast::{Call, PathMember},
|
||||
@ -88,6 +89,7 @@ fn create_grid_output2(
|
||||
call: &Call,
|
||||
columns_param: Option<String>,
|
||||
) -> Value {
|
||||
let ls_colors = LsColors::from_env().unwrap_or_default();
|
||||
let mut grid = Grid::new(GridOptions {
|
||||
direction: Direction::TopToBottom,
|
||||
filling: Filling::Text(" | ".into()),
|
||||
@ -96,7 +98,9 @@ fn create_grid_output2(
|
||||
for (_row_index, header, value) in items {
|
||||
// only output value if the header name is 'name'
|
||||
if header == "name" {
|
||||
let mut cell = Cell::from(value);
|
||||
let style = ls_colors.style_for_path(value.clone());
|
||||
let ansi_style = style.map(Style::to_crossterm_style).unwrap_or_default();
|
||||
let mut cell = Cell::from(ansi_style.apply(value).to_string());
|
||||
cell.alignment = Alignment::Right;
|
||||
grid.add(cell);
|
||||
}
|
||||
|
@ -13,3 +13,4 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
unicode-width = "0.1.9"
|
||||
strip-ansi-escapes = "0.1.1"
|
||||
|
@ -94,10 +94,21 @@
|
||||
use std::cmp::max;
|
||||
use std::fmt;
|
||||
use std::iter::repeat;
|
||||
|
||||
// extern crate unicode_width;
|
||||
use strip_ansi_escapes::strip;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
fn unicode_width_strip_ansi(astring: &str) -> usize {
|
||||
let stripped_string: String = {
|
||||
if let Ok(bytes) = strip(astring) {
|
||||
String::from_utf8_lossy(&bytes).to_string()
|
||||
} else {
|
||||
astring.to_string()
|
||||
}
|
||||
};
|
||||
|
||||
UnicodeWidthStr::width(&stripped_string[..])
|
||||
}
|
||||
|
||||
/// Alignment indicate on which side the content should stick if some filling
|
||||
/// is required.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@ -129,7 +140,7 @@ pub struct Cell {
|
||||
impl From<String> for Cell {
|
||||
fn from(string: String) -> Self {
|
||||
Self {
|
||||
width: UnicodeWidthStr::width(&*string),
|
||||
width: unicode_width_strip_ansi(&*string),
|
||||
contents: string,
|
||||
alignment: Alignment::Left,
|
||||
}
|
||||
@ -139,7 +150,7 @@ impl From<String> for Cell {
|
||||
impl<'a> From<&'a str> for Cell {
|
||||
fn from(string: &'a str) -> Self {
|
||||
Self {
|
||||
width: UnicodeWidthStr::width(&*string),
|
||||
width: unicode_width_strip_ansi(&*string),
|
||||
contents: string.into(),
|
||||
alignment: Alignment::Left,
|
||||
}
|
||||
@ -177,7 +188,7 @@ impl Filling {
|
||||
fn width(&self) -> Width {
|
||||
match *self {
|
||||
Filling::Spaces(w) => w,
|
||||
Filling::Text(ref t) => UnicodeWidthStr::width(&t[..]),
|
||||
Filling::Text(ref t) => unicode_width_strip_ansi(&t[..]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user