From ff5d3dee8e1735b85b482880fb840b3532003481 Mon Sep 17 00:00:00 2001 From: mecaneer23 Date: Wed, 13 Mar 2024 19:25:28 -0500 Subject: [PATCH] feat: implement buffer for tcurses addch --- src/get_todo.py | 4 ++-- src/implementing_buffer.txt | 20 -------------------- src/tcurses.py | 19 +++++++++++++++++-- 3 files changed, 19 insertions(+), 24 deletions(-) delete mode 100644 src/implementing_buffer.txt diff --git a/src/get_todo.py b/src/get_todo.py index e946113..7bd9202 100644 --- a/src/get_todo.py +++ b/src/get_todo.py @@ -341,9 +341,9 @@ def get_todo( if len(chars) + 1 >= win.getmaxyx()[1] - 1: return todo.set_display_text(_set_once(mode, chars)) if position == len(chars): - win.addstr(1, len(chars) + 1, "█") + win.addch(1, len(chars) + 1, "█") for i, char in enumerate("".join(chars).ljust(win.getmaxyx()[1] - 2)): - win.addstr(1, i + 1, char, curses.A_STANDOUT if i == position else 0) + win.addch(1, i + 1, char, curses.A_STANDOUT if i == position else 0) win.refresh() try: input_char = win.getch() diff --git a/src/implementing_buffer.txt b/src/implementing_buffer.txt deleted file mode 100644 index ffdae92..0000000 --- a/src/implementing_buffer.txt +++ /dev/null @@ -1,20 +0,0 @@ -How to implement a buffer: -- store buffer as a list (to be filled up with characters) -- buffer is nonlocal -- when function is called, add to buffer -- if buffer.is_full() or char == "\n": output buffer and empty buffer - -how do we determine if the buffer is full? -- it can have an arbitrary length -- it can have a set length based on a percentage of the width of the window -- we can store a counter and check how many calls the function has recieved and use an arbitrary amount of calls - -oplist: -- get length -len(self.buffer) -- possible to output as a string -"".join(self.buffer) -- resettable -self.buffer.clear() -- add/append a character -self.buffer.append(character) \ No newline at end of file diff --git a/src/tcurses.py b/src/tcurses.py index 6821aa3..a8b87bb 100644 --- a/src/tcurses.py +++ b/src/tcurses.py @@ -10,7 +10,7 @@ T = TypeVar("T") -class Screen: +class Screen: # pylint: disable=too-many-instance-attributes @staticmethod def _updates_screen(func: Callable[..., None]) -> Callable[..., None]: @wraps(func) @@ -36,6 +36,10 @@ def __init__( self.has_key = BooleanVar() self.has_key.set(False) root.bind("", self._handle_key) + self.buffer: list[str] = [] + self.stored_attr: int = 0 + self.stored_x: int = 0 + self.stored_y: int = 0 def __del__(self): root.bind("", stdscr._handle_key) @@ -151,7 +155,18 @@ def getmaxyx(self) -> tuple[int, int]: return self.height, self.width def addch(self, y: int, x: int, char: str, attr: int = 0) -> None: - self.addstr(y, x, char, attr) + self.buffer.append(char) + if len(self.buffer) == 1: + self.stored_x = x + self.stored_y = y + if ( + attr not in {self.stored_attr, 0} + or char == "\n" + or len(self.buffer) + self.stored_x >= self.width + ): + self.addstr(self.stored_y, self.stored_x, "".join(self.buffer), self.stored_attr) + self.stored_attr = attr + self.buffer.clear() def nodelay(self, flag: bool = True) -> None: _ = flag