1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-27 15:28:56 +00:00

Merge branch 'scorphus-fish-put-to-history'

This commit is contained in:
nvbn 2016-04-22 03:17:40 +03:00
commit d6c2c7266d
4 changed files with 39 additions and 0 deletions

View File

@ -78,3 +78,12 @@ class TestFish(object):
history_lines(['- cmd: ls', ' when: 1432613911', history_lines(['- cmd: ls', ' when: 1432613911',
'- cmd: rm', ' when: 1432613916']) '- cmd: rm', ' when: 1432613916'])
assert list(shell.get_history()) == ['ls', 'rm'] 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)

View File

@ -1,6 +1,9 @@
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from time import time from time import time
import os import os
import sys
import six
from .. import logs
from ..utils import DEVNULL, memoize, cache from ..utils import DEVNULL, memoize, cache
from .generic import Generic from .generic import Generic
@ -65,3 +68,20 @@ class Fish(Generic):
def how_to_configure(self): def how_to_configure(self):
return (r"eval (thefuck --alias | tr '\n' ';')", return (r"eval (thefuck --alias | tr '\n' ';')",
'~/.config/fish/config.fish') '~/.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)

View File

@ -81,3 +81,11 @@ class Generic(object):
def _script_from_history(self, line): def _script_from_history(self, line):
return line return line
def put_to_history(self, command):
"""Adds fixed command to shell history.
In most of shells we change history on shell-level, but not
all shells support it (Fish).
"""

View File

@ -280,6 +280,8 @@ class CorrectedCommand(object):
""" """
if self.side_effect: if self.side_effect:
compatibility_call(self.side_effect, old_cmd, self.script) 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: # This depends on correct setting of PYTHONIOENCODING by the alias:
logs.debug(u'PYTHONIOENCODING: {}'.format( logs.debug(u'PYTHONIOENCODING: {}'.format(
os.environ.get('PYTHONIOENCODING', '!!not-set!!'))) os.environ.get('PYTHONIOENCODING', '!!not-set!!')))