diff options
Diffstat (limited to 'poezio/windows/base_wins.py')
-rw-r--r-- | poezio/windows/base_wins.py | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/poezio/windows/base_wins.py b/poezio/windows/base_wins.py index 6dabd7b8..658e1533 100644 --- a/poezio/windows/base_wins.py +++ b/poezio/windows/base_wins.py @@ -7,40 +7,37 @@ the text window, the roster window, etc. A Tab (see the poezio.tabs module) is composed of multiple Windows """ -TAB_WIN = None - -import logging -log = logging.getLogger(__name__) +from __future__ import annotations import curses +import logging import string -from typing import Optional, Tuple +from contextlib import contextmanager +from typing import Optional, Tuple, TYPE_CHECKING, cast from poezio.theming import to_curses_attr, read_tuple -FORMAT_CHAR = '\x19' -# These are non-printable chars, so they should never appear in the input, -# I guess. But maybe we can find better chars that are even less risky. -format_chars = '\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x1A' +from poezio.ui.consts import FORMAT_CHAR +log = logging.getLogger(__name__) -class DummyWin: - def __getattribute__(self, name: str): - if name != '__bool__': - return lambda *args, **kwargs: (0, 0) - else: - return object.__getattribute__(self, name) - - def __bool__(self) -> bool: - return False +if TYPE_CHECKING: + from _curses import _CursesWindow # pylint: disable=E0611 class Win: __slots__ = ('_win', 'height', 'width', 'y', 'x') + width: int + height: int + x: int + y: int + def __init__(self) -> None: - self._win = None + if TAB_WIN is None: + raise ValueError + self._win: _CursesWindow = TAB_WIN self.height, self.width = 0, 0 def _resize(self, height: int, width: int, y: int, x: int) -> None: @@ -49,11 +46,11 @@ class Win: return self.height, self.width, self.x, self.y = height, width, x, y try: + if TAB_WIN is None: + raise ValueError('TAB_WIN is None') self._win = TAB_WIN.derwin(height, width, y, x) except: log.debug('DEBUG: mvwin returned ERR. Please investigate') - if self._win is None: - self._win = DummyWin() def resize(self, height: int, width: int, y: int, x: int) -> None: """ @@ -76,6 +73,24 @@ class Win: # of the screen. pass + @contextmanager + def colored_text(self, color: Optional[Tuple]=None, attr: Optional[int]=None): + """Context manager which sets up an attr/color when inside""" + if color is None and attr is None: + yield None + return + mode: int + if color is not None: + mode = to_curses_attr(color) + if attr is not None: + mode = mode | attr + else: + # attr cannot be none here + mode = cast(int, attr) + self._win.attron(mode) + yield None + self._win.attroff(mode) + def addstr(self, *args) -> None: """ Safe call to addstr @@ -160,3 +175,6 @@ class Win: self.addnstr(' ' * size, size, to_curses_attr(color)) else: self.addnstr(' ' * size, size) + + +TAB_WIN: Optional[_CursesWindow] = None |