Improve Linux terminal input and interactive path prompts
This commit is contained in:
1 parent
eb5937a629
commit
ad2b15994f
5 files changed
+190
-22
No files matched your search
+63
-7
@@ -3,12 +3,14 @@ from __future__ import annotations
|
||||
import os
|
||||
import select
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import textwrap
|
||||
import time
|
||||
from collections import deque
|
||||
from contextlib import contextmanager
|
||||
from math import floor, log10
|
||||
from pathlib import Path
|
||||
from threading import Lock
|
||||
|
||||
|
||||
@@ -44,6 +46,26 @@ def pause_if_interactive() -> None:
|
||||
pass
|
||||
|
||||
|
||||
def launch_terminal_if_needed(argv: list[str]) -> bool:
|
||||
"""Relaunch an interactive script when a desktop starts it without a terminal."""
|
||||
if os.name == "nt" or (stream_is_interactive(sys.stdin) and stream_is_interactive(sys.stdout)):
|
||||
return False
|
||||
if not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
|
||||
return False
|
||||
|
||||
terminals = (("konsole", "-e"), ("gnome-terminal", "--"), ("x-terminal-emulator", "-e"))
|
||||
for name, separator in terminals:
|
||||
executable = shutil.which(name)
|
||||
if executable:
|
||||
script = Path(argv[0]).resolve()
|
||||
subprocess.Popen(
|
||||
[executable, separator, sys.executable, str(script), *argv[1:]],
|
||||
cwd=script.parent,
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def supports_color() -> bool:
|
||||
return not os.environ.get("NO_COLOR") and supports_ansi()
|
||||
|
||||
@@ -117,7 +139,7 @@ def raw_key_input():
|
||||
fd = sys.stdin.fileno()
|
||||
settings = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setraw(fd)
|
||||
tty.setcbreak(fd)
|
||||
yield read_key
|
||||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, settings)
|
||||
@@ -129,13 +151,47 @@ def read_key() -> str:
|
||||
|
||||
key = msvcrt.getwch()
|
||||
if key in {"\x00", "\xe0"}:
|
||||
return {"H": "up", "P": "down", "K": "left", "M": "right"}.get(msvcrt.getwch(), "")
|
||||
return {"H": "up", "P": "down", "K": "left", "M": "right", "S": "delete"}.get(msvcrt.getwch(), "")
|
||||
else:
|
||||
key = sys.stdin.read(1)
|
||||
if key == "\x1b" and select.select([sys.stdin], [], [], 0.03)[0]:
|
||||
key += sys.stdin.read(2)
|
||||
return {"\x1b[A": "up", "\x1b[B": "down", "\x1b[D": "left", "\x1b[C": "right"}.get(key, "esc")
|
||||
return {"\r": "enter", "\n": "enter", "\x1b": "esc", "\x08": "backspace", "\x7f": "backspace", " ": "space"}.get(key, key)
|
||||
fd = sys.stdin.fileno()
|
||||
key = _read_unix_character(fd)
|
||||
if key == "\x1b":
|
||||
return _read_escape_sequence(fd)
|
||||
return {"\r": "enter", "\n": "enter", "\t": "tab", "\x1b": "esc", "\x08": "backspace", "\x7f": "backspace", " ": "space"}.get(key, key)
|
||||
|
||||
|
||||
def _read_unix_character(fd: int) -> str:
|
||||
data = os.read(fd, 1)
|
||||
first = data[0]
|
||||
length = 1 if first < 0x80 else 2 if first < 0xE0 else 3 if first < 0xF0 else 4
|
||||
while len(data) < length:
|
||||
data += os.read(fd, length - len(data))
|
||||
return data.decode("utf-8")
|
||||
|
||||
|
||||
def _read_escape_sequence(fd: int) -> str:
|
||||
if not select.select([fd], [], [], 0.1)[0]:
|
||||
return "esc"
|
||||
sequence = "\x1b" + _read_unix_character(fd)
|
||||
if sequence[1:] in {"[", "O"}:
|
||||
for _ in range(6):
|
||||
if not select.select([fd], [], [], 0.1)[0]:
|
||||
break
|
||||
character = _read_unix_character(fd)
|
||||
sequence += character
|
||||
if character.isalpha() or character == "~":
|
||||
break
|
||||
return {
|
||||
"\x1b[A": "up",
|
||||
"\x1b[B": "down",
|
||||
"\x1b[D": "left",
|
||||
"\x1b[C": "right",
|
||||
"\x1bOA": "up",
|
||||
"\x1bOB": "down",
|
||||
"\x1bOD": "left",
|
||||
"\x1bOC": "right",
|
||||
"\x1b[3~": "delete",
|
||||
}.get(sequence, "")
|
||||
|
||||
|
||||
def draw_screen(lines: list[str]) -> None:
|
||||
|
||||
Reference in new issue
Block a user