-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetch.py
More file actions
147 lines (111 loc) · 4.63 KB
/
getch.py
File metadata and controls
147 lines (111 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# cardinal_pythonlib/getch.py
"""
===============================================================================
Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).
This file is part of cardinal_pythonlib.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
===============================================================================
**Offers the getch() and kbhit() functions, and other terminal-related stuff.**
In this module, :func:`getch` and :func:`kbhit` are mapped to their OS-specific
versions.
"""
import atexit
import select
import sys
try:
import msvcrt # Windows only, but part of core Python under Windows
termios = None
tty = None
except ImportError:
msvcrt = None
import termios # Unix only; tty/termios are part of core Python under Unix
import tty # requires termios, so Unix only # noqa: F401
# =============================================================================
# Read single character, waiting for it
# =============================================================================
# https://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user # noqa: E501
# http://home.wlu.edu/~levys/software/kbhit.py
# ... modified a little
def _getch_windows() -> str:
"""
Under Windows, wets a single character from standard input. Does not echo
to the screen.
"""
# noinspection PyUnresolvedReferences
return msvcrt.getch().decode("utf-8")
def _getch_unix() -> str:
"""
Under UNIX, gets a single character from standard input. Does not echo to
the screen. Note that the terminal will have been pre-configured, below.
"""
return sys.stdin.read(1)
# =============================================================================
# Is a keystroke available?
# =============================================================================
# http://code.activestate.com/recipes/572182-how-to-implement-kbhit-on-linux/
# https://stackoverflow.com/questions/2408560/python-nonblocking-console-input
def _kbhit_windows() -> bool:
"""
Under Windows: is a keystroke available?
"""
# noinspection PyUnresolvedReferences
return msvcrt.kbhit()
def _kbhit_unix() -> bool:
"""
Under UNIX: is a keystroke available?
"""
dr, dw, de = select.select([sys.stdin], [], [], 0)
return dr != []
# =============================================================================
# Configure terminal (UNIX)
# =============================================================================
def set_normal_term() -> None:
"""
Under UNIX: switch to a normal terminal. (Compare :func:`set_curses_term`.)
"""
termios.tcsetattr(_fd, termios.TCSAFLUSH, _old_term)
def set_curses_term() -> None:
"""
Under UNIX: switch to an unbuffered, curses-style terminal. (Compare
:func:`set_normal_term`.)
"""
termios.tcsetattr(_fd, termios.TCSAFLUSH, _new_term)
# =============================================================================
# Set up for specific OS
# =============================================================================
if msvcrt:
# -------------------------------------------------------------------------
# Windows
# -------------------------------------------------------------------------
getch = _getch_windows
kbhit = _kbhit_windows
else:
# -------------------------------------------------------------------------
# Unix
# -------------------------------------------------------------------------
getch = _getch_unix
kbhit = _kbhit_unix
try:
# save the terminal settings
_fd = sys.stdin.fileno()
_new_term = termios.tcgetattr(_fd)
_old_term = termios.tcgetattr(_fd)
# new terminal setting unbuffered
_new_term[3] = _new_term[3] & ~termios.ICANON & ~termios.ECHO
_new_term[6][termios.VMIN] = 1
_new_term[6][termios.VTIME] = 0
atexit.register(set_normal_term)
set_curses_term()
except termios.error:
# termios.error: (25, 'Inappropriate ioctl for device')
# when imported from GitHub CI as part of docs build
pass