Skip to content

Commit

Permalink
move short_help printing logic command printer
Browse files Browse the repository at this point in the history
to address pallets#936 and related issues it seems cleaner to remove this
function and let the formatter print the command block
  • Loading branch information
fdavis committed May 14, 2018
1 parent 29cc352 commit f465aab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
48 changes: 32 additions & 16 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,12 +769,14 @@ def __init__(self, name, context_settings=None, callback=None,
self.epilog = epilog
self.options_metavar = options_metavar
if short_help is None and help:
try:
max_width = min(context_settings.get('max_content_width'),
get_terminal_size()[0])
echo('trying to max width with %s' % max_width)
short_help = make_default_short_help(help, max_width)
except (AttributeError, TypeError) as e:
if (context_settings is not None and
isinstance(context_settings.get('max_content_width'), int)):
max_width = min(context_settings.get('max_content_width'),
get_terminal_size()[0])
# arbitrary trim width, if a subcommond is longer than the
# it get printed poorly
short_help = make_default_short_help(help, max_width - 20)
else:
short_help = make_default_short_help(help)
self.short_help = short_help
self.add_help_option = add_help_option
Expand Down Expand Up @@ -1013,16 +1015,30 @@ def format_commands(self, ctx, formatter):
after the options.
"""
rows = []
for subcommand in self.list_commands(ctx):
cmd = self.get_command(ctx, subcommand)
# What is this, the tool lied about a command. Ignore it
if cmd is None:
continue
if cmd.hidden:
continue

help = cmd.short_help or ''
rows.append((subcommand, help))
# Ignore errant none case and hidden commands
commands = [self.get_command(ctx, cmd) for cmd in self.list_commands(ctx)
if cmd is not None]
commands = [cmd for cmd in commands if not cmd.hidden]
if len(commands) > 0:
name_length = lambda c: len(c.name)
# formater adds wraps additional spaces
command_names_width = max(map(name_length,commands)) + 4
ctx_width = ctx.max_content_width or ctx.terminal_width
if ctx_width is not None:
max_width = min(ctx_width, get_terminal_size()[0])
else:
max_width = get_terminal_size()[0]
# add space to allow for truncating word with elipse
max_width = max_width - command_names_width - 6
for subcommand in commands:
if subcommand.help is None:
help = ''
else:
if len(subcommand.help) < max_width:
help = subcommand.help
else:
help = make_default_short_help(subcommand.help, max_width)
rows.append((subcommand.name, help))

if rows:
with formatter.section('Commands'):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def dist(ctx, count):


def test_auto_shorthelp(runner):
@click.group()
@click.group(context_settings={'max_content_width': 67})
def cli():
pass

Expand Down

0 comments on commit f465aab

Please sign in to comment.