2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-10-09 14:14:23 +00:00
|
|
|
import curses
|
2020-10-09 15:04:32 +00:00
|
|
|
from types import TracebackType
|
2020-10-09 14:14:23 +00:00
|
|
|
|
2020-10-09 15:04:32 +00:00
|
|
|
|
2020-11-10 20:32:42 +00:00
|
|
|
class TermManager: # pragma: no cover
|
2020-11-18 11:27:59 +00:00
|
|
|
"""
|
|
|
|
The TermManager object initializes the terminal, returns a screen object and
|
|
|
|
de-initializes the terminal after use
|
|
|
|
"""
|
2020-10-09 14:14:23 +00:00
|
|
|
def __init__(self):
|
|
|
|
self.screen = curses.initscr()
|
|
|
|
# convert escapes sequences to curses abstraction
|
|
|
|
self.screen.keypad(True)
|
|
|
|
# stop printing typed keys to the terminal
|
|
|
|
curses.noecho()
|
|
|
|
# send keys through without having to press <enter>
|
|
|
|
curses.cbreak()
|
|
|
|
# make cursor invisible
|
|
|
|
curses.curs_set(False)
|
2020-12-11 15:56:22 +00:00
|
|
|
# Catch mouse events
|
|
|
|
curses.mousemask(True)
|
2020-11-10 21:30:55 +00:00
|
|
|
# Enable colors
|
|
|
|
curses.start_color()
|
2020-10-09 14:14:23 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2020-10-09 15:04:32 +00:00
|
|
|
return self
|
2020-10-09 14:14:23 +00:00
|
|
|
|
2020-10-09 15:04:32 +00:00
|
|
|
def __exit__(self, exc_type: type, exc_value: Exception,
|
|
|
|
exc_traceback: TracebackType) -> None:
|
2020-10-09 14:14:23 +00:00
|
|
|
# restore the terminal to its original state
|
|
|
|
self.screen.keypad(False)
|
|
|
|
curses.echo()
|
|
|
|
curses.nocbreak()
|
|
|
|
curses.curs_set(True)
|
|
|
|
curses.endwin()
|