misc.py 1.76 KB
Newer Older
1 2 3 4 5 6 7
"""
Wins that don’t fit any category
"""

import logging
import curses

8 9
from typing import List

Link Mauve's avatar
Link Mauve committed
10
from poezio.windows.base_wins import Win
11
from poezio.theming import get_theme, to_curses_attr
12

13 14
log = logging.getLogger(__name__)

mathieui's avatar
mathieui committed
15

16 17 18 19 20
class VerticalSeparator(Win):
    """
    Just a one-column window, with just a line in it, that is
    refreshed only on resize, but never on refresh, for efficiency
    """
mathieui's avatar
mathieui committed
21

22 23
    __slots__ = ()

24
    def rewrite_line(self) -> None:
25 26 27
        self._win.vline(0, 0, curses.ACS_VLINE, self.height,
                        to_curses_attr(get_theme().COLOR_VERTICAL_SEPARATOR))
        self._refresh()
28

29
    def refresh(self) -> None:
30 31 32 33 34
        log.debug('Refresh: %s', self.__class__.__name__)
        self.rewrite_line()


class SimpleTextWin(Win):
35 36
    __slots__ = ('_text', 'built_lines')

37
    def __init__(self, text) -> None:
38 39
        Win.__init__(self)
        self._text = text
40
        self.built_lines = []  # type: List[str]
41

42
    def rebuild_text(self) -> None:
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        """
        Transform the text in lines than can then be
        displayed without any calculation or anything
        at refresh() time
        It is basically called on each resize
        """
        self.built_lines = []
        for line in self._text.split('\n'):
            while len(line) >= self.width:
                limit = line[:self.width].rfind(' ')
                if limit <= 0:
                    limit = self.width
                self.built_lines.append(line[:limit])
                line = line[limit:]
            self.built_lines.append(line)

59
    def refresh(self) -> None:
60
        log.debug('Refresh: %s', self.__class__.__name__)
61 62 63 64
        self._win.erase()
        for y, line in enumerate(self.built_lines):
            self.addstr_colored(line, y, 0)
        self._refresh()