Skip to content

Commit

Permalink
Merge pull request #6096 from cakebaker/fmt_fail_if_goal_bigger_than_…
Browse files Browse the repository at this point in the history
…default_width

fmt: fail if goal is bigger than default width
  • Loading branch information
sylvestre authored Mar 20, 2024
2 parents 660014e + 3ad226c commit a3ff7b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/uu/fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ mod parasplit;
const ABOUT: &str = help_about!("fmt.md");
const USAGE: &str = help_usage!("fmt.md");
const MAX_WIDTH: usize = 2500;
const DEFAULT_GOAL: usize = 70;
const DEFAULT_WIDTH: usize = 75;
// by default, goal is 93% of width
const DEFAULT_GOAL_TO_WIDTH_RATIO: usize = 93;

mod options {
pub const CROWN_MARGIN: &str = "crown-margin";
Expand All @@ -39,9 +43,6 @@ mod options {
pub const FILES: &str = "files";
}

// by default, goal is 93% of width
const DEFAULT_GOAL_TO_WIDTH_RATIO: usize = 93;

pub type FileOrStdReader = BufReader<Box<dyn Read + 'static>>;
pub struct FmtOptions {
crown: bool,
Expand Down Expand Up @@ -100,10 +101,13 @@ impl FmtOptions {
(w, g)
}
(None, Some(&g)) => {
if g > DEFAULT_WIDTH {
return Err(USimpleError::new(1, "GOAL cannot be greater than WIDTH."));
}
let w = (g * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO).max(g + 3);
(w, g)
}
(None, None) => (75, 70),
(None, None) => (DEFAULT_WIDTH, DEFAULT_GOAL),
};
debug_assert!(width >= goal, "GOAL {goal} should not be greater than WIDTH {width} when given {width_opt:?} and {goal_opt:?}.");

Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ fn test_fmt_goal_too_big() {
}
}

#[test]
fn test_fmt_goal_bigger_than_default_width_of_75() {
for param in ["-g", "--goal"] {
new_ucmd!()
.args(&["one-word-per-line.txt", param, "76"])
.fails()
.code_is(1)
.stderr_is("fmt: GOAL cannot be greater than WIDTH.\n");
}
}

#[test]
fn test_fmt_invalid_goal() {
for param in ["-g", "--goal"] {
Expand Down

0 comments on commit a3ff7b7

Please sign in to comment.