# SPDX-License-Identifier: GPL-3.0-or-later # Copyright (C) 2025-2026 Benjamin Abendroth '''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 additional 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))