Skip to content

Commit b5a1194

Browse files
joknarfAlonely0
andcommitted
fix(ls): take care of performance regressions
Co-authored-by: Guillem L. Jara <4lon3ly0@tutanota.com>
1 parent e6fd64c commit b5a1194

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

src/uu/ls/src/config.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub struct Config {
172172
// Dir and vdir needs access to this field
173173
pub quoting_style: QuotingStyle,
174174
pub(crate) locale_quoting: Option<LocaleQuoting>,
175-
pub(crate) indicator_style: IndicatorStyle,
175+
pub(crate) indicator_style: Option<IndicatorStyle>,
176176
pub(crate) time_format_recent: String, // Time format for recent dates
177177
pub(crate) time_format_older: Option<String>, // Time format for older dates (optional, if not present, time_format_recent is used)
178178
pub(crate) context: bool,
@@ -554,34 +554,28 @@ fn extract_quoting_style(
554554
/// # Returns
555555
///
556556
/// An [`IndicatorStyle`] variant representing the indicator style to use.
557-
fn extract_indicator_style(options: &clap::ArgMatches) -> IndicatorStyle {
557+
fn extract_indicator_style(options: &clap::ArgMatches) -> Option<IndicatorStyle> {
558558
if let Some(field) = options.get_one::<String>(options::INDICATOR_STYLE) {
559559
match field.as_str() {
560-
"none" => IndicatorStyle::None,
561-
"file-type" => IndicatorStyle::FileType,
562-
"classify" => IndicatorStyle::Classify,
563-
"slash" => IndicatorStyle::Slash,
564-
&_ => IndicatorStyle::None,
560+
"none" => None,
561+
"file-type" => Some(IndicatorStyle::FileType),
562+
"classify" => Some(IndicatorStyle::Classify),
563+
"slash" => Some(IndicatorStyle::Slash),
564+
&_ => None,
565565
}
566566
} else if let Some(field) = options.get_one::<String>(options::indicator_style::CLASSIFY) {
567567
match field.as_str() {
568-
"never" | "no" | "none" => IndicatorStyle::None,
569-
"always" | "yes" | "force" => IndicatorStyle::Classify,
570-
"auto" | "tty" | "if-tty" => {
571-
if stdout().is_terminal() {
572-
IndicatorStyle::Classify
573-
} else {
574-
IndicatorStyle::None
575-
}
576-
}
577-
&_ => IndicatorStyle::None,
568+
"never" | "no" | "none" => None,
569+
"always" | "yes" | "force" => Some(IndicatorStyle::Classify),
570+
"auto" | "tty" | "if-tty" => stdout().is_terminal().then_some(IndicatorStyle::Classify),
571+
&_ => None,
578572
}
579573
} else if options.get_flag(options::indicator_style::SLASH) {
580-
IndicatorStyle::Slash
574+
Some(IndicatorStyle::Slash)
581575
} else if options.get_flag(options::indicator_style::FILE_TYPE) {
582-
IndicatorStyle::FileType
576+
Some(IndicatorStyle::FileType)
583577
} else {
584-
IndicatorStyle::None
578+
None
585579
}
586580
}
587581

@@ -954,7 +948,7 @@ impl Config {
954948
} else if options.get_flag(options::dereference::DIR_ARGS) {
955949
Dereference::DirArgs
956950
} else if options.get_flag(options::DIRECTORY)
957-
|| indicator_style == IndicatorStyle::Classify
951+
|| indicator_style == Some(IndicatorStyle::Classify)
958952
|| format == Format::Long
959953
{
960954
Dereference::None

src/uu/ls/src/display.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ pub(crate) struct DisplayItemName {
9595

9696
#[derive(Clone, Copy, PartialEq, Eq)]
9797
pub(crate) enum IndicatorStyle {
98-
None,
9998
Slash,
10099
FileType,
101100
Classify,
@@ -733,14 +732,14 @@ fn display_item_name(
733732
// This is because relative symlinks will fail to get_metadata.
734733
let absolute_target = if target_path.is_relative() {
735734
match path.path().parent() {
736-
Some(p) => p.join(&target_path),
737-
None => target_path.clone(),
735+
Some(p) => &p.join(&target_path),
736+
None => &target_path,
738737
}
739738
} else {
740-
target_path.clone()
739+
&target_path
741740
};
742741

743-
match fs::canonicalize(&absolute_target) {
742+
match fs::canonicalize(absolute_target) {
744743
Ok(resolved_target) => {
745744
let target_data = PathData::new(
746745
resolved_target.as_path().into(),
@@ -750,27 +749,32 @@ fn display_item_name(
750749
false,
751750
);
752751

753-
let mut target_display = escaped_target.clone();
754-
if let Some(style_manager) = &mut style_manager {
755-
let md_option: Option<Metadata> = target_data
756-
.metadata()
757-
.cloned()
758-
.or_else(|| target_data.p_buf.symlink_metadata().ok());
759-
752+
let target_display = if let Some(style_manager) = style_manager {
753+
let md = match target_data.metadata() {
754+
Some(md) => Some(Cow::Borrowed(md)),
755+
None => target_data.p_buf.symlink_metadata().ok().map(Cow::Owned),
756+
};
757+
// Check if the target actually needs coloring
760758
if style_manager
761759
.colors
762-
.style_for_path_with_metadata(&target_data.p_buf, md_option.as_ref())
760+
.style_for_path_with_metadata(&target_data.p_buf, md.as_deref())
763761
.is_some()
764762
{
765-
target_display = color_name(
766-
escaped_target.clone(),
763+
// Only apply coloring if there's actually a style
764+
&color_name(
765+
escaped_target,
767766
&target_data,
768767
style_manager,
769768
None,
770769
is_wrap(name.len()),
771-
);
770+
)
771+
} else {
772+
// For regular files with no coloring, just use plain text
773+
&escaped_target
772774
}
773-
}
775+
} else {
776+
&escaped_target
777+
};
774778

775779
name.push(target_display);
776780
if let Some(c) = indicator_char(&target_data, config.indicator_style) {
@@ -786,6 +790,8 @@ fn display_item_name(
786790
),
787791
);
788792
} else {
793+
// If no coloring is required, we just use target as is.
794+
// with the right quoting
789795
name.push(escaped_target);
790796
}
791797
}
@@ -1103,7 +1109,8 @@ fn display_item_long(
11031109
Ok(())
11041110
}
11051111

1106-
fn indicator_char(path: &PathData, style: IndicatorStyle) -> Option<char> {
1112+
fn indicator_char(path: &PathData, style: Option<IndicatorStyle>) -> Option<char> {
1113+
let style = style?;
11071114
let sym = classify_file(path);
11081115

11091116
match style {
@@ -1116,7 +1123,6 @@ fn indicator_char(path: &PathData, style: IndicatorStyle) -> Option<char> {
11161123
Some('/') => Some('/'),
11171124
_ => None,
11181125
},
1119-
IndicatorStyle::None => None,
11201126
}
11211127
}
11221128

0 commit comments

Comments
 (0)