From 556b771078f1f7dfe67e963f6bc30f1b9465271a Mon Sep 17 00:00:00 2001 From: Dominic Zimmer Date: Thu, 8 Aug 2019 16:06:33 +0200 Subject: [PATCH] Add inital work on cursor movement, bugs everywhere! --- drawtool.py | 43 ++++++++++++++++++++++++++++++++++++++-- mainview.py | 56 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 76 insertions(+), 23 deletions(-) diff --git a/drawtool.py b/drawtool.py index 62874a3..607824a 100644 --- a/drawtool.py +++ b/drawtool.py @@ -60,12 +60,51 @@ class Drawtool(): return newlines #return textwrap.wrap(s, width = width) + def move_cursor_home(self): + y, x = self._get_cursor_position(self.main_view.inputs, width = self.W - 4) + lines = self._get_input_lines(self.main_view.inputs, width = self.W - 4)[-self.input_lines:] + self.main_view.cursor -= x + #len(lines[y]) + + def move_cursor_end(self): + try: + y, x = self._get_cursor_position(self.main_view.inputs, width = self.W - 4) + lines = self._get_input_lines(self.main_view.inputs, width = self.W - 4)[-self.input_lines:] + self.main_view.cursor -= x - len(lines[y]) + except: + show_stacktrace() + #len(lines[y]) + + def move_cursor_up(self): + y, x = self._get_cursor_position(self.main_view.inputs, width = self.W - 4) + lines = self._get_input_lines(self.main_view.inputs, width = self.W - 4)[-self.input_lines:] + debug(lines) + debug(f"x:{x} y:{y}") + if y > 0: + self.main_view.cursor -= x + self.main_view.cursor -= 1 + + def move_cursor_down(self): + pass + def _get_cursor_position(self, s, width = 50): lines = self._get_input_lines(s, width = width)[-self.input_lines:] + alllines = self._get_input_lines(s, width = width) if not lines: return (0, 0) - x = len(lines[-1]) - y = len(lines) - 1 + curs = self.main_view.cursor + y = 0 + x = 0 + for line in alllines: + if curs > len(line): + curs -= len(line) + y += 1 + else: + x = curs + break + curs -= 1 + if y >= self.input_lines: + y = self.input_lines - 1 return y, x async def redraw(self): diff --git a/mainview.py b/mainview.py index b08bafa..01a026a 100644 --- a/mainview.py +++ b/mainview.py @@ -28,7 +28,7 @@ class MainView(): self.text_emojis = True self.inputs = "" - self.inputs_cursor = 0 + self.cursor = 0 self.popup = None @@ -442,33 +442,47 @@ class MainView(): self.mode = "normal" elif key == "LEFT": self.insert_move_left() + elif key == "UP": + self.drawtool.move_cursor_up() elif key == "RIGHT": self.insert_move_right() + elif key == "HOME": + self.drawtool.move_cursor_home() + #self.cursor = 0 + elif key == "END": + self.drawtool.move_cursor_end() + #self.cursor = len(self.inputs) + elif key == "DEL": + try: + if len(self.inputs) > self.cursor: + inp = list(self.inputs) + inp.pop(self.cursor) + self.inputs = "".join(inp) + except: + debug(f"{self.cursor}, {self.inputs}") + show_stacktrace() elif key == "BACKSPACE": - self.inputs = self.inputs[0:-1] - elif key == "RETURN": - self.inputs += "\n" + try: + if len(self.inputs) > 0: + inp = list(self.inputs) + inp.pop(self.cursor - 1) + self.inputs = "".join(inp) + self.insert_move_left() + except: + debug(f"{self.cursor}, {self.inputs}") + show_stacktrace() else: - self.inputs += key + if key == "RETURN": + key = "\n" + inp = list(self.inputs) + inp.insert(self.cursor, key) + self.inputs = "".join(inp) + self.cursor += 1 self.command_box = "" await self.drawtool.redraw() def insert_move_left(self): - self.inputs_cursor = max(0, self.cursor - 1) + self.cursor = max(0, self.cursor - 1) def insert_move_right(self): - self.inputs_cursor = min(len(self.inputs), self.cursor + 1) - - async def handle_key_old(self, key): - if key == "RETURN": - with await self.inputevent: - self.inputevent.notify() - elif key == "": - chat = self.dialogs[self.selected_chat]["dialog"] - last_message = self.dialogs[self.selected_chat]["messages"][0] - await self.client.send_read_acknowledge(chat, max_id=last_message.id) - self.dialogs[self.selected_chat]["unread_count"] = 0 - else: - self.inputs += key - self.drawtool.redraw() - + self.cursor = min(len(self.inputs), self.cursor + 1)