Debian Codegraph @

git_delete_merged_branches/_messenger.py

raw symbols SCIP
1 # Copyright (C) 2020 Sebastian Pipping <[email protected]>
2 # Licensed under GPL v3 or later
3
4 import sys
5
6 import colorama
7
8 from ._shell import escape_for_shell_display
9
10 _INFO_COLOR = colorama.Fore.WHITE + colorama.Style.BRIGHT
11 _ERROR_COLOR = colorama.Fore.RED + colorama.Style.BRIGHT
12 _COMMAND_COLOR = colorama.Fore.CYAN
13 _QUESTION_COLOR = colorama.Fore.GREEN + colorama.Style.BRIGHT
14 _RESET_COLOR = colorama.Style.RESET_ALL
15
16
17 class Messenger:
18 def __init__(self, colorize):
19 self._colorize = colorize
20
21 # Multi-line block of text should by separated from consecutive output (if any)
22 # by a blank line to give it some "air". This flag is a tiny state machine.
23 self._air_needed = False
24
25 def produce_air(self):
26 if self._air_needed:
27 print()
28 self._air_needed = False
29
30 def request_air(self, future_message):
31 if "\n" in future_message:
32 self._air_needed = True
33
34 def _produce_and_request_air(self, future_message):
35 self.produce_air()
36 self.request_air(future_message)
37
38 def tell_info(self, message):
39 self._produce_and_request_air(message)
40 if self._colorize:
41 message = f"{_INFO_COLOR}{message}{_RESET_COLOR}"
42 print(message)
43
44 def tell_command(self, argv, comment):
45 self._produce_and_request_air("")
46 epilog = f" # {comment}" if comment else ""
47 argv = [escape_for_shell_display(arg) for arg in argv]
48 message = f"# {' '.join(argv)}"
49 if self._colorize:
50 message = f"{_COMMAND_COLOR}{message}{_RESET_COLOR}"
51 message += epilog
52
53 print(message, file=sys.stderr)
54
55 def tell_error(self, message):
56 self._produce_and_request_air(message)
57 message = f"Error: {message}"
58 if self._colorize:
59 message = f"{_ERROR_COLOR}{message}{_RESET_COLOR}"
60 print(message, file=sys.stderr)
61
62 def format_question(self, message):
63 if self._colorize:
64 message = f"{_QUESTION_COLOR}{message}{_RESET_COLOR}"
65 return message