+from sys import stderr
+from typing import Never
from mazegen.config.parser_combinator import ParseError
from mazegen.display.observer import MazeRegenerate, TTYTracker
+from mazegen.display.tty import BackendException
from mazegen.maze import (
Maze,
Pattern,
make_pacman,
make_perfect,
)
-from mazegen.config.config_parser import Config
+from mazegen.config.config_parser import Config, ConfigError
from mazegen.maze.output import format_output
import random
config_filename = "./example.conf"
config_str = open(config_filename).read()
+
+
+def error(s: str) -> Never:
+ print("Error:", file=stderr)
+ print(s, end="", file=stderr)
+ exit(1)
+
+
try:
config = Config.parse(config_str)
except ParseError as e:
- print(e.pretty_format(config_str, config_filename))
- exit(1)
+ error(e.pretty_format(config_str, config_filename))
+except ConfigError as e:
+ error(e.args[0] + "\n")
if config.seed is not None:
random.seed(config.seed)
pacman_tracker = PacmanTracker(maze)
network_tracker = NetworkTracker(maze)
-tty_tracker = TTYTracker(maze, config) if config.visual else None
+try:
+ tty_tracker = TTYTracker(maze, config) if config.visual else None
+except BackendException as e:
+ error(e.args[0] + "\n")
excluded = {maze.entry, maze.exit}
"""
Extracts the color pairs from the config, and maps them to text
attributes
- May raise a backend exception if too many colors are used or an invalid
- variable color is used
+ May raise a backend exception if too many colors are used, an invalid
+ variable color is used, or the terminal doesn't support setting colors
+ but custom colors were used
"""
all_tilemaps = [
e
raise BackendException("Unknown color " + var_color + " in config")
res_colors[var_color] = color_lookup[var_color]
available_colors -= {color_lookup[var_color]}
+ if len(value_colors) != 0 and not curses.can_change_color():
+ raise BackendException(
+ "Cannot set colors for terminal, "
+ + "please check that your terminal is configured correctly "
+ + "or only use named colors in config"
+ )
if len(available_colors) < len(value_colors):
raise BackendException(
"Too many value color values in config: "
self.__dims * IVec2.splat(2) + IVec2.splat(1)
)
- self.__uninit: bool = False
- self.__screen: curses.window = curses.initscr()
+ self.__uninit: bool = True
+ try:
+ self.__screen: curses.window = curses.initscr()
+ except curses.error as e:
+ raise BackendException(
+ "Failed to initiate screen, "
+ + "check that your terminal is setup correctly"
+ )
+ self.__uninit = False
self.__screen.timeout(0)
curses.start_color()
curses.noecho()
curses.curs_set(0)
self.__screen.keypad(True)
- pair_map = extract_pairs(config)
+ try:
+ pair_map = extract_pairs(config)
+ except BackendException as e:
+ self.uninit()
+ raise e
self.tilemaps = TileMaps(config, pair_map, self)
self.__scratch: curses.window = curses.newpad(1, 1)