Skip to content

Commit 5876450

Browse files
committed
fix: display theme icons with --color=never option
Previously, when using --color=never, custom icons from theme.yml were not displayed because the icon rendering was coupled with color styling logic. This change separates icon display from color styling by: - Always applying icon glyphs from theme configuration - Only applying color styles when colors are enabled - Preserving existing behavior for all other cases Fixes the issue where --color=never would suppress themed icons while still maintaining proper color control functionality. Tested with custom theme.yml containing file extension and filename icon mappings - icons now display correctly with --color=never.
1 parent 97f9f36 commit 5876450

File tree

2 files changed

+64
-20
lines changed

2 files changed

+64
-20
lines changed

src/theme/mod.rs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,31 @@ pub struct Theme {
6969
impl Options {
7070
#[must_use]
7171
pub fn to_theme(&self, isatty: bool) -> Theme {
72-
if self.use_colours == UseColours::Never
73-
|| (self.use_colours == UseColours::Automatic && !isatty)
74-
{
75-
let ui = UiStyles::plain();
76-
let exts = Box::new(NoFileStyle);
77-
return Theme { ui, exts };
78-
}
72+
let use_colors = self.use_colours != UseColours::Never
73+
&& (self.use_colours != UseColours::Automatic || isatty);
7974

8075
#[cfg(windows)]
81-
if nu_ansi_term::enable_ansi_support().is_err() {
82-
// Failed to enable ansi support, probably because legacy mode console.
83-
// No need to alert the user unless they explicitly set color=always
84-
if self.use_colours == UseColours::Always {
85-
eprintln!("eza: Ignoring option color=always in legacy console.");
76+
let use_colors = use_colors && {
77+
if nu_ansi_term::enable_ansi_support().is_err() {
78+
// Failed to enable ansi support, probably because legacy mode console.
79+
// No need to alert the user unless they explicitly set color=always
80+
if self.use_colours == UseColours::Always {
81+
eprintln!("eza: Ignoring option color=always in legacy console.");
82+
}
83+
false
84+
} else {
85+
true
8686
}
87-
let ui = UiStyles::plain();
88-
let exts = Box::new(NoFileStyle);
89-
return Theme { ui, exts };
90-
}
87+
};
9188

9289
match self.theme_config {
9390
Some(ref theme) => {
9491
if let Some(mut ui) = theme.to_theme() {
92+
// If colors are disabled, strip color information but keep other styling
93+
if !use_colors {
94+
ui = ui.plain_colors();
95+
}
96+
9597
let (exts, use_default_filetypes) = self.definitions.parse_color_vars(&mut ui);
9698
let exts: Box<dyn FileStyle> =
9799
match (exts.is_non_empty(), use_default_filetypes) {
@@ -102,14 +104,18 @@ impl Options {
102104
};
103105
return Theme { ui, exts };
104106
}
105-
self.default_theme()
107+
self.default_theme_with_colors(use_colors)
106108
}
107-
None => self.default_theme(),
109+
None => self.default_theme_with_colors(use_colors),
108110
}
109111
}
110112

111-
fn default_theme(&self) -> Theme {
112-
let mut ui = UiStyles::default_theme(self.colour_scale);
113+
fn default_theme_with_colors(&self, use_colors: bool) -> Theme {
114+
let mut ui = if use_colors {
115+
UiStyles::default_theme(self.colour_scale)
116+
} else {
117+
UiStyles::plain()
118+
};
113119
let (exts, use_default_filetypes) = self.definitions.parse_color_vars(&mut ui);
114120
let exts: Box<dyn FileStyle> = match (exts.is_non_empty(), use_default_filetypes) {
115121
(false, false) => Box::new(NoFileStyle),

src/theme/ui_styles.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,44 @@ impl UiStyles {
505505
extensions: None,
506506
}
507507
}
508+
509+
/// Creates a version of this `UiStyles` with colors stripped but preserving
510+
/// icon configurations and other non-color styling
511+
#[must_use]
512+
pub fn plain_colors(mut self) -> Self {
513+
// Set the theme as non-colorful
514+
self.colourful = Some(false);
515+
516+
// Strip colors from filename and extension styles but preserve icons
517+
if let Some(ref mut filenames) = self.filenames {
518+
for style in filenames.values_mut() {
519+
if let Some(ref mut filename_style) = style.filename {
520+
*filename_style = Style::default();
521+
}
522+
// Keep icon configurations unchanged
523+
}
524+
}
525+
526+
if let Some(ref mut extensions) = self.extensions {
527+
for style in extensions.values_mut() {
528+
if let Some(ref mut filename_style) = style.filename {
529+
*filename_style = Style::default();
530+
}
531+
// Keep icon configurations unchanged
532+
}
533+
}
534+
535+
// Create a plain version for all other styles while preserving the structure
536+
let plain_base = UiStyles::plain();
537+
538+
// Keep only the icon-related fields from self, everything else from plain
539+
UiStyles {
540+
colourful: Some(false),
541+
filenames: self.filenames,
542+
extensions: self.extensions,
543+
..plain_base
544+
}
545+
}
508546
}
509547

510548
impl UiStyles {

0 commit comments

Comments
 (0)