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

Feature request: Footer formatting and terminal geometry #355

Open
sergeyklay opened this issue Nov 30, 2019 · 6 comments
Open

Feature request: Footer formatting and terminal geometry #355

sergeyklay opened this issue Nov 30, 2019 · 6 comments

Comments

@sergeyklay
Copy link

sergeyklay commented Nov 30, 2019

Hello,

I would like to format footer output using configured width as well as to take into account terminal geometry.

Consider the following example:

$ ./app subcommand --help

SYNOPSIS
  subcommand [OPTIONS]

OPTIONS
  --option1 TEXT Some description for this option
  --flag1        Some description for this flag
  --flag2        Some description for this flag
  -h,--help      Print this help message and quit

DESCRIPTION
Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n
sed do eiusmod tempor incididunt ut labore et dolor\n
magna aliqua. Ut enim ad minim veniam, quis nostrud\n
exercitation ullamco laboris nisi ut aliquip ex ea commodo\n
consequat. Duis aute irure dolor in reprehenderit in voluptate\n
velit esse cillum dolore eu fugiat nulla pariatur.\n

Excepteur sint occaecat cupidatat non proident, sunt in culpa\n
qui officia deserunt mollit anim id est laborum.\n

Here DESCRIPTION is the footer text. And right now I forced to split string manually using "\n". I do not want to split the footer string manually. I want pass the whole string to the footer() and not worried about line breaks. I would like to calculate terminal geometry (internally) and set footer width at CLI11 configuring time. Some examples:

app.footer_width(78);
cmd.get_fooler().width(78);
cmd->footer(string, 78);

This is probably an unrelated task, one would be even better if I could align the text to the right border. For example:
image

@sergeyklay sergeyklay changed the title Footer formatting and terminal geometry Feature request: Footer formatting and terminal geometry Dec 1, 2019
@phlptp
Copy link
Collaborator

phlptp commented Dec 3, 2019

I believe the formatter that does the printing is polymorphic so you might be able customize the output to meet the requirements. @henryiii would know more, I haven't used that feature.

@henryiii
Copy link
Collaborator

Wow, haven't seen fixed width full alignment before. I guess one of the next things to work on is proper docs for the formatter. You can replace parts or the whole. And I'm happy to take improvements to the default. We could also have a library of user submitted formatters, I suppose...

@flagarde
Copy link

Hi,
If you don't mind relying on other library maybe fmt could help https://fmt.dev/latest/syntax.html
Aligning the text and specifying a width:

fmt::format("{:<30}", "left aligned");
// Result: "left aligned                  "
fmt::format("{:>30}", "right aligned");
// Result: "                 right aligned"
fmt::format("{:^30}", "centered");
// Result: "           centered           "
fmt::format("{:*^30}", "centered");  // use '*' as a fill char
// Result: "***********centered***********"

Dynamic width:

fmt::format("{:<{}}", "left aligned", 30);
// Result: "left aligned     

@jakoblover
Copy link
Contributor

I very much enjoy the fact that CLI11 does not require external dependencies, please don't make users include fmt. I like the idea of user-submitted formatters though.

@flagarde
Copy link

Yes I agree I enjoy it too !
But fmt is an header library too i think and it could be included with gitmodule , cmake externalpackage etc...
it looks fmt has a quite permissive license maybe some part of the code could be used

@jakoblover
Copy link
Contributor

I definitely think there should be a helpformatter that does include fmt, but that fmt should not be required to compile cli11. I might get something up and running.

phlptp added a commit that referenced this issue Oct 7, 2024
_This is the new PR I've mentioned to work on in PR #858_

## A better Help Formatter
_See below for images of the new help page_

