dir_entry: Implement file_name() for broken symlinks

This commit is contained in:
Tavian Barnes 2022-10-25 11:16:31 -04:00 committed by David Peter
parent f0c50befce
commit b2c8888a50
1 changed files with 9 additions and 1 deletions

View File

@ -121,7 +121,15 @@ impl Colorable for DirEntry {
fn file_name(&self) -> OsString {
let name = match &self.inner {
DirEntryInner::Normal(e) => e.file_name(),
DirEntryInner::BrokenSymlink(_) => todo!(),
DirEntryInner::BrokenSymlink(path) => {
// Path::file_name() only works if the last component is Normal,
// but we want it for all component types, so we open code it.
// Copied from LsColors::style_for_path_with_metadata().
path.components()
.last()
.map(|c| c.as_os_str())
.unwrap_or_else(|| path.as_os_str())
}
};
name.to_owned()
}