Skip to content

fmt: implement Default for FmtOptions#4747

Merged
tertsdiepraam merged 3 commits intouutils:mainfrom
cakebaker:fmt_implement_default_for_fmtoptions
Aug 11, 2023
Merged

fmt: implement Default for FmtOptions#4747
tertsdiepraam merged 3 commits intouutils:mainfrom
cakebaker:fmt_implement_default_for_fmtoptions

Conversation

@cakebaker
Copy link
Contributor

This PR implements Default for FmtOptions.

I'm not sure whether the defaults for width and goal, which is derived from width, are correct. Or whether they differ from the ones used by GNU fmt on purpose. GNU fmt uses a width of 75 as default, whereas we use 79.

@cakebaker cakebaker force-pushed the fmt_implement_default_for_fmtoptions branch from f711d6d to 7644a75 Compare April 18, 2023 08:52
@cakebaker cakebaker force-pushed the fmt_implement_default_for_fmtoptions branch from 7644a75 to 167ac6e Compare April 28, 2023 14:56
@cakebaker
Copy link
Contributor Author

In the latest push I adapted the defaults to match those of GNU fmt.

@uutils uutils deleted a comment from github-actions bot Apr 28, 2023
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/fmt/long-line is no longer failing!
Congrats! The gnu test tests/misc/paste is no longer failing!

));
}
fmt_opts.goal = cmp::min(fmt_opts.width * 94 / 100, fmt_opts.width - 3);
fmt_opts.goal = cmp::min(fmt_opts.width * 93 / 100, fmt_opts.width - 3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to move 93 into a const with a good variable name?
to make it more clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope it is more clear now.

@cakebaker cakebaker force-pushed the fmt_implement_default_for_fmtoptions branch from 167ac6e to ec44577 Compare April 29, 2023 15:04
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/fmt/long-line is no longer failing!

@cakebaker cakebaker force-pushed the fmt_implement_default_for_fmtoptions branch from ec44577 to 21d30a9 Compare May 31, 2023 08:36
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/fmt/long-line is no longer failing!
GNU test failed: tests/tail-2/inotify-dir-recreate. tests/tail-2/inotify-dir-recreate is passing on 'main'. Maybe you have to rebase?

};
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(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really have anything to do with your change, but are we sure this is right?

I also think the way this is implemented is a bit strange, maybe it would be better in a match expression:

let width_opt = matches.get_one::<String>(OPT_WIDTH);
let goal_opt = matches.get_one::<String>(OPT_GOAL);
match (width_opt, goal_opt) {
    (Some(width), Some(goal)) => {...},
    ...
}

Maybe we can make a good-first-issue for someone to clean this up a bit.

@@ -155,7 +169,10 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, FmtOptions)
}
};
if !matches.get_flag(OPT_WIDTH) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to your change, I'm getting a runtime error here because OPT_WIDTH is not marked as a flag in clap:

thread 'main' panicked at 'Mismatch between definition and access of `width`. Could not downcast to bool, need to downcast to alloc::string::String
', /home/terts/Programming/uutils/coreutils/src/uu/fmt/src/fmt.rs:171:21

@cakebaker cakebaker force-pushed the fmt_implement_default_for_fmtoptions branch from 21d30a9 to 09db8d8 Compare May 31, 2023 14:42
@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/fmt/long-line is no longer failing!

@tertsdiepraam
Copy link
Collaborator

I already approved this, but consider this a second approval if the checks are now green (something was up in head, which seemed unrelated).

@github-actions
Copy link

GNU testsuite comparison:

Congrats! The gnu test tests/fmt/long-line is no longer failing!

@tertsdiepraam tertsdiepraam merged commit 84bfbb0 into uutils:main Aug 11, 2023
@cakebaker cakebaker deleted the fmt_implement_default_for_fmtoptions branch August 11, 2023 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants