151 lines
4.5 kB
1"""Terminal views of a run."""
2
3import sys
4from typing import Final
5
6from ..scenario import Scenario
7from ..schedule import Move
8from .base import Renderer
9
10RESET: Final[str] = "\x1b[0m"
11ANSI_COLORS: Final[dict[str, str]] = {
12 "black": "\x1b[30m",
13 "red": "\x1b[31m",
14 "green": "\x1b[32m",
15 "yellow": "\x1b[33m",
16 "blue": "\x1b[34m",
17 "magenta": "\x1b[35m",
18 "purple": "\x1b[35m",
19 "cyan": "\x1b[36m",
20 "white": "\x1b[37m",
21 "gray": "\x1b[90m",
22 "grey": "\x1b[90m",
23 "orange": "\x1b[38;5;208m",
24 "pink": "\x1b[38;5;213m",
25 "crimson": "\x1b[38;5;160m",
26 "darkred": "\x1b[38;5;88m",
27 "maroon": "\x1b[38;5;52m",
28 "gold": "\x1b[38;5;220m",
29 "lime": "\x1b[38;5;118m",
30 "violet": "\x1b[38;5;135m",
31 "teal": "\x1b[38;5;30m",
32 "navy": "\x1b[38;5;18m",
33 "silver": "\x1b[38;5;250m",
34 "brown": "\x1b[38;5;130m",
35 "rainbow": "\x1b[38;5;201m",
36}
37ANSI_UNNAMED: Final[tuple[str, ...]] = (
38 "\x1b[38;5;109m",
39 "\x1b[38;5;144m",
40 "\x1b[38;5;139m",
41 "\x1b[38;5;151m",
42)
43
44
45def ansi_of(color: str | None) -> str | None:
46 """Pick the escape code a zone's name is printed in.
47
48 A color word the table does not know is not an error, since the
49 subject fixes no list of names. Rather than printing it plain,
50 which reads as a bug next to its coloured neighbours, the word is
51 spread over a few neutral codes by its letters: the same word
52 always lands on the same color.
53
54 Args:
55 color: The map's color= value, or None when it gave none.
56
57 Returns:
58 The escape code to wrap the name in, or None for an uncolored
59 zone.
60 """
61 if color is None:
62 return None
63 known = ANSI_COLORS.get(color.lower())
64 if known is not None:
65 return known
66 spread = sum(ord(letter) for letter in color)
67 return ANSI_UNNAMED[spread % len(ANSI_UNNAMED)]
68
69
70class TextRenderer(Renderer):
71 """Prints the graded output: one line of movements per turn.
72
73 The format is fixed by the subject, so nothing else reaches
74 standard output.
75 """
76
77 def begin(self, scenario: Scenario) -> None:
78 """Print nothing, since the output format allows no header.
79
80 Args:
81 scenario: The map about to be replayed, unused here.
82 """
83
84 def show_turn(self, index: int, moves: list[Move]) -> None:
85 """Print one turn's movements, space-separated.
86
87 A turn where nobody moves prints nothing rather than an empty
88 line, since such a turn contributes no movement to read.
89
90 Args:
91 index: 1-based number of the turn, unused here.
92 moves: The drones that moves this turn.
93 """
94 if not moves:
95 return
96 print(" ".join(self.format_move(move) for move in moves))
97
98 def finish(self, total_turns: int) -> None:
99 """Print nothing, since the output format allows no summary.
100
101 Args:
102 total_turns: Length of the run, unused here
103 """
104
105 def format_move(self, move: Move) -> str:
106 """Spell one movement.
107
108 Args:
109 move: The movement to spell.
110
111 Returns:
112 The plain D<ID>-<place> form.
113 """
114 return move.label()
115
116
117class ColorTextRenderer(TextRenderer):
118 """Prints the same lines, colored by destination zone.
119
120 The map's color= metadata drives the escape codes, through the
121 same rule the graphical view follows: a known word keeps its
122 meaning, an unknown one is given a stable color of its own. A zone
123 that named no color is the only one printed plain.
124 """
125
126 def __init__(self, enabled: bool | None = None) -> None:
127 """Choose whether to emit escape codes.
128
129 Args:
130 enabled: Force colors on or off. When omitted, colors are
131 used only if standard output is a terminal, so piping
132 the run to a file keeps the graded format clean.
133 """
134 self._enabled = sys.stdout.isatty() if enabled is None else enabled
135
136 def format_move(self, move: Move) -> str:
137 """Spell one movement, wrapped in its zone's color.
138
139 Args:
140 move: The movement to spell.
141
142 Returns:
143 The D<ID>-<place> form, colored when possible.
144 """
145 label = move.label()
146 if not self._enabled:
147 return label
148 escape = ansi_of(move.placement.current_zone().color)
149 if escape is None:
150 return label
151 return f"{escape}{label}{RESET}"