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

feat: strip and truncate doc strings #14

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion click_command_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _print_tree(command, depth=0, is_last_item=False, is_last_parent=False):
tree_item = '└── ' if is_last_item else '├── '

line = prefix * (depth - 1) + tree_item + command.name
doc = command.command.__doc__
doc = _get_truncated_docstring(command.command)
if doc:
line += ' - {}'.format(doc)

Expand All @@ -56,3 +56,23 @@ def _print_tree(command, depth=0, is_last_item=False, is_last_parent=False):
depth=(depth + 1),
is_last_item=(i == (len(command.children) - 1)),
is_last_parent=is_last_item)


def _get_truncated_docstring(command):
doc = command.__doc__

if not doc:
return None

lines = doc.split("\n")
if not lines:
return None

for line in lines:
line = line.strip()
if not line:
continue

return line[:80] + ' ...' if len(line) > 80 else line

return None
20 changes: 20 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,26 @@ def command_three():
root
├── command-one
└── command-three
"""[1:]

self._assert_correct_output(root, expected_output)

def test_long_docstring(self):
@click.group(name='root')
def root():
pass

@root.command(name='command')
def command():
"""
this is a really long multi line doc string this is a really long multi line doc string
this is a really long multi line doc string this is a really long multi line doc string
"""
pass

expected_output = """
root
└── command - this is a really long multi line doc string this is a really long multi line doc ...
"""[1:]

self._assert_correct_output(root, expected_output)
Expand Down
Loading