8000 fmt: fail if goal is bigger than default width by cakebaker · Pull Request #6096 · uutils/coreutils · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fmt: fail if goal is bigger than default width #6096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
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
< 518B tr data-hunk="269779014cef9fe4b0c8557aeff0ba3c2fc79a3ed6064c1790fb7381a78c0c10" class="show-top-border">
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