Skip to content

Commit 507e1cc

Browse files
authored
Merge pull request #8271 from RenjiSann/quoting-simplify
uucore(quoting_style): Improve quoting style handling
2 parents 367bc30 + ec1781b commit 507e1cc

7 files changed

Lines changed: 108 additions & 145 deletions

File tree

src/uu/dir/src/dir.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ffi::OsString;
88
use std::path::Path;
99
use uu_ls::{Config, Format, options};
1010
use uucore::error::UResult;
11-
use uucore::quoting_style::{Quotes, QuotingStyle};
11+
use uucore::quoting_style::QuotingStyle;
1212

1313
#[uucore::main]
1414
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
@@ -45,9 +45,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4545
let mut config = Config::from(&matches)?;
4646

4747
if default_quoting_style {
48-
config.quoting_style = QuotingStyle::C {
49-
quotes: Quotes::None,
50-
};
48+
config.quoting_style = QuotingStyle::C_NO_QUOTES;
5149
}
5250
if default_format_style {
5351
config.format = Format::Columns;

src/uu/ls/src/ls.rs

Lines changed: 16 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ use uucore::libc::{S_IXGRP, S_IXOTH, S_IXUSR};
6161
use uucore::libc::{dev_t, major, minor};
6262
use uucore::line_ending::LineEnding;
6363
use uucore::locale::{get_message, get_message_with_args};
64-
use uucore::quoting_style::{
65-
self, QuotingStyle, locale_aware_escape_dir_name, locale_aware_escape_name,
66-
};
64+
use uucore::quoting_style::{QuotingStyle, locale_aware_escape_dir_name, locale_aware_escape_name};
6765
use uucore::{
6866
display::Quotable,
6967
error::{UError, UResult, set_exit_code},
@@ -598,34 +596,15 @@ fn extract_hyperlink(options: &clap::ArgMatches) -> bool {
598596
fn match_quoting_style_name(style: &str, show_control: bool) -> Option<QuotingStyle> {
599597
match style {
600598
"literal" => Some(QuotingStyle::Literal { show_control }),
601-
"shell" => Some(QuotingStyle::Shell {
602-
escape: false,
603-
always_quote: false,
604-
show_control,
605-
}),
606-
"shell-always" => Some(QuotingStyle::Shell {
607-
escape: false,
608-
always_quote: true,
609-
show_control,
610-
}),
611-
"shell-escape" => Some(QuotingStyle::Shell {
612-
escape: true,
613-
always_quote: false,
614-
show_control,
615-
}),
616-
"shell-escape-always" => Some(QuotingStyle::Shell {
617-
escape: true,
618-
always_quote: true,
619-
show_control,
620-
}),
621-
"c" => Some(QuotingStyle::C {
622-
quotes: quoting_style::Quotes::Double,
623-
}),
624-
"escape" => Some(QuotingStyle::C {
625-
quotes: quoting_style::Quotes::None,
626-
}),
599+
"shell" => Some(QuotingStyle::SHELL),
600+
"shell-always" => Some(QuotingStyle::SHELL_QUOTE),
601+
"shell-escape" => Some(QuotingStyle::SHELL_ESCAPE),
602+
"shell-escape-always" => Some(QuotingStyle::SHELL_ESCAPE_QUOTE),
603+
"c" => Some(QuotingStyle::C_DOUBLE),
604+
"escape" => Some(QuotingStyle::C_NO_QUOTES),
627605
_ => None,
628606
}
607+
.map(|qs| qs.show_control(show_control))
629608
}
630609

631610
/// Extracts the quoting style to use based on the options provided.
@@ -651,13 +630,9 @@ fn extract_quoting_style(options: &clap::ArgMatches, show_control: bool) -> Quot
651630
} else if options.get_flag(options::quoting::LITERAL) {
652631
QuotingStyle::Literal { show_control }
653632
} else if options.get_flag(options::quoting::ESCAPE) {
654-
QuotingStyle::C {
655-
quotes: quoting_style::Quotes::None,
656-
}
633+
QuotingStyle::C_NO_QUOTES
657634
} else if options.get_flag(options::quoting::C) {
658-
QuotingStyle::C {
659-
quotes: quoting_style::Quotes::Double,
660-
}
635+
QuotingStyle::C_DOUBLE
661636
} else if options.get_flag(options::DIRED) {
662637
QuotingStyle::Literal { show_control }
663638
} else {
@@ -684,11 +659,7 @@ fn extract_quoting_style(options: &clap::ArgMatches, show_control: bool) -> Quot
684659
// By default, `ls` uses Shell escape quoting style when writing to a terminal file
685660
// descriptor and Literal otherwise.
686661
if stdout().is_terminal() {
687-
QuotingStyle::Shell {
688-
escape: true,
689-
always_quote: false,
690-
show_control,
691-
}
662+
QuotingStyle::SHELL_ESCAPE.show_control(show_control)
692663
} else {
693664
QuotingStyle::Literal { show_control }
694665
}
@@ -2010,7 +1981,7 @@ fn show_dir_name(
20101981
config: &Config,
20111982
) -> std::io::Result<()> {
20121983
let escaped_name =
2013-
locale_aware_escape_dir_name(path_data.p_buf.as_os_str(), &config.quoting_style);
1984+
locale_aware_escape_dir_name(path_data.p_buf.as_os_str(), config.quoting_style);
20141985

20151986
let name = if config.hyperlink && !config.dired {
20161987
create_hyperlink(&escaped_name, path_data)
@@ -2511,7 +2482,7 @@ fn display_items(
25112482
// option, print the security context to the left of the size column.
25122483

25132484
let quoted = items.iter().any(|item| {
2514-
let name = locale_aware_escape_name(&item.display_name, &config.quoting_style);
2485+
let name = locale_aware_escape_name(&item.display_name, config.quoting_style);
25152486
os_str_starts_with(&name, b"'")
25162487
});
25172488

@@ -3175,7 +3146,7 @@ fn display_item_name(
31753146
current_column: LazyCell<usize, Box<dyn FnOnce() -> usize + '_>>,
31763147
) -> OsString {
31773148
// This is our return value. We start by `&path.display_name` and modify it along the way.
3178-
let mut name = locale_aware_escape_name(&path.display_name, &config.quoting_style);
3149+
let mut name = locale_aware_escape_name(&path.display_name, config.quoting_style);
31793150

31803151
let is_wrap =
31813152
|namelen: usize| config.width != 0 && *current_column + namelen > config.width.into();
@@ -3267,7 +3238,7 @@ fn display_item_name(
32673238
name.push(path.p_buf.read_link().unwrap());
32683239
} else {
32693240
name.push(color_name(
3270-
locale_aware_escape_name(target.as_os_str(), &config.quoting_style),
3241+
locale_aware_escape_name(target.as_os_str(), config.quoting_style),
32713242
path,
32723243
style_manager,
32733244
&mut state.out,
@@ -3280,7 +3251,7 @@ fn display_item_name(
32803251
// Apply the right quoting
32813252
name.push(locale_aware_escape_name(
32823253
target.as_os_str(),
3283-
&config.quoting_style,
3254+
config.quoting_style,
32843255
));
32853256
}
32863257
}

src/uu/vdir/src/vdir.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ffi::OsString;
88
use std::path::Path;
99
use uu_ls::{Config, Format, options};
1010
use uucore::error::UResult;
11-
use uucore::quoting_style::{Quotes, QuotingStyle};
11+
use uucore::quoting_style::QuotingStyle;
1212

1313
#[uucore::main]
1414
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
@@ -44,9 +44,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4444
let mut config = Config::from(&matches)?;
4545

4646
if default_quoting_style {
47-
config.quoting_style = QuotingStyle::C {
48-
quotes: Quotes::None,
49-
};
47+
config.quoting_style = QuotingStyle::C_NO_QUOTES;
5048
}
5149
if default_format_style {
5250
config.format = Format::Long;

src/uu/wc/src/wc.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,6 @@ mod options {
127127
static ARG_FILES: &str = "files";
128128
static STDIN_REPR: &str = "-";
129129

130-
static QS_ESCAPE: &QuotingStyle = &QuotingStyle::Shell {
131-
escape: true,
132-
always_quote: false,
133-
show_control: false,
134-
};
135-
static QS_QUOTE_ESCAPE: &QuotingStyle = &QuotingStyle::Shell {
136-
escape: true,
137-
always_quote: true,
138-
show_control: false,
139-
};
140-
141130
/// Supported inputs.
142131
#[derive(Debug)]
143132
enum Inputs<'a> {
@@ -260,7 +249,8 @@ impl<'a> Input<'a> {
260249
let path = path.as_os_str();
261250
if path.to_string_lossy().contains('\n') {
262251
Some(Cow::Owned(quoting_style::locale_aware_escape_name(
263-
path, QS_ESCAPE,
252+
path,
253+
QuotingStyle::SHELL_ESCAPE,
264254
)))
265255
} else {
266256
Some(Cow::Borrowed(path))
@@ -761,9 +751,12 @@ fn files0_iter_file<'a>(path: &Path) -> UResult<impl Iterator<Item = InputIterIt
761751
"wc-error-cannot-open-for-reading",
762752
HashMap::from([(
763753
"path".to_string(),
764-
quoting_style::locale_aware_escape_name(path.as_os_str(), QS_QUOTE_ESCAPE)
765-
.into_string()
766-
.expect("All escaped names with the escaping option return valid strings."),
754+
quoting_style::locale_aware_escape_name(
755+
path.as_os_str(),
756+
QuotingStyle::SHELL_ESCAPE_QUOTE,
757+
)
758+
.into_string()
759+
.expect("All escaped names with the escaping option return valid strings."),
767760
)]),
768761
)
769762
})),
@@ -814,7 +807,7 @@ fn files0_iter<'a>(
814807
}
815808

816809
fn escape_name_wrapper(name: &OsStr) -> String {
817-
quoting_style::locale_aware_escape_name(name, QS_ESCAPE)
810+
quoting_style::locale_aware_escape_name(name, QuotingStyle::SHELL_ESCAPE)
818811
.into_string()
819812
.expect("All escaped names with the escaping option return valid strings.")
820813
}

src/uucore/src/lib/features/format/argument.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::format::spec::ArgumentLocation;
88
use crate::{
99
error::set_exit_code,
1010
parser::num_parser::{ExtendedParser, ExtendedParserError},
11-
quoting_style::{Quotes, QuotingStyle, locale_aware_escape_name},
11+
quoting_style::{QuotingStyle, locale_aware_escape_name},
1212
show_error, show_warning,
1313
};
1414
use os_display::Quotable;
@@ -153,12 +153,7 @@ fn extract_value<T: Default>(p: Result<T, ExtendedParserError<'_, T>>, input: &s
153153
Ok(v) => v,
154154
Err(e) => {
155155
set_exit_code(1);
156-
let input = locale_aware_escape_name(
157-
OsStr::new(input),
158-
&QuotingStyle::C {
159-
quotes: Quotes::None,
160-
},
161-
);
156+
let input = locale_aware_escape_name(OsStr::new(input), QuotingStyle::C_NO_QUOTES);
162157
match e {
163158
ExtendedParserError::Overflow(v) => {
164159
show_error!("{}: Numerical result out of range", input.quote());

src/uucore/src/lib/features/format/spec.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,7 @@ impl Spec {
404404
Self::QuotedString { position } => {
405405
let s = locale_aware_escape_name(
406406
args.next_string(position).as_ref(),
407-
&QuotingStyle::Shell {
408-
escape: true,
409-
always_quote: false,
410-
show_control: false,
411-
},
407+
QuotingStyle::SHELL_ESCAPE,
412408
);
413409
#[cfg(unix)]
414410
let bytes = std::os::unix::ffi::OsStringExt::into_vec(s);

0 commit comments

Comments
 (0)