fill3: Coding style
- Optimize splitting stdin by using a regex.
This commit is contained in:
parent
ccdd657c10
commit
f77d5743a1
2 changed files with 12 additions and 24 deletions
|
|
@ -6,6 +6,7 @@ import asyncio
|
||||||
import contextlib
|
import contextlib
|
||||||
import itertools
|
import itertools
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import signal
|
import signal
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
@ -494,25 +495,12 @@ async def update_screen(screen_widget):
|
||||||
APPEARANCE_CHANGED_EVENT.clear()
|
APPEARANCE_CHANGED_EVENT.clear()
|
||||||
|
|
||||||
|
|
||||||
def digest_terminal_input(input_str):
|
_INPUT_REGEX = re.compile("\x1b[^\x00-\x1f]+|[\x00-\x1f]|[^\x00-\x1f]+")
|
||||||
cursor = 0
|
|
||||||
result = []
|
|
||||||
for index, char in enumerate(input_str):
|
|
||||||
if cursor != index:
|
|
||||||
if char == terminal.ESC:
|
|
||||||
result.append(input_str[cursor:index])
|
|
||||||
cursor = index
|
|
||||||
elif ord(char) < 32:
|
|
||||||
result.extend([input_str[cursor:index], char])
|
|
||||||
cursor = index + 1
|
|
||||||
if cursor != index + 1:
|
|
||||||
result.append(input_str[cursor:index+1])
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
@handle_exception
|
@handle_exception
|
||||||
def on_terminal_input(screen_widget):
|
def on_terminal_input(screen_widget):
|
||||||
for part in digest_terminal_input(sys.stdin.read()):
|
for part in _INPUT_REGEX.findall(sys.stdin.read()):
|
||||||
if part.startswith(terminal.MOUSE):
|
if part.startswith(terminal.MOUSE):
|
||||||
screen_widget.on_mouse_input(part[3:])
|
screen_widget.on_mouse_input(part[3:])
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -140,16 +140,16 @@ class WidgetTests(unittest.TestCase):
|
||||||
"B A")
|
"B A")
|
||||||
|
|
||||||
|
|
||||||
class DigestTerminalInputTestCase(unittest.TestCase):
|
class TerminalInputTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def test_digest_terminal_input(self):
|
def test_input_regex(self):
|
||||||
self.assertRaises(UnboundLocalError, fill3.digest_terminal_input, "")
|
self.assertEqual(fill3._INPUT_REGEX.findall(""), [])
|
||||||
self.assertEqual(fill3.digest_terminal_input("a"), ["a"])
|
self.assertEqual(fill3._INPUT_REGEX.findall("a"), ["a"])
|
||||||
self.assertEqual(fill3.digest_terminal_input("ab"), ["ab"])
|
self.assertEqual(fill3._INPUT_REGEX.findall("ab"), ["ab"])
|
||||||
self.assertEqual(fill3.digest_terminal_input("a\nb"), ["a", "\n", "b"])
|
self.assertEqual(fill3._INPUT_REGEX.findall("a\nb"), ["a", "\n", "b"])
|
||||||
self.assertEqual(fill3.digest_terminal_input("a\tb"), ["a", "\t", "b"])
|
self.assertEqual(fill3._INPUT_REGEX.findall("a\tb"), ["a", "\t", "b"])
|
||||||
self.assertEqual(fill3.digest_terminal_input(terminal.UP * 2), [terminal.UP] * 2)
|
self.assertEqual(fill3._INPUT_REGEX.findall(terminal.UP * 2), [terminal.UP] * 2)
|
||||||
self.assertEqual(fill3.digest_terminal_input(terminal.CTRL_C * 2), [terminal.CTRL_C] * 2)
|
self.assertEqual(fill3._INPUT_REGEX.findall(terminal.CTRL_C * 2), [terminal.CTRL_C] * 2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue