| 1 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
# Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]> |
| 3 |
|
| 4 |
'''Built-int Manual.''' |
| 5 |
|
| 6 |
from .errors import CrazyError |
| 7 |
from .str_utils import indent, join_with_wrap |
| 8 |
|
| 9 |
|
| 10 |
# flake8: noqa: E501 |
| 11 |
# pylint: disable=line-too-long |
| 12 |
# pylint: disable=too-many-lines |
| 13 |
|
| 14 |
|
| 15 |
%COMMANDS% |
| 16 |
|
| 17 |
|
| 18 |
class TerminalFormatter: |
| 19 |
'''Format string for terminals.''' |
| 20 |
|
| 21 |
def __init__(self, use_colors, tab_width): |
| 22 |
self.use_colors = use_colors |
| 23 |
self.tab_width = tab_width |
| 24 |
self.tab = ' ' * tab_width |
| 25 |
|
| 26 |
def bold(self, s): |
| 27 |
'''Make bold string.''' |
| 28 |
|
| 29 |
if not self.use_colors: |
| 30 |
return s |
| 31 |
|
| 32 |
return f"\033[1m{s}\033[0m" |
| 33 |
|
| 34 |
def underline(self, s): |
| 35 |
'''Make underline string.''' |
| 36 |
|
| 37 |
if not self.use_colors: |
| 38 |
return s |
| 39 |
|
| 40 |
return f"\033[4m{s}\033[0m" |
| 41 |
|
| 42 |
|
| 43 |
def _make_command_section(command, formatter): |
| 44 |
r = formatter.bold('COMMAND') |
| 45 |
r += f'\n{formatter.tab}{command["command"]}' |
| 46 |
return r |
| 47 |
|
| 48 |
|
| 49 |
def _make_description_section(command, formatter): |
| 50 |
r = formatter.bold('DESCRIPTION') |
| 51 |
r += f'\n{formatter.tab}{command["short"]}' |
| 52 |
|
| 53 |
if command['long']: |
| 54 |
r += '\n\n' |
| 55 |
|
| 56 |
if formatter.use_colors: |
| 57 |
r += indent(command['long_colored'].strip(), formatter.tab_width) |
| 58 |
else: |
| 59 |
r += indent(command['long'].strip(), formatter.tab_width) |
| 60 |
|
| 61 |
return r |
| 62 |
|
| 63 |
|
| 64 |
def _make_example_section(command, formatter): |
| 65 |
r = formatter.bold('EXAMPLE') |
| 66 |
r += '\n' |
| 67 |
|
| 68 |
if formatter.use_colors: |
| 69 |
r += indent(command['definition_colored'].strip(), formatter.tab_width) |
| 70 |
else: |
| 71 |
r += indent(command['definition'].strip(), formatter.tab_width) |
| 72 |
|
| 73 |
return r |
| 74 |
|
| 75 |
|
| 76 |
def _make_output_section(command, formatter): |
| 77 |
r = formatter.bold('OUTPUT') |
| 78 |
r += '\n' |
| 79 |
r += indent(command['output'].strip(), formatter.tab_width) |
| 80 |
return r |
| 81 |
|
| 82 |
|
| 83 |
def _make_notes_sectiong(notes, formatter): |
| 84 |
if not notes: |
| 85 |
return '' |
| 86 |
|
| 87 |
r = [formatter.bold('NOTES')] |
| 88 |
|
| 89 |
for note in notes: |
| 90 |
note = f'{formatter.tab}- {note}' |
| 91 |
r.append(note) |
| 92 |
|
| 93 |
return '\n'.join(r) |
| 94 |
|
| 95 |
|
| 96 |
def _make_see_also_section(command, formatter): |
| 97 |
if not command['also']: |
| 98 |
return '' |
| 99 |
|
| 100 |
r = formatter.bold('SEE ALSO') |
| 101 |
r += '\n' |
| 102 |
|
| 103 |
for also_cmd, also_desc in command['also'].items(): |
| 104 |
r += f'{formatter.tab}{formatter.bold(also_cmd)}: {also_desc}\n' |
| 105 |
|
| 106 |
return r.rstrip() |
| 107 |
|
| 108 |
|
| 109 |
def print_help_for_command(name, use_colors): |
| 110 |
'''Print a manual like help for command `name`.''' |
| 111 |
|
| 112 |
command = None |
| 113 |
for cmd in COMMANDS: |
| 114 |
if cmd['command'] == name: |
| 115 |
command = cmd |
| 116 |
break |
| 117 |
|
| 118 |
if not command: |
| 119 |
raise CrazyError("Command not found: %s" % name) |
| 120 |
|
| 121 |
formatter = TerminalFormatter(use_colors, 4) |
| 122 |
|
| 123 |
notes = list(command['notes']) |
| 124 |
if command['implemented']: |
| 125 |
r = 'This completer is currently only implemented in ' |
| 126 |
r += command['implemented'][0] |
| 127 |
if len(command['implemented']) > 1: |
| 128 |
r += ' and ' |
| 129 |
r += command['implemented'][1] |
| 130 |
notes.append(r) |
| 131 |
|
| 132 |
r = [ |
| 133 |
_make_command_section(command, formatter), |
| 134 |
_make_description_section(command, formatter), |
| 135 |
_make_example_section(command, formatter), |
| 136 |
_make_output_section(command, formatter), |
| 137 |
_make_notes_sectiong(notes, formatter), |
| 138 |
_make_see_also_section(command, formatter), |
| 139 |
] |
| 140 |
|
| 141 |
print() |
| 142 |
print('\n\n'.join(l for l in r if l)) |
| 143 |
print() |
| 144 |
|
| 145 |
|
| 146 |
def print_help_topic(topic, use_colors): |
| 147 |
'''Print help for `topic`.''' |
| 148 |
|
| 149 |
try: |
| 150 |
print_help_for_command(topic, use_colors) |
| 151 |
except CrazyError: |
| 152 |
commands = [cmd['command'] for cmd in COMMANDS] |
| 153 |
print('Topic not found') |
| 154 |
print('') |
| 155 |
print('Available completers:') |
| 156 |
print(indent(join_with_wrap(' ', '\n', 40, commands), 4)) |