- commit
- f9fb7f8b62840dcd74387b9a47f67fb4b6cbf7cd
- parent
- 7ccce164aeeb41cb666a8f0cb826e334fca48d64
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-07-15 06:42
fix: split input into keys
Diffstat
| M | README.md | 3 | --- |
| M | boon.py | 20 | +++++++++++++++++--- |
2 files changed, 17 insertions, 6 deletions
diff --git a/README.md b/README.md
@@ -72,9 +72,6 @@ escape sequences: 72 72 - `KEY_RIGHT` 73 73 - `KEY_LEFT` 74 7475 -1 Note that boon reads all available input, so in rare occasions `key` might76 -1 contain more than one key. This should not be an issue in practice though.77 -178 75 79 76 ## Low level API 80 77
diff --git a/boon.py b/boon.py
@@ -1,5 +1,6 @@ 1 1 import curses 2 2 import os -1 3 import re 3 4 import selectors 4 5 import shutil 5 6 import signal @@ -11,6 +12,7 @@ from contextlib import contextmanager 11 12 curses.setupterm() 12 13 13 14 CSI = '\033[' -1 15 ESCAPE_CODE = re.compile(rf'{CSI}\[[0-9;]*[a-zA-Z~]') 14 16 15 17 # tigertstr uses \033O (SS3) instead of \033[ (CSI) as prefix 16 18 # https://en.wikipedia.org/wiki/ANSI_escape_code @@ -81,10 +83,21 @@ class ReusableFullscreen: 81 83 return self.mgr.__exit__(*args, **kwargs) 82 84 83 85 -1 86 def ansi_split(s): -1 87 while s: -1 88 m = ESCAPE_CODE.match(s) -1 89 if m: -1 90 yield m[0] -1 91 s = s[m.end():] -1 92 else: -1 93 yield s[0] -1 94 s = s[1:] -1 95 -1 96 84 97 def getch():85 -1 # NOTE: result might contain more than one key86 98 fd = sys.stdin.fileno()87 -1 return os.read(fd, 8).decode('utf-8')-1 99 s = os.read(fd, 1024).decode('utf-8') -1 100 return ansi_split(s) 88 101 89 102 90 103 class App: @@ -148,7 +161,8 @@ class App: 148 161 self.update(force=True) 149 162 else: 150 163 if key.fileobj is sys.stdin:151 -1 self.on_key(getch())-1 164 for key in getch(): -1 165 self.on_key(key) 152 166 elif callable(key.data): 153 167 key.data() 154 168 self.update()