Finally, after a lot of planning, understanding CLI11's codebase,
testing and coding, the new default Help Formatter is done. There are a
lot of changes to make the help page more readable and closer to UNIX
standards, see Changelog below for details. One of the highlights is
automatic paragraph formatting with correct line wrapping for App and
options/flag descriptions as well as the footer.
A goal was to provide more flexibility and better readability for the
help page while providing full compatibility with Apps using CLI11 (no
breaking changes and no changes to Apps required). Also better support
for different terminal sizes. Users can now specify three new optional
attributes: `right_column_width_`, `description_paragraph_width_` and
`footer_paragraph_width_`. See code documentation for more details. The
different columns for options/flags now scale with the set
`column_width_` value: Single dash flags occupy 33% of the set
`column_width_`, double dash flags and options (like REQUIRED) 66%.
These new attributes allow for indirectly respecting terminal geometry,
footer paragraph formatting has also been added (#355). This PR also
implements the issues #353 and #856.
The new help page formatting can also be used as an input for man page
generation, since it's oriented on the man page style (#413).
[help2man](https://www.gnu.org/software/help2man/) can be used to
generate man pages from help output (see comment down below for
example).

I thoroughly tested this code with all possible combinations of flags,
options, positionals, subcommands, validators, ...
So far everything works great.
I hope this PR looks good and meets all requirements. I'm looking
forward to the implementation of this PR into CLI11. If you have any
questions or suggestions feel free to comment.

### Fixed/implemented issues by this PR
- #353 Better options formatting
- #856 Space between options
- #355 Footer formatting
- #413 Man page generation can be achieved using help2man with the new
help formatting
- #384 (comment)
Better help formatting can be marked as complete

### What about the failing tests?
Of course the tests expect the old help text format. This is why 6 of
the tests are failing. Since it is a bit of work to migrate the tests to
the new help format, I first wanted to push out this PR and get
confirmation before I'll update all the tests.
So please let me know if this PR gets implemented, what changes should
be made and then I'll migrate the tests to the new help format, either
in this PR or I'll make a new one.

## Changelog:
#### There are _no breaking changes_. Every App using CLI11 will work
with this new formatter with no changes required.
- Added empty lines at beginning and end of help text
- Removed double new-line between option groups for consistency. Now all
sections have the same number of new-lines
- Switched usage and description order
- Only show "Usage"-string if no App name is present. This provides
better readability
- Made categories (Options, Positionals, ...) capital
- Changed `ConfigBase::to_config` to correctly process capital
"OPTIONS"-group (only affects descriptions of the config file, not a
breaking change)
- Added a paragraph formatter function `streamOutAsParagraph` to
StringTools.hpp
- Made "description" a paragraph block with correct, word respecting
line wrapping and indentation (using the new paragraph formatter
function)
- Made the footer a paragraph block with correct, word respecting line
wrapping and indentation
- Updated documentation for `column_width_` to make it more clear
- Added new member: `right_column_width_`, added getter and setter for
`right_column_width_`
- Added new member: `description_paragraph_width_`, added getter and
setter for `description_paragraph_width_`
- Added new member: `footer_paragraph_width_`, added getter and setter
for `footer_paragraph_width_ `
- Positionals description are now formatted as paragraph with correct,
word respecting line wrapping
- Options description are now formatted as paragraph with correct, word
respecting line wrapping
- Short and long options/flags/names are now correctly formatted to
always be at the right position (also for subcommand options/flags)
- Short and long options/flags/names column widths scale linearly with
the `column_width_` attribute to better adapt to different
`column_width_` sizes
- Merged PR #860

## What's planned for the future?
- I'm thinking of better formatting the options of flags (like REQUIRED,
TEXT, INT, ...) and make them also in a seperate column. This way they
would also always be at the same position. However I decided against it
for this PR, since I wanted them to be as close as possible to the
actual flag. With my implementation it is quite easy to add this change
in the future.
- Subcommands: I'm planning on better formatting the Subcommands. With
this PR only the short and long flags/options of subcommands are better
formatted (like it is with the main flags, see images down below).
- Maybe implement a different way to display expected data type options
(TEXT, INT, ...). For example: `--file-name=<TEXT>` for long flags only
and if `disable_flag_override_` is false.
- Maybe add something like this:
#554

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Philip Top <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants