-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#414: Move system-dependent utils in
system
module
- Loading branch information
Showing
7 changed files
with
120 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
|
||
class _GenConst(object): | ||
def __init__(self, name): | ||
self._name = name | ||
|
||
def __repr__(self): | ||
return u'<const: {}>'.format(self._name) | ||
|
||
|
||
KEY_UP = _GenConst('↑') | ||
KEY_DOWN = _GenConst('↓') | ||
KEY_CTRL_C = _GenConst('Ctrl+C') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import sys | ||
|
||
|
||
if sys.platform == 'win32': | ||
from .win32 import * | ||
else: | ||
from .unix import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters