Skip to content

Commit

Permalink
feat: implement buffer for tcurses addch
Browse files Browse the repository at this point in the history
  • Loading branch information
mecaneer23 committed Mar 14, 2024
1 parent ece357c commit ff5d3de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/get_todo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 0 additions & 20 deletions src/implementing_buffer.txt

This file was deleted.

19 changes: 17 additions & 2 deletions src/tcurses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -36,6 +36,10 @@ def __init__(
self.has_key = BooleanVar()
self.has_key.set(False)
root.bind("<Key>", 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("<Key>", stdscr._handle_key)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ff5d3de

Please sign in to comment.