mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-20 20:09:07 +00:00
#298 Fix python 2 support
This commit is contained in:
parent
7933e963d8
commit
c8550a0ce5
2
setup.py
2
setup.py
@ -22,7 +22,7 @@ elif (3, 0) < version < (3, 3):
|
||||
|
||||
VERSION = '2.5.6'
|
||||
|
||||
install_requires = ['psutil', 'colorama', 'six', 'getch']
|
||||
install_requires = ['psutil', 'colorama', 'six']
|
||||
extras_require = {':python_version<"3.4"': ['pathlib']}
|
||||
|
||||
setup(name='thefuck',
|
||||
|
@ -1,3 +1,5 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
from mock import Mock
|
||||
import pytest
|
||||
from itertools import islice
|
||||
@ -88,14 +90,14 @@ class TestSelectCommand(object):
|
||||
assert ui.select_command(commands,
|
||||
Mock(debug=False, no_color=True,
|
||||
require_confirmation=True)) == commands[0]
|
||||
assert capsys.readouterr() == ('', '\x1b[1K\rls [enter/↑/↓/ctrl+c]\n')
|
||||
assert capsys.readouterr() == ('', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\n')
|
||||
|
||||
def test_with_confirmation_abort(self, capsys, patch_getch, commands):
|
||||
patch_getch([KeyboardInterrupt])
|
||||
assert ui.select_command(commands,
|
||||
Mock(debug=False, no_color=True,
|
||||
require_confirmation=True)) is None
|
||||
assert capsys.readouterr() == ('', '\x1b[1K\rls [enter/↑/↓/ctrl+c]\nAborted\n')
|
||||
assert capsys.readouterr() == ('', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\nAborted\n')
|
||||
|
||||
def test_with_confirmation_with_side_effct(self, capsys, patch_getch,
|
||||
commands_with_side_effect):
|
||||
@ -104,7 +106,7 @@ class TestSelectCommand(object):
|
||||
Mock(debug=False, no_color=True,
|
||||
require_confirmation=True))\
|
||||
== commands_with_side_effect[0]
|
||||
assert capsys.readouterr() == ('', '\x1b[1K\rls (+side effect) [enter/↑/↓/ctrl+c]\n')
|
||||
assert capsys.readouterr() == ('', u'\x1b[1K\rls (+side effect) [enter/↑/↓/ctrl+c]\n')
|
||||
|
||||
def test_with_confirmation_select_second(self, capsys, patch_getch, commands):
|
||||
patch_getch(['\x1b', '[', 'B', '\n'])
|
||||
@ -112,4 +114,4 @@ class TestSelectCommand(object):
|
||||
Mock(debug=False, no_color=True,
|
||||
require_confirmation=True)) == commands[1]
|
||||
assert capsys.readouterr() == (
|
||||
'', '\x1b[1K\rls [enter/↑/↓/ctrl+c]\x1b[1K\rcd [enter/↑/↓/ctrl+c]\n')
|
||||
'', u'\x1b[1K\rls [enter/↑/↓/ctrl+c]\x1b[1K\rcd [enter/↑/↓/ctrl+c]\n')
|
||||
|
@ -1,3 +1,5 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime
|
||||
import sys
|
||||
|
@ -1,7 +1,25 @@
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
import sys
|
||||
from getch import getch
|
||||
from . import logs
|
||||
|
||||
try:
|
||||
from msvcrt import getch
|
||||
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
|
||||
finally:
|
||||
termios.tcsetattr(fd, termios.TCSADRAIN, old)
|
||||
|
||||
SELECT = 0
|
||||
ABORT = 1
|
||||
PREVIOUS = 2
|
||||
@ -11,14 +29,10 @@ NEXT = 3
|
||||
def read_actions():
|
||||
"""Yields actions for pressed keys."""
|
||||
buffer = []
|
||||
ch = None
|
||||
while True:
|
||||
try:
|
||||
try:
|
||||
ch = getch()
|
||||
except OverflowError: # Ctrl+C, KeyboardInterrupt will be reraised
|
||||
pass
|
||||
except KeyboardInterrupt:
|
||||
ch = getch()
|
||||
except KeyboardInterrupt: # Ctrl+C
|
||||
yield ABORT
|
||||
|
||||
if ch in ('\n', '\r'): # Enter
|
||||
|
Loading…
x
Reference in New Issue
Block a user