Skip to content
Open
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
67 changes: 41 additions & 26 deletions src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2582,7 +2582,7 @@ fn display_dir_entry_size(
}
}

// A simple, performant, ExtendPad trait to add a string to a Vec<u8>, padding with spaces
// A simple, performant, ExtendPad trait to append a string to a Vec<u8> or String, padding with spaces
// on the left or right, without making additional copies, or using formatting functions.
trait ExtendPad {
fn extend_pad_left(&mut self, string: &str, count: usize);
Expand All @@ -2605,6 +2605,22 @@ impl ExtendPad for Vec<u8> {
}
}

impl ExtendPad for String {
Copy link
Contributor

Choose a reason for hiding this comment

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

please add comments explaining why you are doing this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

comments are already there for this trait -- updated it

fn extend_pad_left(&mut self, string: &str, count: usize) {
if string.len() < count {
self.extend(iter::repeat_n(' ', count - string.len()));
}
self.push_str(string);
}

fn extend_pad_right(&mut self, string: &str, count: usize) {
self.push_str(string);
if string.len() < count {
self.extend(iter::repeat_n(' ', count - string.len()));
}
}
}

// TODO: Consider converting callers to use ExtendPad instead, as it avoids
// additional copies.
fn pad_left(string: &str, count: usize) -> String {
Expand Down Expand Up @@ -2643,26 +2659,28 @@ fn display_additional_leading_info(
{
if config.inode {
let i = if let Some(md) = item.metadata() {
get_inode(md)
&get_inode(md)
} else {
"?".to_owned()
"?"
};
write!(result, "{} ", pad_left(&i, padding.inode)).unwrap();
result.extend_pad_left(i, padding.inode);
result.push(' ');
}
}

if config.alloc_size {
let s = if let Some(md) = item.metadata() {
display_size(get_block_size(md, config), config)
&display_size(get_block_size(md, config), config)
} else {
"?".to_owned()
"?"
};
// extra space is insert to align the sizes, as needed for all formats, except for the comma format.
if config.format == Format::Commas {
write!(result, "{s} ").unwrap();
result.push_str(s);
} else {
write!(result, "{} ", pad_left(&s, padding.block_size)).unwrap();
result.extend_pad_left(s, padding.block_size);
}
result.push(' ');
}
Ok(result)
}
Expand Down Expand Up @@ -3372,7 +3390,7 @@ fn display_item_name(
};

if let Some(c) = char_opt {
name.push(OsStr::new(&c.to_string()));
name.write_char(c).unwrap();
}
}

Expand Down Expand Up @@ -3444,14 +3462,13 @@ fn display_item_name(
// to get correct alignment from later calls to`display_grid()`.
if config.context {
if let Some(pad_count) = prefix_context {
let security_context = if matches!(config.format, Format::Commas) {
path.security_context(config).to_string()
let old_name = name;
name = if matches!(config.format, Format::Commas) {
path.security_context(config).into()
} else {
pad_left(path.security_context(config), pad_count)
pad_left(path.security_context(config), pad_count).into()
};

let old_name = name;
name = format!("{security_context} ").into();
name.push(" ");
name.push(old_name);
}
}
Expand All @@ -3472,19 +3489,17 @@ fn create_hyperlink(name: &OsStr, path: &PathData) -> OsString {
let unencoded_chars = "_-.:~/\\";

// percentage encoding of path
let absolute_path: String = absolute_path
.chars()
.map(|c| {
if c.is_alphanumeric() || unencoded_chars.contains(c) {
c.to_string()
} else {
format!("%{:02x}", c as u8)
}
})
.collect();
let mut absolute_path_encoded = String::with_capacity(absolute_path.len() * 2);
for c in absolute_path.chars() {
if c.is_alphanumeric() || unencoded_chars.contains(c) {
absolute_path_encoded.push(c);
} else {
let _ = write!(&mut absolute_path_encoded, "%{:02x}", c as u8);
}
}

// \x1b = ESC, \x07 = BEL
let mut ret: OsString = format!("\x1b]8;;file://{hostname}{absolute_path}\x07").into();
let mut ret: OsString = format!("\x1b]8;;file://{hostname}{absolute_path_encoded}\x07").into();
ret.push(name);
ret.push("\x1b]8;;\x07");
ret
Expand Down
Loading