File: output.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 (82 lines) | stat: -rw-r--r-- 2,536 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
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2025-2026 Benjamin Abendroth <braph93@gmx.de>

'''Classes for generating file output.'''


_GENERATION_NOTICE = '''\
# This script was generated by crazy-complete.
# crazy-complete: A tool that creates robust and reliable autocompletion scripts for Bash, Fish and Zsh.
# For more information, visit: https://github.com/crazy-complete/crazy-complete'''


class Output:
    '''Class for generating output.'''

    def __init__(self, config, helpers):
        self.config = config
        self.helpers = helpers
        self.output = []

    def add(self, string):
        '''Add to output.'''
        self.output.append(string)

    def extend(self, strings):
        '''Add many to output.'''
        self.output.extend(strings)

    def add_as_block(self):
        '''Return a helper context manager for adding a multi-line block.'''

        class _Block:
            def __init__(self, parent):
                self.parent = parent
                self.lines = []

            def add(self, line):
                '''Add a single line to the current block.'''
                self.lines.append(line)

            def extend(self, lines):
                '''Add multiple lines to current block.'''
                self.lines.extend(lines)

            def __enter__(self):
                return self

            def __exit__(self, exc_type, exc_val, exc_tb):
                block_text = "\n".join(self.lines)
                self.parent.output.append(block_text)

        return _Block(self)

    def add_generation_notice(self):
        '''Add the generation notice.'''
        self.add(_GENERATION_NOTICE)

    def add_comments(self):
        '''Add additioanl comments.'''
        if self.config.comments:
            self.add('\n'.join(f'# {c}' for c in self.config.comments))

    def add_included_files(self):
        '''Add included files.'''
        for file in self.config.include_files:
            with open(file, 'r', encoding='utf-8') as fh:
                self.add(fh.read().strip())

    def add_helper_functions_code(self):
        '''Add helper functions.'''
        for code in self.helpers.get_used_functions_code():
            self.add(code)

    def add_vim_modeline(self, shell):
        '''Add the vim modeline.'''
        if self.config.vim_modeline:
            vim = 'vim'
            self.add(f'# {vim}: ft={shell} ts=2 sts=2 sw=2 et')

    def get(self):
        '''Return the output.'''
        return '\n\n'.join(filter(None, self.output))