Skip to content

Commit

Permalink
feat: switch out ansiterm crate for nu_ansi_term
Browse files Browse the repository at this point in the history
Co-authored-by: Joey Sabey <[email protected]>
  • Loading branch information
2 people authored and cafkafk committed Mar 27, 2024
1 parent cb55827 commit 3338685
Show file tree
Hide file tree
Showing 31 changed files with 133 additions and 113 deletions.
27 changes: 10 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ name = "eza"


[dependencies]
ansi-width = "0.1.0"
ansiterm = { version = "0.12.2", features = ["ansi_colours"] }
nu-ansi-term = "0.49.0"
chrono = { version = "0.4.34", default-features = false, features = ["clock"] }
glob = "0.3"
libc = "0.2"
Expand All @@ -92,6 +91,7 @@ timeago = { version = "0.4.2", default-features = false }
unicode-width = "0.1"
zoneinfo_compiled = "0.5.1"
rayon = "1.10.0"
ansi-width = "0.1.0"

[dependencies.git2]
version = "0.18"
Expand Down
2 changes: 1 addition & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::ffi::OsStr;

use ansiterm::{ANSIString, Colour};
use nu_ansi_term::{AnsiString as ANSIString, Color as Colour};

/// Sets the internal logger, changing the log level based on the value of an
/// environment variable.
Expand Down
7 changes: 1 addition & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::io::{self, stdin, ErrorKind, IsTerminal, Read, Write};
use std::path::{Component, PathBuf};
use std::process::exit;

use ansiterm::{ANSIStrings, Style};
use nu_ansi_term::{AnsiStrings as ANSIStrings, Style};

