Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/uu/ls/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ fn display_item_name(
target_path.file_name().map(Cow::Borrowed),
config,
false,
false,
);

// Check if the target actually needs coloring
Expand Down
27 changes: 20 additions & 7 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ pub struct PathData<'a> {
p_buf: Cow<'a, Path>,
must_dereference: bool,
command_line: bool,
is_dot_dir: bool,
}

impl<'a> PathData<'a> {
Expand All @@ -838,6 +839,7 @@ impl<'a> PathData<'a> {
file_name: Option<Cow<'a, OsStr>>,
config: &Config,
command_line: bool,
is_dot_dir: bool,
) -> Self {
// We cannot use `Path::ends_with` or `Path::Components`, because they remove occurrences of '.'
// For '..', the filename is None
Expand Down Expand Up @@ -904,6 +906,7 @@ impl<'a> PathData<'a> {
p_buf,
must_dereference,
command_line,
is_dot_dir,
}
}

Expand Down Expand Up @@ -976,6 +979,15 @@ impl<'a> PathData<'a> {
PathDataDisplayName::Custom(ref cow) => cow,
}
}

fn file_name(&self) -> &OsStr {
match self.display_name {
PathDataDisplayName::SelfReferential => {
self.p_buf.file_name().unwrap_or(self.p_buf.as_os_str())
}
PathDataDisplayName::Custom(ref cow) => cow,
}
}
}

impl Colorable for PathData<'_> {
Expand Down Expand Up @@ -1164,7 +1176,7 @@ pub fn list_with_output<O: LsOutput>(
let initial_locs_len = locs.len();

for loc in locs {
let path_data = PathData::new(loc.into(), None, None, config, true);
let path_data = PathData::new(loc.into(), None, None, config, true, false);

// Getting metadata here is no big deal as it's just the CWD
// and we really just want to know if the strings exist as files/dirs
Expand Down Expand Up @@ -1278,13 +1290,15 @@ fn collect_directory_entries<O: LsOutput>(
Some(OsStr::new(".").into()),
config,
false,
true,
));
entries.push(PathData::new(
dotdot_path(path_data.path()).into(),
None,
Some(OsStr::new("..").into()),
config,
false,
true,
));
}

Expand All @@ -1305,6 +1319,7 @@ fn collect_directory_entries<O: LsOutput>(
None,
config,
false,
false,
));
}
}
Expand Down Expand Up @@ -1375,6 +1390,7 @@ fn enter_directory<O: LsOutput>(
None,
config,
entry.command_line,
false,
);

if !entry.is_first {
Expand Down Expand Up @@ -1404,12 +1420,9 @@ fn enter_directory<O: LsOutput>(
write_directory_entries(entries, config, output)?;

if config.recursive {
let start = if config.files == Files::All { 2 } else { 0 };

for child in entries
.iter()
.skip(start)
.filter(|p| p.file_type().is_some_and(FileType::is_dir))
.filter(|p| p.file_type().is_some_and(FileType::is_dir) && !p.is_dot_dir)
.rev()
{
let child_path = child.path().to_path_buf();
Expand Down Expand Up @@ -1473,8 +1486,8 @@ fn sort_entries(entries: &mut [PathData], config: &Config) {
Sort::Name => entries.sort_unstable_by(|a, b| a.display_name().cmp(b.display_name())),
Sort::Version => entries.sort_unstable_by(|a, b| {
version_cmp(
os_str_as_bytes_lossy(a.path().as_os_str()).as_ref(),
os_str_as_bytes_lossy(b.path().as_os_str()).as_ref(),
os_str_as_bytes_lossy(a.file_name()).as_ref(),
os_str_as_bytes_lossy(b.file_name()).as_ref(),
)
.then(a.path().cmp(b.path()))
}),
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3661,7 +3661,7 @@ fn test_ls_version_sort() {
);

let result = scene.ucmd().arg("-a1v").succeeds();
expected.insert(expected.len() - 1, "..");
expected.insert(0, "..");
expected.insert(0, ".");
assert_eq!(
result.stdout_str().split('\n').collect::<Vec<_>>(),
Expand Down
Loading