File: manual.in.py

package info (click to toggle)
crazy-complete 0.3.7-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 13,342; sh: 995; makefile: 68
file content (156 lines) | stat: -rw-r--r-- 3,803 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>

'''Built-int Manual.'''

from .errors import CrazyError
from .str_utils import indent, join_with_wrap


# flake8: noqa: E501
# pylint: disable=line-too-long
# pylint: disable=too-many-lines


%COMMANDS%


class TerminalFormatter:
    '''Format string for terminals.'''

    def __init__(self, use_colors, tab_width):
        self.use_colors = use_colors
        self.tab_width = tab_width
        self.tab = ' ' * tab_width

    def bold(self, s):
        '''Make bold string.'''

        if not self.use_colors:
            return s

        return f"\033[1m{s}\033[0m"

    def underline(self, s):
        '''Make underline string.'''

        if not self.use_colors:
            return s

        return f"\033[4m{s}\033[0m"


def _make_command_section(command, formatter):
    r = formatter.bold('COMMAND')
    r += f'\n{formatter.tab}{command["command"]}'
    return r


def _make_description_section(command, formatter):
    r = formatter.bold('DESCRIPTION')
    r += f'\n{formatter.tab}{command["short"]}'

    if command['long']:
        r += '\n\n'

        if formatter.use_colors:
            r += indent(command['long_colored'].strip(), formatter.tab_width)
        else:
            r += indent(command['long'].strip(), formatter.tab_width)

    return r


def _make_example_section(command, formatter):
    r = formatter.bold('EXAMPLE')
    r += '\n'

    if formatter.use_colors:
        r += indent(command['definition_colored'].strip(), formatter.tab_width)
    else:
        r += indent(command['definition'].strip(), formatter.tab_width)

    return r


def _make_output_section(command, formatter):
    r = formatter.bold('OUTPUT')
    r += '\n'
    r += indent(command['output'].strip(), formatter.tab_width)
    return r


def _make_notes_sectiong(notes, formatter):
    if not notes:
        return ''

    r = [formatter.bold('NOTES')]

    for note in notes:
        note = f'{formatter.tab}- {note}'
        r.append(note)

    return '\n'.join(r)


def _make_see_also_section(command, formatter):
    if not command['also']:
        return ''

    r = formatter.bold('SEE ALSO')
    r += '\n'

    for also_cmd, also_desc in command['also'].items():
        r += f'{formatter.tab}{formatter.bold(also_cmd)}: {also_desc}\n'

    return r.rstrip()


def print_help_for_command(name, use_colors):
    '''Print a manual like help for command `name`.'''

    command = None
    for cmd in COMMANDS:
        if cmd['command'] == name:
            command = cmd
            break

    if not command:
        raise CrazyError("Command not found: %s" % name)

    formatter = TerminalFormatter(use_colors, 4)

    notes = list(command['notes'])
    if command['implemented']:
        r = 'This completer is currently only implemented in '
        r += command['implemented'][0]
        if len(command['implemented']) > 1:
            r += ' and '
            r += command['implemented'][1]
        notes.append(r)

    r = [
        _make_command_section(command, formatter),
        _make_description_section(command, formatter),
        _make_example_section(command, formatter),
        _make_output_section(command, formatter),
        _make_notes_sectiong(notes, formatter),
        _make_see_also_section(command, formatter),
    ]

    print()
    print('\n\n'.join(l for l in r if l))
    print()


def print_help_topic(topic, use_colors):
    '''Print help for `topic`.'''

    try:
        print_help_for_command(topic, use_colors)
    except CrazyError:
        commands = [cmd['command'] for cmd in COMMANDS]
        print('Topic not found')
        print('')
        print('Available completers:')
        print(indent(join_with_wrap(' ', '\n', 40, commands), 4))