use crate::fs::feature::git::GitCache;
use crate::fs::filter::GitIgnore;
Expand All @@ -53,11 +53,6 @@ fn main() {

logger::configure(env::var_os(vars::EZA_DEBUG).or_else(|| env::var_os(vars::EXA_DEBUG)));

#[cfg(windows)]
if let Err(e) = ansiterm::enable_ansi_support() {
warn!("Failed to enable ANSI support: {}", e);
}

let stdout_istty = io::stdout().is_terminal();

let mut input = String::new();
Expand Down
4 changes: 2 additions & 2 deletions src/output/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::iter::Sum;
use std::ops::{Add, Deref, DerefMut};

use ansiterm::{ANSIString, ANSIStrings, Style};
use nu_ansi_term::{AnsiString as ANSIString, AnsiStrings as ANSIStrings, Style};
use unicode_width::UnicodeWidthStr;

/// An individual cell that holds text in a table, used in the details and
Expand Down Expand Up @@ -158,7 +158,7 @@ impl TextCellContents {
pub fn width(&self) -> DisplayWidth {
self.0
.iter()
.map(|anstr| DisplayWidth::from(&**anstr))
.map(|anstr| DisplayWidth::from(anstr.as_str()))
.sum()
}

Expand Down
40 changes: 35 additions & 5 deletions src/output/color_scale.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ansiterm::{Colour, Style};
use log::trace;
use palette::{FromColor, Oklab, Srgb};
use nu_ansi_term::{Color as Colour, Style};
use palette::{FromColor, LinSrgb, Oklab, Srgb};

use crate::{
fs::{dir_action::RecurseOptions, feature::git::GitCache, fields::Size, DotFilter, File},
Expand Down Expand Up @@ -203,13 +203,43 @@ impl Extremes {
}

fn adjust_luminance(color: Colour, x: f32, min_l: f32) -> Colour {
let color = Srgb::from_components(color.into_rgb()).into_linear();
let rgb_color = match color {
Colour::Rgb(r, g, b) => LinSrgb::new(
f32::from(r) / 255.0,
f32::from(g) / 255.0,
f32::from(b) / 255.0,
),

let mut lab: Oklab = Oklab::from_color(color);
Colour::Black => LinSrgb::new(0.0, 0.0, 0.0),

Colour::Green | Colour::LightGreen => LinSrgb::new(0.0, 1.0, 0.0),

Colour::Yellow | Colour::LightYellow => LinSrgb::new(1.0, 1.0, 0.0),

Colour::Blue | Colour::LightBlue => LinSrgb::new(0.0, 0.0, 1.0),

Colour::Magenta | Colour::LightMagenta => LinSrgb::new(1.0, 0.0, 1.0),

Colour::Cyan | Colour::LightCyan => LinSrgb::new(0.0, 1.0, 1.0),

Colour::White => LinSrgb::new(1.0, 1.0, 1.0),

Colour::LightGray => LinSrgb::new(0.5, 0.5, 0.5),

Colour::LightRed | Colour::Red => LinSrgb::new(1.0, 0.0, 0.0),

Colour::DarkGray => LinSrgb::new(0.25, 0.25, 0.25),

Colour::LightPurple | Colour::Purple => LinSrgb::new(0.5, 0.0, 0.5),

_ => LinSrgb::new(1.0, 1.0, 1.0),
};

let mut lab: Oklab = Oklab::from_color(rgb_color);
lab.l = (min_l + (1.0 - min_l) * (-4.0 * (1.0 - x)).exp()).clamp(0.0, 1.0);

let adjusted_rgb: Srgb<f32> = Srgb::from_color(lab);
Colour::RGB(
Colour::Rgb(
(adjusted_rgb.red * 255.0).round() as u8,
(adjusted_rgb.green * 255.0).round() as u8,
(adjusted_rgb.blue * 255.0).round() as u8,
Expand Down
4 changes: 2 additions & 2 deletions src/output/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ use std::io::{self, Write};
use std::path::PathBuf;
use std::vec::IntoIter as VecIntoIter;

use ansiterm::Style;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use nu_ansi_term::Style;
use rayon::prelude::*;

use log::*;

Expand Down
2 changes: 1 addition & 1 deletion src/output/escape.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::file_name::QuoteStyle;
use ansiterm::{ANSIString, Style};
use nu_ansi_term::{AnsiString as ANSIString, Style};

pub fn escape(
string: String,
Expand Down
2 changes: 1 addition & 1 deletion src/output/file_name.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::path::Path;

use ansiterm::{ANSIString, Style};
use nu_ansi_term::{AnsiString as ANSIString, Style};
use path_clean;
use unicode_width::UnicodeWidthStr;

Expand Down
4 changes: 3 additions & 1 deletion src/output/grid_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use std::io::{self, Write};

use term_grid::{Direction, Filling, Grid, GridOptions};
use ansi_width;
use grid::{Direction, Filling, Grid, GridOptions};
use term_grid as grid;

use crate::fs::feature::git::GitCache;
use crate::fs::filter::FileFilter;
Expand Down
2 changes: 1 addition & 1 deletion src/output/icons.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::Style;
use nu_ansi_term::Style;
use phf::{phf_map, Map};

use crate::fs::File;
Expand Down
2 changes: 1 addition & 1 deletion src/output/lines.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{self, Write};

use ansiterm::ANSIStrings;
use nu_ansi_term::AnsiStrings as ANSIStrings;

use crate::fs::filter::FileFilter;
use crate::fs::File;
Expand Down
14 changes: 7 additions & 7 deletions src/output/render/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ansiterm::Style;
use locale::Numeric as NumericLocale;
use nu_ansi_term::Style;
use number_prefix::Prefix;

use crate::fs::fields as f;
Expand Down Expand Up @@ -72,8 +72,8 @@ pub trait Colours {

#[cfg(test)]
pub mod test {
use ansiterm::Colour::*;
use ansiterm::Style;
use nu_ansi_term::Color::*;
use nu_ansi_term::Style;

use super::Colours;
use crate::fs::fields as f;
Expand Down Expand Up @@ -103,7 +103,7 @@ pub mod test {
SizeFormat::JustBytes,
&NumericLocale::english()
)
)
);
}

#[test]
Expand All @@ -121,7 +121,7 @@ pub mod test {
SizeFormat::DecimalBytes,
&NumericLocale::english()
)
)
);
}

#[test]
Expand All @@ -139,7 +139,7 @@ pub mod test {
SizeFormat::BinaryBytes,
&NumericLocale::english()
)
)
);
}

#[test]
Expand All @@ -157,6 +157,6 @@ pub mod test {
SizeFormat::JustBytes,
&NumericLocale::english()
)
)
);
}
}
2 changes: 1 addition & 1 deletion src/output/render/filetype.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::{ANSIString, Style};
use nu_ansi_term::{AnsiString as ANSIString, Style};

use crate::fs::fields as f;

Expand Down
2 changes: 1 addition & 1 deletion src/output/render/flags.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::Style;
use nu_ansi_term::Style;

use crate::fs::fields as f;
use crate::output::cell::TextCell;
Expand Down
2 changes: 1 addition & 1 deletion src/output/render/flags_bsd.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::Style;
use nu_ansi_term::Style;
use std::ffi::CStr;

#[cfg(target_os = "netbsd")]
Expand Down
2 changes: 1 addition & 1 deletion src/output/render/flags_windows.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::fs::fields as f;
use crate::output::table::FlagsFormat;
use crate::output::TextCell;
use ansiterm::Style;
use nu_ansi_term::Style;

// See https://learn.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
const FILE_ATTRIBUTE_READONLY: u32 = 0x0000_0001; // R
Expand Down
10 changes: 5 additions & 5 deletions src/output/render/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::{ANSIString, Style};
use nu_ansi_term::{AnsiString as ANSIString, Style};

use crate::fs::fields as f;
use crate::output::cell::{DisplayWidth, TextCell};
Expand Down Expand Up @@ -98,8 +98,8 @@ pub mod test {
use crate::fs::fields as f;
use crate::output::cell::{DisplayWidth, TextCell};

use ansiterm::Colour::*;
use ansiterm::Style;
use nu_ansi_term::Color::*;
use nu_ansi_term::Style;

struct TestColours;

Expand Down Expand Up @@ -142,7 +142,7 @@ pub mod test {
contents: vec![Fixed(90).paint("-"), Fixed(90).paint("-")].into(),
};

assert_eq!(expected, stati.render(&TestColours))
assert_eq!(expected, stati.render(&TestColours));
}

#[test]
Expand All @@ -157,6 +157,6 @@ pub mod test {
contents: vec![Fixed(91).paint("N"), Fixed(92).paint("M")].into(),
};

assert_eq!(expected, stati.render(&TestColours))
assert_eq!(expected, stati.render(&TestColours));
}
}
6 changes: 3 additions & 3 deletions src/output/render/groups.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::Style;
use nu_ansi_term::Style;
use uzers::{Groups, Users};

use crate::fs::fields as f;
Expand Down Expand Up @@ -85,8 +85,8 @@ pub mod test {
use crate::output::cell::TextCell;
use crate::output::table::{GroupFormat, UserFormat};

use ansiterm::Colour::*;
use ansiterm::Style;
use nu_ansi_term::Color::*;
use nu_ansi_term::Style;
use uzers::mock::MockUsers;
use uzers::os::unix::GroupExt;
use uzers::{Group, User};
Expand Down
4 changes: 2 additions & 2 deletions src/output/render/inode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ansiterm::Style;
use nu_ansi_term::Style;

use crate::fs::fields as f;
use crate::output::cell::TextCell;
Expand All @@ -14,7 +14,7 @@ pub mod test {
use crate::fs::fields as f;
use crate::output::cell::TextCell;

use ansiterm::Colour::*;
use nu_ansi_term::Color::*;

#[test]
fn blocklessness() {
Expand Down
6 changes: 3 additions & 3 deletions src/output/render/links.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ansiterm::Style;
#[cfg(unix)]
use locale::Numeric as NumericLocale;
use nu_ansi_term::Style;

#[cfg(unix)]
use crate::fs::fields as f;
Expand Down Expand Up @@ -33,10 +33,10 @@ pub mod test {
#[cfg(unix)]
use crate::output::cell::{DisplayWidth, TextCell};

use ansiterm::Colour::*;
use ansiterm::Style;
#[cfg(unix)]
use locale;
use nu_ansi_term::Color::*;
use nu_ansi_term::Style;

struct TestColours;

Expand Down
Loading

0 comments on commit 3338685

Please sign in to comment.