1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-02-21 20:38:54 +00:00

⚠️ #442: Change history only on shell side

This commit is contained in:
nvbn 2016-02-22 18:31:28 +03:00
parent f20d4dbf85
commit 9b260eb239
8 changed files with 20 additions and 71 deletions

View File

@ -48,16 +48,6 @@ class TestFish(object):
def test_to_shell(self, shell): def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd' assert shell.to_shell('pwd') == 'pwd'
@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)
def test_and_(self, shell): def test_and_(self, shell):
assert shell.and_('foo', 'bar') == 'foo; and bar' assert shell.and_('foo', 'bar') == 'foo; and bar'

View File

@ -15,11 +15,6 @@ class TestGeneric(object):
def test_to_shell(self, shell): def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd' assert shell.to_shell('pwd') == 'pwd'
def test_put_to_history(self, builtins_open, shell):
assert shell.put_to_history('ls') is None
assert shell.put_to_history(u'echo café') is None
assert builtins_open.call_count == 0
def test_and_(self, shell): def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd' assert shell.and_('ls', 'cd') == 'ls && cd'

View File

@ -31,16 +31,6 @@ class TestTcsh(object):
def test_to_shell(self, shell): def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd' assert shell.to_shell('pwd') == 'pwd'
@pytest.mark.parametrize('entry, entry_utf8', [
('ls', '#+1430707243\nls\n'),
(u'echo café', '#+1430707243\necho café\n')])
def test_put_to_history(self, entry, entry_utf8, builtins_open, shell, mocker):
mocker.patch('thefuck.shells.tcsh.time',
return_value=1430707243.3517463)
shell.put_to_history(entry)
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with(entry_utf8)
def test_and_(self, shell): def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd' assert shell.and_('ls', 'cd') == 'ls && cd'

View File

@ -30,16 +30,6 @@ class TestZsh(object):
def test_to_shell(self, shell): def test_to_shell(self, shell):
assert shell.to_shell('pwd') == 'pwd' assert shell.to_shell('pwd') == 'pwd'
@pytest.mark.parametrize('entry, entry_utf8', [
('ls', ': 1430707243:0;ls\n'),
(u'echo café', ': 1430707243:0;echo café\n')])
def test_put_to_history(self, entry, entry_utf8, builtins_open, mocker, shell):
mocker.patch('thefuck.shells.zsh.time',
return_value=1430707243.3517463)
shell.put_to_history(entry)
builtins_open.return_value.__enter__.return_value. \
write.assert_called_once_with(entry_utf8)
def test_and_(self, shell): def test_and_(self, shell):
assert shell.and_('ls', 'cd') == 'ls && cd' assert shell.and_('ls', 'cd') == 'ls && cd'
@ -54,8 +44,7 @@ class TestZsh(object):
assert 'alias fuck' in shell.app_alias('fuck') assert 'alias fuck' in shell.app_alias('fuck')
assert 'alias FUCK' in shell.app_alias('FUCK') assert 'alias FUCK' in shell.app_alias('FUCK')
assert 'thefuck' in shell.app_alias('fuck') assert 'thefuck' in shell.app_alias('fuck')
assert 'TF_ALIAS=fuck PYTHONIOENCODING' in shell.app_alias('fuck') assert 'PYTHONIOENCODING' in shell.app_alias('fuck')
assert 'PYTHONIOENCODING=utf-8 thefuck' in shell.app_alias('fuck')
def test_get_history(self, history_lines, shell): def test_get_history(self, history_lines, shell):
history_lines([': 1432613911:0;ls', ': 1432613916:0;rm']) history_lines([': 1432613911:0;ls', ': 1432613916:0;rm'])

View File

@ -1,16 +1,21 @@
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
import os import os
from ..conf import settings
from ..utils import DEVNULL, memoize, cache from ..utils import DEVNULL, memoize, cache
from .generic import Generic from .generic import Generic
class Bash(Generic): class Bash(Generic):
def app_alias(self, fuck): def app_alias(self, fuck):
return "TF_ALIAS={0}" \ alias = "TF_ALIAS={0}" \
" alias {0}='PYTHONIOENCODING=utf-8" \ " alias {0}='PYTHONIOENCODING=utf-8" \
" TF_CMD=$(thefuck $(fc -ln -1)) && " \ " TF_CMD=$(thefuck $(fc -ln -1)) && " \
" eval $TF_CMD &&" \ " eval $TF_CMD".format(fuck)
" history -s $TF_CMD'".format(fuck)
if settings.alter_history:
return alias + " && history -s $TF_CMD'"
else:
return alias + "'"
def _parse_alias(self, alias): def _parse_alias(self, alias):
name, value = alias.replace('alias ', '', 1).split('=', 1) name, value = alias.replace('alias ', '', 1).split('=', 1)
@ -34,10 +39,6 @@ class Bash(Generic):
def _get_history_line(self, command_script): def _get_history_line(self, command_script):
return u'{}\n'.format(command_script) return u'{}\n'.format(command_script)
def put_to_history(self, command_script):
# handled by the alias
pass
def how_to_configure(self): def how_to_configure(self):
if os.path.join(os.path.expanduser('~'), '.bashrc'): if os.path.join(os.path.expanduser('~'), '.bashrc'):
config = '~/.bashrc' config = '~/.bashrc'

View File

@ -2,11 +2,8 @@ import io
import os import os
import shlex import shlex
import six import six
import sys
from ..utils import memoize from ..utils import memoize
from ..conf import settings from ..conf import settings
from .. import logs
class Generic(object): class Generic(object):
@ -39,23 +36,6 @@ class Generic(object):
def _get_history_line(self, command_script): def _get_history_line(self, command_script):
return '' return ''
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)
@memoize @memoize
def get_history(self): def get_history(self):
return list(self._get_history_lines()) return list(self._get_history_lines())

View File

@ -1,15 +1,21 @@
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
from time import time from time import time
import os import os
from ..conf import settings
from ..utils import DEVNULL, memoize, cache from ..utils import DEVNULL, memoize, cache
from .generic import Generic from .generic import Generic
class Zsh(Generic): class Zsh(Generic):
def app_alias(self, fuck): def app_alias(self, fuck):
return "alias {0}='eval $(TF_ALIAS={0} PYTHONIOENCODING=utf-8" \ alias = "alias {0}='TF_ALIAS={0} PYTHONIOENCODING=utf-8" \
" thefuck $(fc -ln -1 | tail -n 1));" \ " TF_CMD=$(thefuck $(fc -ln -1 | tail -n 1)) &&" \
" fc -R'".format(fuck) " eval $TF_CMD".format(fuck)
if settings.alter_history:
return alias + " && print -s $TF_CMD'"
else:
return alias + "'"
def _parse_alias(self, alias): def _parse_alias(self, alias):
name, value = alias.split('=', 1) name, value = alias.split('=', 1)

View File

@ -279,8 +279,6 @@ 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-<')))