The bug
The current algorithm doesn't take into account the length of "..." properly:
from click.utils import make_default_short_help
out = make_default_short_help('1234 67890 xxxxxx', max_length=10)
print(out)
print('Length:', len(out))
Prints:
Expected output: 1234...
Environment
- Click version: 7.2 and master
Solution
I'm going to open a PR.
Related issue (or: why this bug has never been reported)
The reason this bug has not been reported is because another "issue" compensates for it, making sure it never manifests itself in the terminal. The following code is from Group.format_commands:
|
# allow for 3 times the default spacing |
|
if len(commands): |
|
limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands) |
|
|
|
rows = [] |
|
for subcommand, cmd in commands: |
|
help = cmd.get_short_help_str(limit) |
|
rows.append((subcommand, help)) |
Group.format_commands needs to know col_spacing to calculate the width of the 2nd column (i.e. the short help limit). But it:
- doesn't set
col_spacing in formatter.write_dl and,
- it assumes that the formatter won't necessarily use the default
col_spacing value (note that the only way to change it is to subclass HelpFormatter and override write_dl; not a very clean use of inheritance).
As a consequence, it makes the conservative choice of "allowing for 3 times the default col_spacing" subtracting 6 to limit (i.e. max_length). Because col_spacing will always be 2, the limit will always be 4 characters smaller than needed, compensating for the eventual excess of 3 characters due to the bug reported in this issue.
Let me know if I misunderstood something.
The bug
The current algorithm doesn't take into account the length of "..." properly:
Prints:
Expected output:
1234...Environment
Solution
I'm going to open a PR.
Related issue (or: why this bug has never been reported)
The reason this bug has not been reported is because another "issue" compensates for it, making sure it never manifests itself in the terminal. The following code is from
Group.format_commands:click/src/click/core.py
Lines 1501 to 1508 in 0dc6d74
Group.format_commandsneeds to knowcol_spacingto calculate the width of the 2nd column (i.e. the short helplimit). But it:col_spacinginformatter.write_dland,col_spacingvalue (note that the only way to change it is to subclassHelpFormatterand overridewrite_dl; not a very clean use of inheritance).As a consequence, it makes the conservative choice of "allowing for 3 times the default
col_spacing" subtracting 6 tolimit(i.e.max_length). Becausecol_spacingwill always be 2, thelimitwill always be 4 characters smaller than needed, compensating for the eventual excess of 3 characters due to the bug reported in this issue.Let me know if I misunderstood something.