Skip to content

Commit 860d755

Browse files
CopilotKSXGitHub
andcommitted
refactor: address review threads 71-75: remove Hash+Eq bounds, rename ansi_prefixes to ls_colors, update doc comment, make lscolors optional under color feature
Co-authored-by: KSXGitHub <11488886+KSXGitHub@users.noreply.github.com>
1 parent 1d3a5d3 commit 860d755

6 files changed

Lines changed: 37 additions & 16 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ required-features = ["cli"]
5050

5151
[features]
5252
default = ["cli"]
53+
color = ["dep:lscolors"]
5354
json = ["serde/derive", "serde_json"]
54-
cli = ["clap/derive", "clap_complete", "clap-utilities", "json"]
55+
cli = ["color", "clap/derive", "clap_complete", "clap-utilities", "json"]
5556
cli-completions = ["cli"]
5657

5758
[dependencies]
@@ -70,7 +71,7 @@ rayon = "1.10.0"
7071
rounded-div = "0.1.4"
7172
serde = { version = "1.0.228", optional = true }
7273
serde_json = { version = "1.0.149", optional = true }
73-
lscolors = { version = "0.21", features = ["nu-ansi-term"] }
74+
lscolors = { version = "0.21", features = ["nu-ansi-term"], optional = true }
7475
smart-default = "0.7.1"
7576
sysinfo = "0.38.2"
7677
terminal_size = "0.4.3"

src/ls_colors.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
use crate::visualizer::coloring::Color;
2+
#[cfg(feature = "color")]
23
use lscolors::{self, Indicator};
34

45
/// ANSI color prefix strings for terminal output, initialized from the `LS_COLORS` environment
56
/// variable.
67
#[derive(Debug, Clone)]
78
pub struct LsColors {
9+
#[cfg(feature = "color")]
810
directory: String,
11+
#[cfg(feature = "color")]
912
normal: String,
13+
#[cfg(feature = "color")]
1014
executable: String,
15+
#[cfg(feature = "color")]
1116
symlink: String,
1217
}
1318

1419
impl LsColors {
1520
/// Initialize by reading the current environment's `LS_COLORS`.
1621
pub fn from_env() -> Self {
17-
Self::from_ls_colors_crate(&lscolors::LsColors::from_env().unwrap_or_default())
22+
#[cfg(feature = "color")]
23+
return Self::from_ls_colors_crate(&lscolors::LsColors::from_env().unwrap_or_default());
24+
#[cfg(not(feature = "color"))]
25+
LsColors {}
1826
}
1927

2028
/// Parse an `LS_COLORS`-format string into an [`LsColors`].
2129
///
2230
/// Unrecognized or invalid entries are silently ignored.
2331
pub fn from_ls_colors_string(input: &str) -> Self {
24-
Self::from_ls_colors_crate(&lscolors::LsColors::from_string(input))
32+
#[cfg(feature = "color")]
33+
return Self::from_ls_colors_crate(&lscolors::LsColors::from_string(input));
34+
#[cfg(not(feature = "color"))]
35+
{
36+
let _ = input;
37+
LsColors {}
38+
}
2539
}
2640

41+
#[cfg(feature = "color")]
2742
fn from_ls_colors_crate(ls_colors: &lscolors::LsColors) -> Self {
2843
let prefix_for = |indicator: Indicator| {
2944
ls_colors
@@ -41,11 +56,17 @@ impl LsColors {
4156

4257
/// Return the ANSI prefix string for the given [`Color`] variant.
4358
pub(crate) fn prefix_str(&self, color: Color) -> &str {
44-
match color {
59+
#[cfg(feature = "color")]
60+
return match color {
4561
Color::Directory => &self.directory,
4662
Color::Normal => &self.normal,
4763
Color::Executable => &self.executable,
4864
Color::Symlink => &self.symlink,
65+
};
66+
#[cfg(not(feature = "color"))]
67+
{
68+
let _ = color;
69+
""
4970
}
5071
}
5172
}

src/visualizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where
6161
pub bar_alignment: BarAlignment,
6262
/// Distribution and total number of characters/blocks can be placed in a line.
6363
pub column_width_distribution: ColumnWidthDistribution,
64-
/// Mapping of names to colors for colorful output.
64+
/// Optional coloring configuration for colorful output, mapping full node paths to colors.
6565
pub coloring: Option<&'a Coloring>,
6666
}
6767

src/visualizer/coloring.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ use zero_copy_pads::Width;
1111
/// Coloring configuration: ANSI prefix strings from the environment and a full-path-to-color map.
1212
#[derive(Debug)]
1313
pub struct Coloring {
14-
ansi_prefixes: LsColors,
14+
ls_colors: LsColors,
1515
map: HashMap<PathBuf, Color>,
1616
}
1717

1818
impl Coloring {
19-
/// Create a new [`Coloring`] from ANSI prefixes and a full-path-to-color map.
20-
pub fn new(ansi_prefixes: LsColors, map: HashMap<PathBuf, Color>) -> Self {
21-
Coloring { ansi_prefixes, map }
19+
/// Create a new [`Coloring`] from LS_COLORS prefixes and a full-path-to-color map.
20+
pub fn new(ls_colors: LsColors, map: HashMap<PathBuf, Color>) -> Self {
21+
Coloring { ls_colors, map }
2222
}
2323

24-
/// Return `(color, prefixes)` for a node, used to build a colored slice for rendering.
24+
/// Return `(color, ls_colors)` for a node, used to build a colored slice for rendering.
2525
pub(super) fn node_color(&self, path: &Path, has_children: bool) -> Option<(Color, &LsColors)> {
2626
let color = if has_children {
2727
Some(Color::Directory)
2828
} else {
2929
self.map.get(path).copied()
3030
}?;
31-
Some((color, &self.ansi_prefixes))
31+
Some((color, &self.ls_colors))
3232
}
3333
}
3434

src/visualizer/display.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ use crate::size;
33
use std::{
44
ffi::OsStr,
55
fmt::{Display, Error, Formatter},
6-
hash::Hash,
76
};
87

98
impl<'a, Name, Size> Display for Visualizer<'a, Name, Size>
109
where
11-
Name: Display + Hash + Eq + AsRef<OsStr>,
10+
Name: Display + AsRef<OsStr>,
1211
Size: size::Size + Into<u64>,
1312
{
1413
/// Create the ASCII chart.

src/visualizer/methods.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ use tree_table::*;
1414

1515
use super::{coloring::ColoredTreeHorizontalSlice, ColumnWidthDistribution, Visualizer};
1616
use crate::size;
17-
use std::{cmp::min, ffi::OsStr, fmt::Display, hash::Hash, path::PathBuf};
17+
use std::{cmp::min, ffi::OsStr, fmt::Display, path::PathBuf};
1818
use zero_copy_pads::{align_left, align_right};
1919

2020
impl<'a, Name, Size> Visualizer<'a, Name, Size>
2121
where
22-
Name: Display + Hash + Eq + AsRef<OsStr>,
22+
Name: Display + AsRef<OsStr>,
2323
Size: size::Size + Into<u64>,
2424
{
2525
/// Create ASCII rows that visualize the [tree](crate::data_tree::DataTree), such rows

0 commit comments

Comments
 (0)