From 2207dd266887e812cae9da67ca00bef80c9985fd Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Tue, 15 Mar 2016 14:10:04 -0700 Subject: [PATCH 1/3] Update _get_shell to work with Windows - _get_shell assumed the parent process would always be the shell process, in Powershell the parent process is Python, with the grandparent being the shell - Switched to walking the process tree so the same code path can be used in both places --- thefuck/shells/__init__.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/thefuck/shells/__init__.py b/thefuck/shells/__init__.py index 414b8ee7..4a75b826 100644 --- a/thefuck/shells/__init__.py +++ b/thefuck/shells/__init__.py @@ -18,11 +18,26 @@ shells = {'bash': Bash, def _get_shell(): - try: - shell_name = Process(os.getpid()).parent().name() - except TypeError: - shell_name = Process(os.getpid()).parent.name - return shells.get(shell_name, Generic)() + proc = Process(os.getpid()) + + while (proc is not None): + name = None + try: + name = proc.name() + except TypeError: + name = proc.name + + name = os.path.splitext(name)[0] + + if name in shells: + return shells[name]() + + try: + proc = proc.parent() + except TypeError: + proc = proc.parent + + return Generic() shell = _get_shell() From 6daf687237508acf7e1868434a65ffb0555bad69 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Tue, 15 Mar 2016 14:12:37 -0700 Subject: [PATCH 2/3] Add Powershell as a supported shell - There may be additional functionality to implement, but I've been running this way for a month with no known issues --- tests/shells/test_powershell.py | 19 +++++++++++++++++++ thefuck/shells/__init__.py | 4 +++- thefuck/shells/powershell.py | 18 ++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 tests/shells/test_powershell.py create mode 100644 thefuck/shells/powershell.py diff --git a/tests/shells/test_powershell.py b/tests/shells/test_powershell.py new file mode 100644 index 00000000..d1e7a874 --- /dev/null +++ b/tests/shells/test_powershell.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +import pytest +from thefuck.shells import Powershell + + +@pytest.mark.usefixtures('isfile', 'no_memoize', 'no_cache') +class TestPowershell(object): + @pytest.fixture + def shell(self): + return Powershell() + + def test_and_(self, shell): + assert shell.and_('ls', 'cd') == '(ls) -and (cd)' + + def test_app_alias(self, shell): + assert 'function fuck' in shell.app_alias('fuck') + assert 'function FUCK' in shell.app_alias('FUCK') + assert 'thefuck' in shell.app_alias('fuck') diff --git a/thefuck/shells/__init__.py b/thefuck/shells/__init__.py index 4a75b826..79fb66c8 100644 --- a/thefuck/shells/__init__.py +++ b/thefuck/shells/__init__.py @@ -9,12 +9,14 @@ from .fish import Fish from .generic import Generic from .tcsh import Tcsh from .zsh import Zsh +from .powershell import Powershell shells = {'bash': Bash, 'fish': Fish, 'zsh': Zsh, 'csh': Tcsh, - 'tcsh': Tcsh} + 'tcsh': Tcsh, + 'powershell': Powershell} def _get_shell(): diff --git a/thefuck/shells/powershell.py b/thefuck/shells/powershell.py new file mode 100644 index 00000000..fd0095ff --- /dev/null +++ b/thefuck/shells/powershell.py @@ -0,0 +1,18 @@ +from .generic import Generic + + +class Powershell(Generic): + def app_alias(self, fuck): + return 'function ' + fuck + ' { \n' \ + ' $fuck = $(thefuck (Get-History -Count 1).CommandLine)\n' \ + ' if (-not [string]::IsNullOrWhiteSpace($fuck)) {\n' \ + ' if ($fuck.StartsWith("echo")) { $fuck = $fuck.Substring(5) }\n' \ + ' else { iex "$fuck" }\n' \ + ' }\n' \ + '}\n' + + def and_(self, *commands): + return u' -and '.join('({0})'.format(c) for c in commands) + + def how_to_configure(self): + return 'iex "thefuck --alias"', '$profile' From d4bc8cebf1d629ff1f3de18ca17af4b1fdde4926 Mon Sep 17 00:00:00 2001 From: Matt Kotsenas Date: Wed, 16 Mar 2016 16:37:59 -0700 Subject: [PATCH 3/3] Replace raise with return for Ctrl+C in Windows - Replace the raise `const.CtrlC` with `return const.CtrlC` the match the unix implementation and prevent a stacktrace when cancelling a command on Windows --- thefuck/system/win32.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/thefuck/system/win32.py b/thefuck/system/win32.py index 5cfd438e..b09778e1 100644 --- a/thefuck/system/win32.py +++ b/thefuck/system/win32.py @@ -16,7 +16,7 @@ def get_key(): ch = msvcrt.getch() # second call returns the actual key code if ch == b'\x03': - raise const.KEY_CTRL_C + return const.KEY_CTRL_C if ch == b'H': return const.KEY_UP if ch == b'P':