Skip to content
Open
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
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ normpath = "1.1.1"
crossbeam-channel = "0.5.15"
clap_complete = {version = "4.6.0", optional = true}
faccess = "0.2.4"
jiff = "0.2.14"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we already have a dependency on jiff, why are you adding it again?

unix_mode = "0.1.4"
jiff = "0.2.18"

[dependencies.clap]
Expand Down
7 changes: 3 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,9 @@ pub struct Opts {
#[arg(long, overrides_with = "absolute_path", hide = true, action = ArgAction::SetTrue)]
relative_path: (),

/// Use a detailed listing format like 'ls -l'. This is basically an alias
/// for '--exec-batch ls -l' with some additional 'ls' options. This can be
/// used to see more metadata, to show symlink targets and to achieve a
/// deterministic sort order.
/// Use a detailed listing format like 'ls -l'. This can be used to see
/// more metadata, to show symlink targets and to achieve a deterministic
/// sort order.
#[arg(
long,
short = 'l',
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub struct Config {
/// Whether or not to use hyperlinks on paths
pub hyperlink: bool,

/// Whether or not to show a long listing format with file metadata
pub list_details: bool,
/// Names that should stop traversal down their parent. (e.g. https://bford.info/cachedir/).
pub ignore_contain: Vec<String>,
}
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
HyperlinkWhen::Never => false,
HyperlinkWhen::Auto => colored_output,
};
let command = extract_command(&mut opts, colored_output)?;
let command = extract_command(&mut opts)?;
let has_command = command.is_some();

let full_path_base = if opts.full_path {
Expand Down Expand Up @@ -335,6 +335,12 @@ fn construct_config(mut opts: Opts, pattern_regexps: &[String]) -> Result<Config
actual_path_separator,
max_results: opts.max_results(),
strip_cwd_prefix: opts.strip_cwd_prefix(|| !(opts.null_separator || has_command)),
list_details: opts.list_details,
})
}

fn extract_command(opts: &mut Opts) -> Result<Option<CommandSet>> {
opts.exec.command.take().map(Ok).transpose()
ignore_contain: opts.ignore_contain,
Comment on lines +342 to 344
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what happened here, but this isn't valid syntax, and is probably part of why the builds are failing.

})
}
Expand Down
103 changes: 102 additions & 1 deletion src/output.rs
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any overlap between these changes and #1866? Could we avoid duplicating logic for getting the additional information.

Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use std::borrow::Cow;
use std::io::{self, Write};
use std::time::SystemTime;

#[cfg(unix)]
use std::os::unix::fs::{MetadataExt, PermissionsExt};

use jiff::{Timestamp, tz::TimeZone};
use lscolors::{Indicator, LsColors, Style};
#[cfg(unix)]
use nix::unistd::{Gid, Group, Uid, User};

use crate::config::Config;
use crate::dir_entry::DirEntry;
Expand All @@ -22,7 +29,9 @@ pub fn print_entry<W: Write>(stdout: &mut W, entry: &DirEntry, config: &Config)
has_hyperlink = true;
}

if let Some(ref format) = config.format {
if config.list_details {
print_entry_details(stdout, entry, config, &config.ls_colors)?;
} else if let Some(ref format) = config.format {
print_entry_format(stdout, entry, config, format)?;
} else if let Some(ref ls_colors) = config.ls_colors {
print_entry_colorized(stdout, entry, config, ls_colors)?;
Expand Down Expand Up @@ -173,3 +182,95 @@ fn print_entry_uncolorized<W: Write>(
print_trailing_slash(stdout, entry, config, None)
}
}

fn format_size(size: u64) -> String {
if size < 1024 {
return format!("{} B", size);
}
let units = ["K", "M", "G", "T", "P", "E"];
let mut size = size as f64;
let mut unit_idx = 0;

size /= 1024.0;

while size >= 1024.0 && unit_idx < units.len() - 1 {
size /= 1024.0;
unit_idx += 1;
}

if size < 10.0 {
format!("{:.1} {}", size, units[unit_idx])
} else {
format!("{:.0} {}", size, units[unit_idx])
}
}

fn print_entry_details<W: Write>(
stdout: &mut W,
entry: &DirEntry,
config: &Config,
ls_colors: &Option<LsColors>,
) -> io::Result<()> {
let metadata = entry.metadata();

#[cfg(unix)]
let mode = metadata.map(|m| m.permissions().mode()).unwrap_or(0);
#[cfg(not(unix))]
let mode = 0;

let perms = unix_mode::to_string(mode);

#[cfg(unix)]
let nlink = metadata.map(|m| m.nlink()).unwrap_or(1);
#[cfg(not(unix))]
let nlink = 1;

#[cfg(unix)]
let (user, group) = {
let uid = metadata.map(|m| m.uid()).unwrap_or(0);
let gid = metadata.map(|m| m.gid()).unwrap_or(0);
let user = User::from_uid(Uid::from_raw(uid))
.ok()
.flatten()
.map(|u| u.name)
.unwrap_or_else(|| uid.to_string());
let group = Group::from_gid(Gid::from_raw(gid))
.ok()
.flatten()
.map(|g| g.name)
.unwrap_or_else(|| gid.to_string());
(user, group)
};
#[cfg(not(unix))]
let (user, group) = ("".to_string(), "".to_string());

let size = metadata.map(|m| m.len()).unwrap_or(0);
let size_str = format_size(size);

let time = metadata
.and_then(|m| m.modified().ok())
.unwrap_or(SystemTime::UNIX_EPOCH);
let timestamp = Timestamp::try_from(time).unwrap_or(Timestamp::UNIX_EPOCH);
let zoned = timestamp.to_zoned(TimeZone::system());
let date_str = zoned.strftime("%b %d %H:%M").to_string();

write!(
stdout,
"{} {:>3} {:>8} {:>8} {:>8} {} ",
perms, nlink, user, group, size_str, date_str
)?;

if let Some(ls_colors) = ls_colors {
print_entry_colorized(stdout, entry, config, ls_colors)?;
} else {
print_entry_uncolorized(stdout, entry, config)?;
}

if entry.file_type().map(|ft| ft.is_symlink()).unwrap_or(false)
&& let Ok(target) = std::fs::read_link(entry.path())
{
write!(stdout, " -> {}", target.to_string_lossy())?;
}

Ok(())
}
12 changes: 12 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2525,6 +2525,18 @@ fn test_list_details() {
te.assert_success_and_get_output(".", &["--list-details"]);
}

#[test]
fn test_list_details_format() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
create_file_with_size(te.test_root().join("size_test_100"), 100);

let output = te.assert_success_and_get_output(".", &["--list-details", "size_test_100"]);
let stdout = String::from_utf8_lossy(&output.stdout);

assert!(stdout.contains("100 B"));
assert!(stdout.trim().ends_with("size_test_100"));
}

#[test]
fn test_single_and_multithreaded_execution() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
Expand Down
Loading