#414: Move system-dependent utils in system module
This commit is contained in:
@@ -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')
|
||||
+3
-12
@@ -2,27 +2,18 @@ from argparse import ArgumentParser
|
||||
from warnings import warn
|
||||
from pprint import pformat
|
||||
import sys
|
||||
import colorama
|
||||
from . import logs, types, shells
|
||||
from .conf import settings
|
||||
from .corrector import get_corrected_commands
|
||||
from .exceptions import EmptyCommand
|
||||
from .utils import get_installation_info
|
||||
from .ui import select_command
|
||||
|
||||
|
||||
def init_colorama():
|
||||
if sys.platform == 'win32':
|
||||
# https://github.com/tartley/colorama/issues/32
|
||||
import win_unicode_console
|
||||
|
||||
win_unicode_console.enable()
|
||||
colorama.init()
|
||||
from .system import init_output
|
||||
|
||||
|
||||
def fix_command():
|
||||
"""Fixes previous command. Used when `thefuck` called without arguments."""
|
||||
init_colorama()
|
||||
init_output()
|
||||
settings.init()
|
||||
with logs.debug_time('Total'):
|
||||
logs.debug(u'Run with settings: {}'.format(pformat(settings)))
|
||||
@@ -60,7 +51,7 @@ def how_to_configure_alias():
|
||||
It'll be only visible when user type fuck and when alias isn't configured.
|
||||
|
||||
"""
|
||||
init_colorama()
|
||||
init_output()
|
||||
settings.init()
|
||||
logs.how_to_configure_alias(shells.how_to_configure())
|
||||
|
||||
|
||||
@@ -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)
|
||||
+9
-45
@@ -3,38 +3,8 @@
|
||||
import sys
|
||||
from .conf import settings
|
||||
from .exceptions import NoRuleMatched
|
||||
from . import logs
|
||||
|
||||
try:
|
||||
import msvcrt
|
||||
|
||||
def getch():
|
||||
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 KeyboardInterrupt
|
||||
if ch == b'H':
|
||||
return 'k'
|
||||
if ch == b'P':
|
||||
return 'j'
|
||||
return ch.decode(sys.stdout.encoding)
|
||||
except ImportError:
|
||||
def getch():
|
||||
import tty
|
||||
import termios
|
||||
|
||||
fd = sys.stdin.fileno()
|
||||
old = termios.tcgetattr(fd)
|
||||
try:
|
||||
tty.setraw(fd)
|
||||
ch = sys.stdin.read(1)
|
||||
if ch == '\x03': # For compatibility with msvcrt.getch
|
||||
raise KeyboardInterrupt
|
||||
return ch
|
||||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
||||
from .system import get_key
|
||||
from . import logs, const
|
||||
|
||||
SELECT = 0
|
||||
ABORT = 1
|
||||
@@ -44,23 +14,17 @@ NEXT = 3
|
||||
|
||||
def read_actions():
|
||||
"""Yields actions for pressed keys."""
|
||||
buffer = []
|
||||
while True:
|
||||
try:
|
||||
ch = getch()
|
||||
except KeyboardInterrupt: # Ctrl+C
|
||||
yield ABORT
|
||||
key = get_key()
|
||||
|
||||
if ch in ('\n', '\r'): # Enter
|
||||
yield SELECT
|
||||
|
||||
buffer.append(ch)
|
||||
buffer = buffer[-3:]
|
||||
|
||||
if buffer == ['\x1b', '[', 'A'] or ch == 'k': # ↑
|
||||
if key in (const.KEY_UP, 'k'):
|
||||
yield PREVIOUS
|
||||
elif buffer == ['\x1b', '[', 'B'] or ch == 'j': # ↓
|
||||
elif key in (const.KEY_DOWN, 'j'):
|
||||
yield NEXT
|
||||
elif key == const.KEY_CTRL_C:
|
||||
yield ABORT
|
||||
elif key in ('\n', '\r'):
|
||||
yield SELECT
|
||||
|
||||
|
||||
class CommandSelector(object):
|
||||
|
||||
Reference in New Issue
Block a user