Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/uu/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ doctest = false
[dependencies]
clap = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["signals"] }
uucore = { workspace = true, features = ["quoting-style", "signals"] }
fluent = { workspace = true }

[target.'cfg(unix)'.dependencies]
Expand Down
4 changes: 3 additions & 1 deletion src/uu/env/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use std::io::stderr;
use std::mem::zeroed;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use uucore::i18n::UEncoding;

use uucore::display::{Quotable, print_all_env_vars};
use uucore::error::{ExitCode, UError, UResult, USimpleError, UUsageError, strip_errno};
Expand Down Expand Up @@ -820,7 +821,8 @@ impl EnvAppData {
apply_change_directory(&opts)?;
if opts.program.is_empty() {
// no program provided, so just dump all env vars to stdout
print_all_env_vars(opts.line_ending)?;
// `encoding` is irrelevant when `quoting_style` is None
print_all_env_vars(opts.line_ending, None, UEncoding::Ascii)?;
} else {
return self.run_program(&opts, self.do_debug_printing);
}
Expand Down
1 change: 0 additions & 1 deletion src/uu/ls/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ ls-help-full-time = like -l --time-style=full-iso
ls-help-context = print any security context of each file
ls-help-group-directories-first = group directories before files; can be augmented with
a --sort option, but any use of --sort=none (-U) disables grouping
ls-invalid-quoting-style = {$program}: Ignoring invalid value of environment variable QUOTING_STYLE: '{$style}'
ls-invalid-columns-width = ignoring invalid width in environment variable COLUMNS: {$width}
ls-invalid-ignore-pattern = Invalid pattern for ignore: {$pattern}
ls-invalid-hide-pattern = Invalid pattern for hide: {$pattern}
Expand Down
1 change: 0 additions & 1 deletion src/uu/ls/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ ls-help-full-time = comme -l --time-style=full-iso
ls-help-context = afficher tout contexte de sécurité de chaque fichier
ls-help-group-directories-first = grouper les répertoires avant les fichiers ; peut être augmenté avec
une option --sort, mais toute utilisation de --sort=none (-U) désactive le groupement
ls-invalid-quoting-style = {$program} : Ignorer la valeur invalide de la variable d'environnement QUOTING_STYLE : '{$style}'
ls-invalid-columns-width = ignorer la largeur invalide dans la variable d'environnement COLUMNS : {$width}
ls-invalid-ignore-pattern = Motif invalide pour ignore : {$pattern}
ls-invalid-hide-pattern = Motif invalide pour hide : {$pattern}
Expand Down
175 changes: 41 additions & 134 deletions src/uu/ls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use std::{
borrow::Cow,
ffi::{OsStr, OsString},
io::{self, IsTerminal, Write as _, stdout},
io::{IsTerminal, stdout},
num::IntErrorKind,
};

Expand All @@ -18,16 +18,23 @@ use lscolors::LsColors;
use term_grid::SPACES_IN_TAB;

use uucore::{
diagnostics::OptionValue, display::Quotable, error::UResult, format::human::SizeFormat,
fsext::MetadataTimeField, line_ending::LineEnding, parser::parse_block_size,
parser::parse_glob, parser::parse_size::parse_size_non_zero_u64, quoting_style::QuotingStyle,
show_error, show_warning, time::format, translate,
diagnostics::OptionValue,
display::Quotable,
error::UResult,
format::human::SizeFormat,
fsext::MetadataTimeField,
line_ending::LineEnding,
parser::{parse_block_size, parse_glob, parse_size::parse_size_non_zero_u64},
quoting_style::{QuotingStyle, quoting_style_from_env},
show_error, show_warning,
time::format,
translate,
};

use crate::{
LsError,
colors::{LsColorsParseError, validate_ls_colors_env},
display::{Format, IndicatorStyle, LocaleQuoting, LongFormat},
display::{Format, IndicatorStyle, LongFormat},
options::QUOTING_STYLE,
};

Expand Down Expand Up @@ -226,7 +233,6 @@ pub struct Config {
// Dir and vdir needs access to this field
pub quoting_style: QuotingStyle,
pub(crate) show_control_chars: bool,
pub(crate) locale_quoting: Option<LocaleQuoting>,
pub(crate) indicator_style: Option<IndicatorStyle>,
pub(crate) time_format_recent: String, // Time format for recent dates
pub(crate) time_format_older: Option<String>, // Time format for older dates (optional, if not present, time_format_recent is used)
Expand Down Expand Up @@ -497,73 +503,6 @@ fn extract_hyperlink(options: &clap::ArgMatches) -> bool {
}
}

/// Match the argument given to --quoting-style or the [`QUOTING_STYLE`] env variable.
///
/// # Arguments
///
/// * `style`: the actual argument string
/// * `show_control` - A boolean value representing whether to show control characters.
///
/// # Returns
///
/// * An option with None if the style string is invalid, or a `QuotingStyle` wrapped in `Some`.
struct QuotingStyleSpec {
style: QuotingStyle,
fixed_control: bool,
locale: Option<LocaleQuoting>,
}

impl QuotingStyleSpec {
fn new(style: QuotingStyle) -> Self {
Self {
style,
fixed_control: false,
locale: None,
}
}

fn with_locale(style: QuotingStyle, locale: LocaleQuoting) -> Self {
Self {
style,
fixed_control: true,
locale: Some(locale),
}
}
}
fn match_quoting_style_name(
style: &str,
show_control: bool,
) -> Option<(QuotingStyle, Option<LocaleQuoting>)> {
let spec = match style {
"literal" => QuotingStyleSpec::new(QuotingStyle::Literal {
show_control: false,
}),
"shell" => QuotingStyleSpec::new(QuotingStyle::SHELL),
"shell-always" => QuotingStyleSpec::new(QuotingStyle::SHELL_QUOTE),
"shell-escape" => QuotingStyleSpec::new(QuotingStyle::SHELL_ESCAPE),
"shell-escape-always" => QuotingStyleSpec::new(QuotingStyle::SHELL_ESCAPE_QUOTE),
"c" => QuotingStyleSpec::new(QuotingStyle::C_DOUBLE),
"escape" => QuotingStyleSpec::new(QuotingStyle::C_NO_QUOTES),
"locale" => QuotingStyleSpec {
style: QuotingStyle::Literal {
show_control: false,
},
fixed_control: true,
locale: Some(LocaleQuoting::Single),
},
"clocale" => QuotingStyleSpec::with_locale(QuotingStyle::C_DOUBLE, LocaleQuoting::Double),
_ => return None,
};

let style = if spec.fixed_control {
spec.style
} else {
spec.style.show_control(show_control)
};

Some((style, spec.locale))
}

/// Extracts the quoting style to use based on the options provided.
/// If no options are given, it looks if a default quoting style is provided
/// through the [`QUOTING_STYLE`] environment variable.
Expand All @@ -580,39 +519,32 @@ fn extract_quoting_style(
options: &clap::ArgMatches,
show_control: bool,
mode: ProgramMode,
) -> (QuotingStyle, Option<LocaleQuoting>) {
) -> QuotingStyle {
let opt_quoting_style = options.get_one::<String>(QUOTING_STYLE);

if let Some(style) = opt_quoting_style {
match match_quoting_style_name(style, show_control) {
Some(pair) => pair,
match QuotingStyle::parse(style) {
Some(qs) => qs.show_control(show_control),
None => unreachable!("Should have been caught by Clap"),
}
} else if options.get_flag(options::quoting::LITERAL) {
(QuotingStyle::Literal { show_control }, None)
QuotingStyle::Literal { show_control }
} else if options.get_flag(options::quoting::ESCAPE) {
(QuotingStyle::C_NO_QUOTES, None)
QuotingStyle::C_NO_QUOTES
} else if options.get_flag(options::quoting::C) {
(QuotingStyle::C_DOUBLE, None)
QuotingStyle::C_DOUBLE
} else {
// If set, the QUOTING_STYLE environment variable specifies a default style.
if let Ok(style) = std::env::var("QUOTING_STYLE") {
if let Some(pair) = match_quoting_style_name(style.as_str(), show_control) {
return pair;
}
let _ = writeln!(
io::stderr(),
"{}",
translate!("ls-invalid-quoting-style", "program" => std::env::args().next().unwrap_or_else(|| "ls".to_string()), "style" => style)
);
if let Some(qs) = quoting_style_from_env() {
Comment thread
RenjiSann marked this conversation as resolved.
return qs.show_control(show_control);
}

match mode {
ProgramMode::Dir | ProgramMode::Vdir => (QuotingStyle::C_NO_QUOTES, None),
ProgramMode::Dir | ProgramMode::Vdir => QuotingStyle::C_NO_QUOTES,
ProgramMode::Ls if stdout().is_terminal() => {
(QuotingStyle::SHELL_ESCAPE.show_control(show_control), None)
QuotingStyle::SHELL_ESCAPE.show_control(show_control)
}
ProgramMode::Ls => (QuotingStyle::Literal { show_control }, None),
ProgramMode::Ls => QuotingStyle::Literal { show_control },
}
}
}
Expand Down Expand Up @@ -860,8 +792,7 @@ impl Config {
|| !stdout().is_terminal()
};

let (mut quoting_style, mut locale_quoting) =
extract_quoting_style(options, show_control, mode);
let mut quoting_style = extract_quoting_style(options, show_control, mode);
let indicator_style = extract_indicator_style(options);

let mut ignore_patterns: Vec<Pattern> = Vec::new();
Expand Down Expand Up @@ -930,52 +861,29 @@ impl Config {
options::quoting::LITERAL,
];
let get_last = |flag: &str| -> usize {
if options.value_source(flag) == Some(clap::parser::ValueSource::CommandLine) {
options.index_of(flag).unwrap_or(0)
} else {
0
}
};
if get_last(options::ZERO)
> zero_formats_opts
.into_iter()
.map(get_last)
.max()
(options.value_source(flag) == Some(clap::parser::ValueSource::CommandLine))
.then(|| options.index_of(flag))
.flatten()
.unwrap_or(0)
{
format = if explicit_long {
format
} else {
Format::OneLine
};
};
let zero_idx = get_last(options::ZERO);
let last_of =
|flag_list: &[&str]| flag_list.iter().copied().map(get_last).max().unwrap_or(0);

if zero_idx > last_of(&zero_formats_opts) && !explicit_long {
format = Format::OneLine;
}
if get_last(options::ZERO)
> zero_colors_opts
.into_iter()
.map(get_last)
.max()
.unwrap_or(0)
{

if zero_idx > last_of(&zero_colors_opts) {
needs_color = false;
}
if get_last(options::ZERO)
> zero_show_control_opts
.into_iter()
.map(get_last)
.max()
.unwrap_or(0)
{

if zero_idx > last_of(&zero_show_control_opts) {
show_control = true;
}
if get_last(options::ZERO)
> zero_quoting_style_opts
.into_iter()
.map(get_last)
.max()
.unwrap_or(0)
{

if zero_idx > last_of(&zero_quoting_style_opts) {
quoting_style = QuotingStyle::Literal { show_control };
locale_quoting = None;
}

if needs_color && let Err(err) = validate_ls_colors_env() {
Expand Down Expand Up @@ -1062,7 +970,6 @@ impl Config {
width,
quoting_style,
show_control_chars: options.get_flag(options::SHOW_CONTROL_CHARS),
locale_quoting,
indicator_style,
time_format_recent,
time_format_older,
Expand Down
Loading
Loading