Skip to content
Merged
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
9 changes: 3 additions & 6 deletions src/uu/ls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ fn parse_width(width_match: Option<&String>) -> Result<u16, LsError> {
}

/// Parses the tab size value from the command line
fn parse_tab_size(size_str: &str) -> Result<usize, Box<LsError>> {
fn parse_tab_size(size_str: &str) -> Result<usize, LsError> {
size_str
.parse::<usize>()
.ok()
Expand All @@ -679,7 +679,7 @@ fn parse_tab_size(size_str: &str) -> Result<usize, Box<LsError>> {
.or_else(|| size_str.strip_prefix("0X"))
.and_then(|hex| usize::from_str_radix(hex, 16).ok())
})
.ok_or_else(|| Box::new(LsError::InvalidTabSize(size_str.to_string())))
.ok_or_else(|| LsError::InvalidTabSize(size_str.to_string()))
}

impl Config {
Expand Down Expand Up @@ -976,10 +976,7 @@ impl Config {
let tab_size = if needs_color {
Some(0)
} else if let Some(size_str) = options.get_one::<String>(options::format::TAB_SIZE) {
match parse_tab_size(size_str) {
Ok(n) => Some(n),
Err(e) => return Err(e),
}
Some(parse_tab_size(size_str)?)
} else {
None
};
Expand Down
27 changes: 10 additions & 17 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5297,24 +5297,17 @@ fn test_symlink_target_extension_color() {
fn test_tabsize_option() {
let scene = TestScenario::new(util_name!());

scene.ucmd().args(&["-T", "3"]).succeeds();
for valid in ["3", "0", "0xff"] {
scene.ucmd().args(&["-T", valid]).succeeds();
}
scene.ucmd().args(&["--tabsize", "0"]).succeeds();
scene
.ucmd()
.arg("--tabsize=-3")
.fails()
.stderr_is("ls: invalid tab size: '-3'\n");
scene
.ucmd()
.arg("--tabsize=a")
.fails()
.stderr_is("ls: invalid tab size: 'a'\n");
scene
.ucmd()
.arg("--tabsize=3.14")
.fails()
.stderr_is("ls: invalid tab size: '3.14'\n");
scene.ucmd().args(&["-T", "0xff"]).succeeds();
for invalid in ["-3", "a", "3.14"] {
scene
.ucmd()
.arg(format!("--tabsize={invalid}"))
.fails()
.stderr_is(format!("ls: invalid tab size: '{invalid}'\n"));
}
scene.ucmd().arg("-T").fails();
}

Expand Down
Loading