Skip to content

Commit d086d00

Browse files
committed
ucurses: Allow typical pattern for KEY_ESC detection.
This pattern makes use of nodelay curses mode, implement that.
1 parent 7cb60c3 commit d086d00

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

ucurses/ucurses/__init__.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import tty, termios
3+
import select
34

45
COLOR_BLACK = 0
56
COLOR_RED = 1
@@ -54,7 +55,7 @@
5455
KEY_ENTER = 1010
5556
KEY_BACKSPACE = 1011
5657
KEY_DELETE = 1012
57-
KEY_ESC = 1020
58+
KEY_ESC = 0x1b
5859

5960
KEY_DC = KEY_DELETE
6061
KEY_PPAGE = KEY_PGUP
@@ -140,6 +141,7 @@ def __init__(self, lines, cols, y, x):
140141
self.bkgattr = A_NORMAL
141142
self.keybuf = None
142143
self.keyi = 0
144+
self.keydelay = -1
143145

144146
def _goto(self, row, col):
145147
_move(self.y + row, self.x + col)
@@ -210,14 +212,33 @@ def keypad(self, yes):
210212
pass
211213

212214
def timeout(self, delay):
213-
assert delay < 0
215+
self.keydelay = delay
216+
217+
def nodelay(self, yes):
218+
if yes:
219+
self.keydelay = 0
220+
else:
221+
self.keydelay = -1
214222

215223
def getch(self):
216224
if self.keybuf and self.keyi < len(self.keybuf):
217225
c = self.keybuf[self.keyi]
218226
self.keyi += 1
219227
return c
220228

229+
if self.keydelay >= 0:
230+
USE_EPOLL = 1
231+
if USE_EPOLL:
232+
poll = select.epoll()
233+
poll.register(0, select.EPOLLIN)
234+
res = poll.poll(self.keydelay / 1000)
235+
poll.unregister(0)
236+
poll.close()
237+
else:
238+
res = select.select([0], [], [], self.keydelay / 1000)[0]
239+
if not res:
240+
return -1
241+
221242
key = os.read(0, 32)
222243
if key[0] != 0x1b:
223244
self.keybuf = key

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy