From abaa7713a73af3afb94c249c652082d2a9d3b097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustav=20S=C3=B6rn=C3=A4s?= Date: Thu, 11 Mar 2021 16:08:22 +0100 Subject: =?UTF-8?q?show=20wip=20typing,=20read=20backspace=20and=20fix=20?= =?UTF-8?q?=C3=A5=C3=A4=C3=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 2 +- cursed.py | 37 ++++++++++++++++++++++--------------- main.py | 6 +++--- tele.py | 5 ++++- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/TODO b/TODO index 17c6738..2d3dea0 100644 --- a/TODO +++ b/TODO @@ -9,7 +9,6 @@ daily driver: - tg chat - tg list - show chat history -- show current wip message - notifications - libnotify - silent messages @@ -31,3 +30,4 @@ later: - save drafts - login - show when someone is typing +- mark messages as read diff --git a/cursed.py b/cursed.py index 5259408..7e262b5 100644 --- a/cursed.py +++ b/cursed.py @@ -3,8 +3,10 @@ import asyncio import sys class Console(): - def __init__(self): + def __init__(self, messages): self.stdscr = curses.initscr() + self.messages = messages + self.typed_message = "" def __enter__(self): curses.noecho() @@ -18,24 +20,29 @@ class Console(): curses.echo() curses.endwin() - def redraw(self, messages): + def redraw(self): self.stdscr.clear() - for msg in messages: + for msg in self.messages: self.stdscr.addstr(msg + '\n') + if self.typed_message != "": + self.stdscr.addstr(self.typed_message) self.stdscr.refresh() -async def start(c, on_str, on_tab, messages): - string = "" - c.redraw(messages) +async def start(c, on_str, on_tab): while True: - char = await asyncio.to_thread(c.stdscr.getkey) - if char == '\n' and string != "": - await on_str(string) - messages.append(string) - c.redraw(messages) - string = "" - elif char == '\t': - await on_tab() + c.redraw() + char = await asyncio.to_thread(c.stdscr.get_wch) + if isinstance(char, str): + if char == '\n': + if c.typed_message != "": + await on_str(c.typed_message) + c.messages.append(c.typed_message) + c.typed_message = "" + elif char == '\t': + await on_tab() + else: + c.typed_message += char else: - string += char + if char == 263: + c.typed_message = c.typed_message[:-1] diff --git a/main.py b/main.py index 3b40e33..e317ecc 100644 --- a/main.py +++ b/main.py @@ -12,12 +12,12 @@ async def on_tab(): def main(): messages = [] - with cursed.Console() as c: + with cursed.Console(messages) as c: tele.messages = messages - tele.redraw = lambda message: c.redraw(message) + tele.redraw = lambda: c.redraw() with tele.client: tele.client.start() - tele.client.loop.run_until_complete(cursed.start(c, on_str, on_tab, messages)) + tele.client.loop.run_until_complete(cursed.start(c, on_str, on_tab)) if __name__=="__main__": diff --git a/tele.py b/tele.py index 2dc3562..c7d980b 100644 --- a/tele.py +++ b/tele.py @@ -17,4 +17,7 @@ client = telethon.TelegramClient( @client.on(telethon.events.NewMessage) async def new_message(event): messages.append(event.raw_text) - redraw(messages) + redraw() + +if __name__ == "__main__": + client.start() -- cgit v1.2.1