ask kitten: Get readline to work even when stdout is redirected
This commit is contained in:
parent
b408abe304
commit
f8991ce3c8
@ -495,16 +495,20 @@ def main(args: List[str]) -> Response:
|
|||||||
loop.loop(phandler)
|
loop.loop(phandler)
|
||||||
return {'items': items, 'response': phandler.response}
|
return {'items': items, 'response': phandler.response}
|
||||||
|
|
||||||
import readline as rl
|
orig_stdout = os.dup(sys.stdout.fileno())
|
||||||
readline = rl
|
try:
|
||||||
init_readline()
|
with open(os.ctermid(), 'r') as tty:
|
||||||
response = None
|
os.dup2(tty.fileno(), sys.stdin.fileno())
|
||||||
|
with open(os.ctermid(), 'w') as tty:
|
||||||
|
os.dup2(tty.fileno(), sys.stdout.fileno())
|
||||||
|
import readline as rl
|
||||||
|
readline = rl
|
||||||
|
init_readline()
|
||||||
|
response = None
|
||||||
|
|
||||||
with alternate_screen(), HistoryCompleter(cli_opts.name):
|
with alternate_screen(), HistoryCompleter(cli_opts.name), suppress(KeyboardInterrupt, EOFError):
|
||||||
if cli_opts.message:
|
if cli_opts.message:
|
||||||
print(styled(cli_opts.message, bold=True))
|
print(styled(cli_opts.message, bold=True))
|
||||||
|
|
||||||
with suppress(KeyboardInterrupt, EOFError):
|
|
||||||
if cli_opts.default:
|
if cli_opts.default:
|
||||||
def prefill_text() -> None:
|
def prefill_text() -> None:
|
||||||
readline.insert_text(cli_opts.default or '')
|
readline.insert_text(cli_opts.default or '')
|
||||||
@ -514,6 +518,10 @@ def main(args: List[str]) -> Response:
|
|||||||
readline.set_pre_input_hook()
|
readline.set_pre_input_hook()
|
||||||
else:
|
else:
|
||||||
response = input(prompt)
|
response = input(prompt)
|
||||||
|
sys.stdout.flush()
|
||||||
|
os.dup2(orig_stdout, sys.stdout.fileno())
|
||||||
|
finally:
|
||||||
|
os.close(orig_stdout)
|
||||||
return {'items': items, 'response': response}
|
return {'items': items, 'response': response}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from typing import IO, Any, Callable, Dict, Generator, Optional, TypeVar, Union
|
from typing import Any, Callable, Dict, Generator, Optional, TypeVar, Union
|
||||||
|
|
||||||
from kitty.fast_data_types import Color
|
from kitty.fast_data_types import Color
|
||||||
from kitty.rgb import color_as_sharp, to_color
|
from kitty.rgb import color_as_sharp, to_color
|
||||||
@ -348,13 +349,13 @@ def cursor(write: Callable[[str], None]) -> Generator[None, None, None]:
|
|||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
def alternate_screen(f: Optional[IO[str]] = None) -> Generator[None, None, None]:
|
def alternate_screen() -> Generator[None, None, None]:
|
||||||
f = f or sys.stdout
|
with open(os.ctermid(), 'w') as f:
|
||||||
print(set_mode(Mode.ALTERNATE_SCREEN), end='', file=f)
|
print(set_mode(Mode.ALTERNATE_SCREEN), end='', file=f, flush=True)
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
finally:
|
finally:
|
||||||
print(reset_mode(Mode.ALTERNATE_SCREEN), end='', file=f)
|
print(reset_mode(Mode.ALTERNATE_SCREEN), end='', file=f, flush=True)
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
@contextmanager
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user