#414: Move system-dependent utils in system module
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
import sys
|
||||
|
||||
|
||||
if sys.platform == 'win32':
|
||||
from .win32 import *
|
||||
else:
|
||||
from .unix import *
|
||||
@@ -0,0 +1,35 @@
|
||||
import sys
|
||||
import tty
|
||||
import termios
|
||||
import colorama
|
||||
from .. import const
|
||||
|
||||
init_output = colorama.init
|
||||
|
||||
|
||||
def getch():
|
||||
fd = sys.stdin.fileno()
|
||||
old = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setraw(fd)
|
||||
return sys.stdin.read(1)
|
||||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
||||
|
||||
|
||||
def get_key():
|
||||
ch = getch()
|
||||
|
||||
if ch == '\x03':
|
||||
return const.KEY_CTRL_C
|
||||
elif ch == '\x1b':
|
||||
next_ch = getch()
|
||||
if next_ch == '[':
|
||||
last_ch = getch()
|
||||
|
||||
if last_ch == 'A':
|
||||
return const.KEY_UP
|
||||
elif last_ch == 'B':
|
||||
return const.KEY_DOWN
|
||||
|
||||
return ch
|
||||
@@ -0,0 +1,25 @@
|
||||
import sys
|
||||
import msvcrt
|
||||
import colorama
|
||||
import win_unicode_console
|
||||
from .. import const
|
||||
|
||||
|
||||
def init_output():
|
||||
win_unicode_console.enable()
|
||||
colorama.init()
|
||||
|
||||
|
||||
def get_key():
|
||||
ch = msvcrt.getch()
|
||||
if ch in (b'\x00', b'\xe0'): # arrow or function key prefix?
|
||||
ch = msvcrt.getch() # second call returns the actual key code
|
||||
|
||||
if ch == b'\x03':
|
||||
raise const.KEY_CTRL_C
|
||||
if ch == b'H':
|
||||
return const.KEY_UP
|
||||
if ch == b'P':
|
||||
return const.KEY_DOWN
|
||||
|
||||
return ch.decode(sys.stdout.encoding)
|
||||
Reference in New Issue
Block a user