From 08654ff9a2b18a6f68ca21339aa5f8d9af4d5cc1 Mon Sep 17 00:00:00 2001 From: Antoine Stevan Date: Mon, 5 Jul 2021 19:21:10 +0200 Subject: [PATCH] Fixed the wrapper issue. --- src/errors.py | 33 ++++++++++++++++++++++++++++++--- src/main.py | 13 +++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/errors.py b/src/errors.py index 6507f7c..dc528da 100644 --- a/src/errors.py +++ b/src/errors.py @@ -1,3 +1,8 @@ +import curses + +from src.utils import log + + class CustomError(Exception): pass @@ -18,14 +23,36 @@ class CustomError(Exception): # def __str__(self): # return f'{self.salary} -> {self.message}' -def error_handler(func): +def curses_wrapper(func): + def wrapper(*args, **kwargs): + log("init") + stdscr = curses.initscr() + curses.noecho() + curses.cbreak() + stdscr.keypad(True) + curses.start_color() + try: + log("main") + func(stdscr=stdscr, *args, **kwargs) + finally: + log("quit") + curses.nocbreak() + stdscr.keypad(False) + curses.echo() + curses.endwin() + + return wrapper + + +def error_handler_wrapper(func): def wrapper(*args, **kwargs): try: - res = func(*args, **kwargs) - return res + return func(*args, **kwargs) except CustomError as ce: + log("CustomError") print(ce) except KeyboardInterrupt: + log("KeyboardInterrupt") pass return wrapper diff --git a/src/main.py b/src/main.py index fcae0ef..da9b7d7 100644 --- a/src/main.py +++ b/src/main.py @@ -6,8 +6,9 @@ from src.Apple import Apple from src.Snake import Snake -from src.errors import error_handler from src.errors import CustomError +from src.errors import curses_wrapper +from src.errors import error_handler_wrapper from src.game import _wall from src.game import PLAY @@ -19,7 +20,8 @@ from src.game import blit -@error_handler +@error_handler_wrapper +@curses_wrapper def main(stdscr): # global game parameters. fps = 15 @@ -30,8 +32,11 @@ def main(stdscr): # check if everything can work properly. if (curses.LINES < sh + 2) or (curses.COLS < sw + 2): + d_lines, d_cols = max(0, sh + 2 - curses.LINES), max(0, sw + 2 - curses.COLS) msg = f"terminal too small.\n" \ - f"expected at least {sw + 2, sh + 2} for a {sw, sh} game board, got {curses.COLS, curses.LINES}" + f"expected at least {sw + 2, sh + 2} for a {sw, sh} game board, got {curses.COLS, curses.LINES}\n" \ + f"please increase terminal's width by {d_cols} pixel{'s' if d_cols > 1 else ''} and " \ + f"its height by {d_lines} pixel{'s' if d_lines > 1 else ''}" raise CustomError(msg) if curses.can_change_color(): @@ -83,4 +88,4 @@ def main(stdscr): if __name__ == "__main__": - curses.wrapper(main) + main()