From d5ae3a6b41fb876dd04ab2a77f37a506aed86963 Mon Sep 17 00:00:00 2001 From: nvbn Date: Fri, 22 Apr 2016 03:14:31 +0300 Subject: [PATCH 1/2] Merge branch 'fish-put-to-history' of https://github.com/scorphus/thefuck into scorphus-fish-put-to-history # Please enter a commit message to explain why this merge is necessary, # especially if it merges an updated upstream into a topic branch. # # Lines starting with '#' will be ignored, and an empty message aborts # the commit. --- tests/shells/test_fish.py | 9 +++++++++ thefuck/shells/fish.py | 20 ++++++++++++++++++++ thefuck/shells/generic.py | 3 +++ thefuck/types.py | 2 ++ 4 files changed, 34 insertions(+) diff --git a/tests/shells/test_fish.py b/tests/shells/test_fish.py index c561352f..5e3e7665 100644 --- a/tests/shells/test_fish.py +++ b/tests/shells/test_fish.py @@ -78,3 +78,12 @@ class TestFish(object): history_lines(['- cmd: ls', ' when: 1432613911', '- cmd: rm', ' when: 1432613916']) assert list(shell.get_history()) == ['ls', 'rm'] + + @pytest.mark.parametrize('entry, entry_utf8', [ + ('ls', '- cmd: ls\n when: 1430707243\n'), + (u'echo café', '- cmd: echo café\n when: 1430707243\n')]) + def test_put_to_history(self, entry, entry_utf8, builtins_open, mocker, shell): + mocker.patch('thefuck.shells.fish.time', return_value=1430707243.3517463) + shell.put_to_history(entry) + builtins_open.return_value.__enter__.return_value. \ + write.assert_called_once_with(entry_utf8) diff --git a/thefuck/shells/fish.py b/thefuck/shells/fish.py index ab7ed477..a2a487e3 100644 --- a/thefuck/shells/fish.py +++ b/thefuck/shells/fish.py @@ -1,6 +1,9 @@ from subprocess import Popen, PIPE from time import time import os +import sys +import six +from .. import logs from ..utils import DEVNULL, memoize, cache from .generic import Generic @@ -65,3 +68,20 @@ class Fish(Generic): def how_to_configure(self): return (r"eval (thefuck --alias | tr '\n' ';')", '~/.config/fish/config.fish') + + def put_to_history(self, command): + try: + return self._put_to_history(command) + except IOError: + logs.exception("Can't update history", sys.exc_info()) + + def _put_to_history(self, command_script): + """Puts command script to shell history.""" + history_file_name = self._get_history_file_name() + if os.path.isfile(history_file_name): + with open(history_file_name, 'a') as history: + entry = self._get_history_line(command_script) + if six.PY2: + history.write(entry.encode('utf-8')) + else: + history.write(entry) diff --git a/thefuck/shells/generic.py b/thefuck/shells/generic.py index 58ecf10b..ba9e0cdd 100644 --- a/thefuck/shells/generic.py +++ b/thefuck/shells/generic.py @@ -81,3 +81,6 @@ class Generic(object): def _script_from_history(self, line): return line + + def put_to_history(self, command): + pass diff --git a/thefuck/types.py b/thefuck/types.py index 62857609..30968be1 100644 --- a/thefuck/types.py +++ b/thefuck/types.py @@ -280,6 +280,8 @@ class CorrectedCommand(object): """ if self.side_effect: compatibility_call(self.side_effect, old_cmd, self.script) + if settings.alter_history: + shell.put_to_history(self.script) # This depends on correct setting of PYTHONIOENCODING by the alias: logs.debug(u'PYTHONIOENCODING: {}'.format( os.environ.get('PYTHONIOENCODING', '!!not-set!!'))) From 51839e65cd75a4d0c8e7c44431da86330173e2de Mon Sep 17 00:00:00 2001 From: nvbn Date: Fri, 22 Apr 2016 03:16:16 +0300 Subject: [PATCH 2/2] #495: Add comment in `put_to_history` --- thefuck/shells/generic.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/thefuck/shells/generic.py b/thefuck/shells/generic.py index ba9e0cdd..e20d7ec3 100644 --- a/thefuck/shells/generic.py +++ b/thefuck/shells/generic.py @@ -83,4 +83,9 @@ class Generic(object): return line def put_to_history(self, command): - pass + """Adds fixed command to shell history. + + In most of shells we change history on shell-level, but not + all shells support it (Fish). + + """