From 42d6604e591307fa591e830cdaeef80d2f586b59 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Tue, 18 Apr 2023 10:12:17 +0200 Subject: [PATCH 1/2] fmt: implement default for FmtOptions --- src/uu/fmt/src/fmt.rs | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index d144bdd8a..fc2226d6b 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -60,6 +60,29 @@ pub struct FmtOptions { goal: usize, tabwidth: usize, } + +impl Default for FmtOptions { + fn default() -> Self { + Self { + crown: false, + tagged: false, + mail: false, + uniform: false, + quick: false, + split_only: false, + use_prefix: false, + prefix: String::new(), + xprefix: false, + use_anti_prefix: false, + anti_prefix: String::new(), + xanti_prefix: false, + width: 79, + goal: 74, + tabwidth: 8, + } + } +} + /// Parse the command line arguments and return the list of files and formatting options. /// /// # Arguments @@ -70,6 +93,7 @@ pub struct FmtOptions { /// /// A tuple containing a vector of file names and a `FmtOptions` struct. #[allow(clippy::cognitive_complexity)] +#[allow(clippy::field_reassign_with_default)] fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec, FmtOptions)> { let matches = uu_app().try_get_matches_from(args)?; @@ -78,23 +102,7 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec, FmtOptions) .map(|v| v.map(ToString::to_string).collect()) .unwrap_or_default(); - let mut fmt_opts = FmtOptions { - crown: false, - tagged: false, - mail: false, - uniform: false, - quick: false, - split_only: false, - use_prefix: false, - prefix: String::new(), - xprefix: false, - use_anti_prefix: false, - anti_prefix: String::new(), - xanti_prefix: false, - width: 79, - goal: 74, - tabwidth: 8, - }; + let mut fmt_opts = FmtOptions::default(); fmt_opts.tagged = matches.get_flag(OPT_TAGGED_PARAGRAPH); if matches.get_flag(OPT_CROWN_MARGIN) { From 09db8d8af926e8b376653a9398c9bdba833dacec Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Fri, 28 Apr 2023 16:55:01 +0200 Subject: [PATCH 2/2] fmt: adapt defaults to the ones used by GNU fmt --- src/uu/fmt/src/fmt.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index fc2226d6b..f112d1b07 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -76,8 +76,8 @@ impl Default for FmtOptions { use_anti_prefix: false, anti_prefix: String::new(), xanti_prefix: false, - width: 79, - goal: 74, + width: 75, + goal: 70, tabwidth: 8, } } @@ -95,6 +95,9 @@ impl Default for FmtOptions { #[allow(clippy::cognitive_complexity)] #[allow(clippy::field_reassign_with_default)] fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec, FmtOptions)> { + // by default, goal is 93% of width + const DEFAULT_GOAL_TO_WIDTH_RATIO: usize = 93; + let matches = uu_app().try_get_matches_from(args)?; let mut files: Vec = matches @@ -149,7 +152,10 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec, FmtOptions) ), )); } - fmt_opts.goal = cmp::min(fmt_opts.width * 94 / 100, fmt_opts.width - 3); + fmt_opts.goal = cmp::min( + fmt_opts.width * DEFAULT_GOAL_TO_WIDTH_RATIO / 100, + fmt_opts.width - 3, + ); }; if let Some(s) = matches.get_one::(OPT_GOAL) { @@ -163,7 +169,10 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec, FmtOptions) } }; if !matches.get_flag(OPT_WIDTH) { - fmt_opts.width = cmp::max(fmt_opts.goal * 100 / 94, fmt_opts.goal + 3); + fmt_opts.width = cmp::max( + fmt_opts.goal * 100 / DEFAULT_GOAL_TO_WIDTH_RATIO, + fmt_opts.goal + 3, + ); } else if fmt_opts.goal > fmt_opts.width { return Err(USimpleError::new(1, "GOAL cannot be greater than WIDTH.")); } @@ -364,14 +373,14 @@ pub fn uu_app() -> Command { Arg::new(OPT_WIDTH) .short('w') .long("width") - .help("Fill output lines up to a maximum of WIDTH columns, default 79.") + .help("Fill output lines up to a maximum of WIDTH columns, default 75.") .value_name("WIDTH"), ) .arg( Arg::new(OPT_GOAL) .short('g') .long("goal") - .help("Goal width, default ~0.94*WIDTH. Must be less than WIDTH.") + .help("Goal width, default of 93% of WIDTH. Must be less than WIDTH.") .value_name("GOAL"), ) .arg(