mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
ls: Process OsStrings instead of Strings
This commit is contained in:
parent
4d6faae555
commit
483a5fd1d4
2 changed files with 19 additions and 17 deletions
|
@ -21,6 +21,7 @@ use lscolors::LsColors;
|
||||||
use number_prefix::NumberPrefix;
|
use number_prefix::NumberPrefix;
|
||||||
use once_cell::unsync::OnceCell;
|
use once_cell::unsync::OnceCell;
|
||||||
use quoting_style::{escape_name, QuotingStyle};
|
use quoting_style::{escape_name, QuotingStyle};
|
||||||
|
use std::ffi::OsString;
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
use std::os::windows::fs::MetadataExt;
|
use std::os::windows::fs::MetadataExt;
|
||||||
use std::{
|
use std::{
|
||||||
|
@ -1173,7 +1174,7 @@ struct PathData {
|
||||||
md: OnceCell<Option<Metadata>>,
|
md: OnceCell<Option<Metadata>>,
|
||||||
ft: OnceCell<Option<FileType>>,
|
ft: OnceCell<Option<FileType>>,
|
||||||
// Name of the file - will be empty for . or ..
|
// Name of the file - will be empty for . or ..
|
||||||
display_name: String,
|
display_name: OsString,
|
||||||
// PathBuf that all above data corresponds to
|
// PathBuf that all above data corresponds to
|
||||||
p_buf: PathBuf,
|
p_buf: PathBuf,
|
||||||
must_dereference: bool,
|
must_dereference: bool,
|
||||||
|
@ -1183,7 +1184,7 @@ impl PathData {
|
||||||
fn new(
|
fn new(
|
||||||
p_buf: PathBuf,
|
p_buf: PathBuf,
|
||||||
file_type: Option<std::io::Result<FileType>>,
|
file_type: Option<std::io::Result<FileType>>,
|
||||||
file_name: Option<String>,
|
file_name: Option<OsString>,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
command_line: bool,
|
command_line: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -1191,16 +1192,13 @@ impl PathData {
|
||||||
// For '..', the filename is None
|
// For '..', the filename is None
|
||||||
let display_name = if let Some(name) = file_name {
|
let display_name = if let Some(name) = file_name {
|
||||||
name
|
name
|
||||||
|
} else if command_line {
|
||||||
|
p_buf.clone().into()
|
||||||
} else {
|
} else {
|
||||||
let display_os_str = if command_line {
|
p_buf
|
||||||
p_buf.as_os_str()
|
.file_name()
|
||||||
} else {
|
.unwrap_or_else(|| p_buf.iter().next_back().unwrap())
|
||||||
p_buf
|
.to_owned()
|
||||||
.file_name()
|
|
||||||
.unwrap_or_else(|| p_buf.iter().next_back().unwrap())
|
|
||||||
};
|
|
||||||
|
|
||||||
display_os_str.to_string_lossy().into_owned()
|
|
||||||
};
|
};
|
||||||
let must_dereference = match &config.dereference {
|
let must_dereference = match &config.dereference {
|
||||||
Dereference::All => true,
|
Dereference::All => true,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use std::char::from_digit;
|
use std::char::from_digit;
|
||||||
|
use std::ffi::OsStr;
|
||||||
|
|
||||||
// These are characters with special meaning in the shell (e.g. bash).
|
// These are characters with special meaning in the shell (e.g. bash).
|
||||||
// The first const contains characters that only have a special meaning when they appear at the beginning of a name.
|
// The first const contains characters that only have a special meaning when they appear at the beginning of a name.
|
||||||
|
@ -255,19 +256,21 @@ fn shell_with_escape(name: &str, quotes: Quotes) -> (String, bool) {
|
||||||
(escaped_str, must_quote)
|
(escaped_str, must_quote)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn escape_name(name: &str, style: &QuotingStyle) -> String {
|
pub(super) fn escape_name(name: &OsStr, style: &QuotingStyle) -> String {
|
||||||
match style {
|
match style {
|
||||||
QuotingStyle::Literal { show_control } => {
|
QuotingStyle::Literal { show_control } => {
|
||||||
if !show_control {
|
if !show_control {
|
||||||
name.chars()
|
name.to_string_lossy()
|
||||||
|
.chars()
|
||||||
.flat_map(|c| EscapedChar::new_literal(c).hide_control())
|
.flat_map(|c| EscapedChar::new_literal(c).hide_control())
|
||||||
.collect()
|
.collect()
|
||||||
} else {
|
} else {
|
||||||
name.into()
|
name.to_string_lossy().into_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QuotingStyle::C { quotes } => {
|
QuotingStyle::C { quotes } => {
|
||||||
let escaped_str: String = name
|
let escaped_str: String = name
|
||||||
|
.to_string_lossy()
|
||||||
.chars()
|
.chars()
|
||||||
.flat_map(|c| EscapedChar::new_c(c, *quotes))
|
.flat_map(|c| EscapedChar::new_c(c, *quotes))
|
||||||
.collect();
|
.collect();
|
||||||
|
@ -283,6 +286,7 @@ pub(super) fn escape_name(name: &str, style: &QuotingStyle) -> String {
|
||||||
always_quote,
|
always_quote,
|
||||||
show_control,
|
show_control,
|
||||||
} => {
|
} => {
|
||||||
|
let name = name.to_string_lossy();
|
||||||
let (quotes, must_quote) = if name.contains('"') {
|
let (quotes, must_quote) = if name.contains('"') {
|
||||||
(Quotes::Single, true)
|
(Quotes::Single, true)
|
||||||
} else if name.contains('\'') {
|
} else if name.contains('\'') {
|
||||||
|
@ -294,9 +298,9 @@ pub(super) fn escape_name(name: &str, style: &QuotingStyle) -> String {
|
||||||
};
|
};
|
||||||
|
|
||||||
let (escaped_str, contains_quote_chars) = if *escape {
|
let (escaped_str, contains_quote_chars) = if *escape {
|
||||||
shell_with_escape(name, quotes)
|
shell_with_escape(&name, quotes)
|
||||||
} else {
|
} else {
|
||||||
shell_without_escape(name, quotes, *show_control)
|
shell_without_escape(&name, quotes, *show_control)
|
||||||
};
|
};
|
||||||
|
|
||||||
match (must_quote | contains_quote_chars, quotes) {
|
match (must_quote | contains_quote_chars, quotes) {
|
||||||
|
@ -362,7 +366,7 @@ mod tests {
|
||||||
fn check_names(name: &str, map: Vec<(&str, &str)>) {
|
fn check_names(name: &str, map: Vec<(&str, &str)>) {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
map.iter()
|
map.iter()
|
||||||
.map(|(_, style)| escape_name(name, &get_style(style)))
|
.map(|(_, style)| escape_name(name.as_ref(), &get_style(style)))
|
||||||
.collect::<Vec<String>>(),
|
.collect::<Vec<String>>(),
|
||||||
map.iter()
|
map.iter()
|
||||||
.map(|(correct, _)| correct.to_string())
|
.map(|(correct, _)| correct.to_string())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue