-
Notifications
You must be signed in to change notification settings - Fork 126
list: show symlink targets (for tar and zip) #934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
f0dd6a4
632db3d
bd1898d
d972532
8313fd0
65cbd95
7ea0015
8b924ca
753288c
736ea65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,14 +16,22 @@ pub struct ListOptions { | |
| pub tree: bool, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub enum FileType { | ||
| File, | ||
| Directory, | ||
| Symlink { target: PathBuf }, | ||
| Hardlink { target: PathBuf }, | ||
| } | ||
|
|
||
| /// Represents a single file in an archive, used in `list::list_files()` | ||
| #[derive(Debug, Clone)] | ||
| pub struct FileInArchive { | ||
| /// The file path | ||
| pub path: PathBuf, | ||
|
|
||
| /// Whether this file is a directory | ||
| pub is_dir: bool, | ||
| /// The type of file | ||
| pub file_type: FileType, | ||
| } | ||
|
|
||
| /// Actually print the files | ||
|
|
@@ -41,40 +49,58 @@ pub fn list_files( | |
| tree.print(&mut out); | ||
| } else { | ||
| for file in files { | ||
| let FileInArchive { path, is_dir } = file?; | ||
| print_entry(&mut out, path.display(), is_dir); | ||
| let FileInArchive { path, file_type } = file?; | ||
| print_entry(&mut out, path.display(), &file_type); | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Print an entry and highlight directories, either by coloring them | ||
| /// if that's supported or by adding a trailing / | ||
| fn print_entry(out: &mut impl Write, name: impl fmt::Display, is_dir: bool) { | ||
| fn print_entry(out: &mut impl Write, name: impl fmt::Display, file_type: &FileType) { | ||
| use crate::utils::colors::*; | ||
|
|
||
| if !is_dir { | ||
| // Not a directory -> just print the file name | ||
| let _ = writeln!(out, "{name}"); | ||
| return; | ||
| } | ||
|
|
||
| // Handle directory display | ||
| let name_str = name.to_string(); | ||
| let display_name = name_str.strip_suffix('/').unwrap_or(&name_str); | ||
|
|
||
| let output = if BLUE.is_empty() { | ||
| // Colors are deactivated, print final / to mark directories | ||
| format!("{display_name}/") | ||
| } else if is_running_in_accessible_mode() { | ||
| // Accessible mode: use colors but print final / for screen readers | ||
| format!("{}{}{}/{}", *BLUE, *STYLE_BOLD, display_name, *ALL_RESET) | ||
| } else { | ||
| // Normal mode: use colors without trailing slash | ||
| format!("{}{}{}{}", *BLUE, *STYLE_BOLD, display_name, *ALL_RESET) | ||
| }; | ||
| match file_type { | ||
| FileType::File => { | ||
| let _ = writeln!(out, "{name}"); | ||
| } | ||
| FileType::Symlink { target } | FileType::Hardlink { target } => { | ||
| if is_running_in_accessible_mode() { | ||
| // Accessible mode: use "->" for screen readers | ||
| let _ = writeln!(out, "{} -> {}", name, target.display()); | ||
| } else { | ||
| // Normal mode: use "->" with colors | ||
| let _ = writeln!( | ||
| out, | ||
| "{}{}{} -> {}{}{}", | ||
|
||
| *CYAN, | ||
| name, | ||
| *ALL_RESET, | ||
| *CYAN, | ||
| target.display(), | ||
| *ALL_RESET | ||
| ); | ||
| } | ||
| } | ||
| FileType::Directory => { | ||
| let name_str = name.to_string(); | ||
| let display_name = name_str.strip_suffix('/').unwrap_or(&name_str); | ||
|
|
||
| let output = if BLUE.is_empty() { | ||
| // Colors are deactivated, print final / to mark directories | ||
| format!("{display_name}/") | ||
| } else if is_running_in_accessible_mode() { | ||
| // Accessible mode: use colors but print final / for screen readers | ||
| format!("{}{}{}/{}", *BLUE, *STYLE_BOLD, display_name, *ALL_RESET) | ||
| } else { | ||
| // Normal mode: use colors without trailing slash | ||
| format!("{}{}{}{}", *BLUE, *STYLE_BOLD, display_name, *ALL_RESET) | ||
| }; | ||
|
|
||
| let _ = writeln!(out, "{output}"); | ||
| let _ = writeln!(out, "{output}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Since archives store files as a list of entries -> without direct | ||
|
|
@@ -91,7 +117,7 @@ mod tree { | |
| use bstr::{ByteSlice, ByteVec}; | ||
| use linked_hash_map::LinkedHashMap; | ||
|
|
||
| use super::FileInArchive; | ||
| use super::{FileInArchive, FileType}; | ||
| use crate::{utils::PathFmt, warning}; | ||
|
|
||
| /// Directory tree | ||
|
|
@@ -151,11 +177,12 @@ mod tree { | |
| }; | ||
|
|
||
| let _ = write!(out, "{prefix}{final_part}"); | ||
| let is_dir = match self.file { | ||
| Some(FileInArchive { is_dir, .. }) => is_dir, | ||
| None => true, | ||
| let file_type = match &self.file { | ||
| Some(FileInArchive { file_type, .. }) => file_type.clone(), | ||
| // If we don't have a file entry but have children, it's an implicit directory | ||
| None => FileType::Directory, | ||
| }; | ||
| super::print_entry(out, <Vec<u8> as ByteVec>::from_os_str_lossy(name).as_bstr(), is_dir); | ||
| super::print_entry(out, <Vec<u8> as ByteVec>::from_os_str_lossy(name).as_bstr(), &file_type); | ||
|
|
||
| // Construct prefix for children, adding either a line if this isn't | ||
| // the last entry in the parent dir or empty space if it is. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we added
->, nowouch listcan't be used in scripts to provide a file list 🤔 .Can you add a check for, when
--quietis set but--treeisn't, we don't print out the arrow nor the target? This way you can stillouch list -q | xargs rm -ieven if there is a symlink in it.Let me know if you think there is a better and simpler approach, to allow for -> but keeping this compatible with scripts.