From c8550a0ce5f99f575b7f7dc4be9be77604f86e58 Mon Sep 17 00:00:00 2001 From: nvbn Date: Wed, 29 Jul 2015 15:22:24 +0300 Subject: [PATCH] #298 Fix python 2 support --- setup.py | 2 +- tests/test_ui.py | 10 ++++++---- thefuck/logs.py | 2 ++ thefuck/ui.py | 28 +++++++++++++++++++++------- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index dadcb36a..bdfcbf68 100755 --- a/setup.py +++ b/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', diff --git a/tests/test_ui.py b/tests/test_ui.py index 3d7d0186..578abce2 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -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') diff --git a/thefuck/logs.py b/thefuck/logs.py index fcdbeca2..08781024 100644 --- a/thefuck/logs.py +++ b/thefuck/logs.py @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + from contextlib import contextmanager from datetime import datetime import sys diff --git a/thefuck/ui.py b/thefuck/ui.py index 3e18e62f..22243d39 100644 --- a/thefuck/ui.py +++ b/thefuck/ui.py @@ -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