Skip to content
Myr

Introduce raw_terminal

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Markus Unterwaditzer · committed by JosiahDub · parent bd59717 · Browse files · 2 files+21 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−7 removed

  • other:+21 added−7 removed

2 files+21 added−7 removed

modified:click/_termui_impl.py+16 added-7 removed
11 unmodified lines
12import os13import sys14import time15import math1616import contextlib17from ._compat import _default_text_stdout, range_type, PY2, isatty, \18 open_stream, strip_ansi, term_len, get_best_encoding, WIN, int_types, \19 CYGWIN20from .utils import echo
503 unmodified lines
524525if WIN:526 import msvcrt527528 @contextlib.contextmanager529 def raw_terminal():530 yield531532 def getchar(echo):533 rv = msvcrt.getch()534 if echo:535 msvcrt.putchar(rv)
8 unmodified lines
544else:545 import tty546 import termios547544 def getchar(echo):548 @contextlib.contextmanager549 def raw_terminal():550 if not isatty(sys.stdin):551 f = open('/dev/tty')552 fd = f.fileno()553 else:
2 unmodified lines
556 try:557 old_settings = termios.tcgetattr(fd)558 try:559 tty.setraw(fd)555 ch = os.read(fd, 32)556 if echo and isatty(sys.stdout):557 sys.stdout.write(ch)560 yield fd561 finally:562 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)563 sys.stdout.flush()564 if f is not None:565 f.close()566 except termios.error:567 pass568569 def getchar(echo):570 with raw_terminal():571 ch = os.read(fd, 32)572 if echo and isatty(sys.stdout):573 sys.stdout.write(ch)565 _translate_ch_to_exc(ch)566 return ch.decode(get_best_encoding(sys.stdin), 'replace')574 _translate_ch_to_exc(ch)575 return ch.decode(get_best_encoding(sys.stdin), 'replace')
modified:click/termui.py+5 added
565 unmodified lines
566 from ._termui_impl import getchar as f567 return f(echo)568569570def raw_terminal():571 from ._termui_impl import raw_terminal as f572 return f()573574575def pause(info='Press any key to continue ...', err=False):576 """This command stops execution and waits for the user to press any577 key to continue. This is similar to the Windows batch "pause"578 command. If the program is not run through a terminal, this command
23 unmodified lines