Skip to content
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

fmt: implement Default for FmtOptions #4747

Merged

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!

@@ -140,7 +148,7 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, FmtOptions)
),
));
}
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?

@@ -155,7 +169,10 @@ fn parse_arguments(args: impl uucore::Args) -> UResult<(Vec<String>, 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(
Copy link
Member

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
Member

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

src/uu/fmt/src/fmt.rs Outdated Show resolved Hide resolved
@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
Member

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
45 checks passed
@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