Coding style.

- Divide input onto on_keyboard_input and on_mouse_input methods.
- Also let on_mouse_input see the original term code in case
  terminal.decode_mouse_input() is not enough.
This commit is contained in:
Andrew Hamilton 2021-04-11 17:41:07 +10:00
parent 39bda8cafb
commit a50f66554a
3 changed files with 41 additions and 33 deletions

View file

@ -659,7 +659,8 @@ class Help:
def _exit_help(self):
self.screen._is_help_visible = False
def _on_mouse_event(self, event, appearance_changed_event):
def on_mouse_input(self, term_code, appearance_changed_event):
event = terminal.decode_mouse_input(term_code)
if event[1] == 4: # Mouse wheel up
self.view.portal.scroll_up()
appearance_changed_event.set()
@ -667,12 +668,9 @@ class Help:
self.view.portal.scroll_down()
appearance_changed_event.set()
def on_input_event(self, event, appearance_changed_event):
if type(event) == tuple:
self._on_mouse_event(event, appearance_changed_event)
return
def on_keyboard_input(self, term_code, appearance_changed_event):
try:
action = self.key_map[event.lower()]
action = self.key_map[term_code.lower()]
except KeyError:
pass
else:
@ -944,7 +942,14 @@ class Screen:
self._is_summary_focused or y <= view_height and
not self._is_summary_focused)))
def _on_mouse_event(self, event):
def on_mouse_input(self, term_code):
if self._is_help_visible:
self._help_widget.on_mouse_input(
term_code, self._appearance_changed_event)
return
event = terminal.decode_mouse_input(term_code)
if event[0] not in ["mouse press", "mouse drag"]:
return
x, y = event[2:4]
if event[0] == "mouse drag":
last_x, last_y = self._last_mouse_position
@ -967,23 +972,18 @@ class Screen:
self._select_entry_at_position(
x, y, view_width, view_height)
self._last_mouse_position = x, y
self._appearance_changed_event.set()
def on_input_event(self, event):
def on_keyboard_input(self, term_code):
if self._is_help_visible:
self._help_widget.on_input_event(
event, self._appearance_changed_event)
self._help_widget.on_keyboard_input(
term_code, self._appearance_changed_event)
return
if type(event) == tuple:
if event[0] in ["mouse press", "mouse drag"]:
self._on_mouse_event(event)
self._appearance_changed_event.set()
return
else:
action = (self._key_map.get(event) or
self._key_map.get(event.lower()))
if action is not None:
action(self)
self._appearance_changed_event.set()
action = (self._key_map.get(term_code) or
self._key_map.get(term_code.lower()))
if action is not None:
action(self)
self._appearance_changed_event.set()
def _fix_listing(self):
widget = self._summary.get_selection()

View file

@ -471,12 +471,12 @@ async def update_screen(screen_widget, appearance_changed_event):
def on_terminal_input(screen_widget):
code = sys.stdin.read()
if code.startswith(terminal.ESC + "[M"): # mouse
for part in code.split(terminal.ESC + "[M")[1:]:
screen_widget.on_input_event(terminal.decode_mouse(part))
term_code = sys.stdin.read()
if term_code.startswith(terminal.MOUSE):
for part in term_code.split(terminal.MOUSE)[1:]:
screen_widget.on_mouse_input(part)
else:
return screen_widget.on_input_event(code) # keyboard
return screen_widget.on_keyboard_input(term_code)
@contextlib.contextmanager
@ -512,17 +512,23 @@ def context(loop, appearance_changed_event, screen_widget, exit_loop=None):
class _Screen:
def __init__(self, appearance_changed_event):
self.appearance_changed_event = appearance_changed_event
self._appearance_changed_event = appearance_changed_event
self.content = Filler(Text("Hello World"))
def appearance(self, dimensions):
return self.content.appearance(dimensions)
def on_input_event(self, event):
if event == "q":
def on_keyboard_input(self, term_code):
if term_code == "q":
asyncio.get_event_loop().stop()
self.content = Filler(Text(repr(event)))
self.appearance_changed_event.set()
else:
self.content = Filler(Text(repr(term_code)))
self._appearance_changed_event.set()
def on_mouse_input(self, term_code):
mouse_code = terminal.decode_mouse_input(term_code)
self.content = Filler(Text(repr(term_code) + " " + repr(mouse_code)))
self._appearance_changed_event.set()
def _main():

View file

@ -9,6 +9,8 @@ import termios
ESC = "\x1b"
MOUSE = ESC + "[M"
normal = "[m"
bold = "[1m"
italic = "[3m"
@ -78,8 +80,8 @@ def interactive():
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_termios_settings)
def decode_mouse(code):
keys = [ord(byte) for byte in code]
def decode_mouse_input(term_code):
keys = [ord(byte) for byte in term_code]
b = keys[0] - 32
x, y = (keys[1] - 33) % 256, (keys[2] - 33) % 256
button = ((b & 64) / 64 * 3) + (b & 3) + 1