#89 #152 Use shell history

This commit is contained in:
nvbn
2015-05-04 04:44:16 +02:00
parent 72ac9650f9
commit 3f2fe0d275
7 changed files with 94 additions and 114 deletions
-27
View File
@@ -1,27 +0,0 @@
import os
import shelve
from tempfile import gettempdir
from psutil import Process
class History(object):
"""Temporary history of commands/fixed-commands dependent on
current shell instance.
"""
def __init__(self):
self._path = os.path.join(gettempdir(), '.thefuck_history')
self._pid = Process(os.getpid()).parent().pid
self._db = shelve.open(self._path)
def _prepare_key(self, key):
return '{}-{}'.format(self._pid, key)
def update(self, **kwargs):
self._db.update({self._prepare_key(k): v for k,v in kwargs.items()})
self._db.sync()
return self
def __getattr__(self, item):
return self._db.get(self._prepare_key(item))
+7 -14
View File
@@ -6,7 +6,7 @@ import os
import sys
from psutil import Process, TimeoutExpired
import colorama
from .history import History
import six
from . import logs, conf, types, shells
@@ -60,22 +60,17 @@ def wait_output(settings, popen):
return False
def get_command(settings, history, args):
def get_command(settings, args):
"""Creates command from `args` and executes it."""
if sys.version_info[0] < 3:
if six.PY2:
script = ' '.join(arg.decode('utf-8') for arg in args[1:])
else:
script = ' '.join(args[1:])
if script == 'fuck' or script == history.last_command:
script = history.last_fixed_command or history.last_command
if not script:
return
script = shells.from_shell(script)
history.update(last_command=script,
last_fixed_command=None)
result = Popen(script, shell=True, stdout=PIPE, stderr=PIPE,
env=dict(os.environ, LANG='C'))
if wait_output(settings, result):
@@ -108,14 +103,13 @@ def confirm(new_command, side_effect, settings):
return False
def run_rule(rule, command, history, settings):
def run_rule(rule, command, settings):
"""Runs command from rule for passed command."""
new_command = shells.to_shell(rule.get_new_command(command, settings))
if confirm(new_command, rule.side_effect, settings):
if rule.side_effect:
rule.side_effect(command, settings)
history.update(last_command=command.script,
last_fixed_command=new_command)
shells.put_to_history(new_command)
print(new_command)
@@ -127,14 +121,13 @@ def main():
colorama.init()
user_dir = setup_user_dir()
settings = conf.get_settings(user_dir)
history = History()
command = get_command(settings, history, sys.argv)
command = get_command(settings, sys.argv)
if command:
rules = get_rules(user_dir, settings)
matched_rule = get_matched_rule(command, rules, settings)
if matched_rule:
run_rule(matched_rule, command, history, settings)
run_rule(matched_rule, command, settings)
return
logs.failed('No fuck given', settings)
+34 -1
View File
@@ -1,9 +1,11 @@
"""Module with shell specific actions, each shell class should
implement `from_shell` and `to_shell` methods.
implement `from_shell`, `to_shell`, `app_alias` and `put_to_history`
methods.
"""
from collections import defaultdict
from subprocess import Popen, PIPE
from time import time
import os
from psutil import Process
@@ -34,6 +36,19 @@ class Generic(object):
def app_alias(self):
return "\nalias fuck='eval $(thefuck $(fc -ln -1))'\n"
def _get_history_file_name(self):
return ''
def _get_history_line(self, command_script):
return ''
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:
history.write(self._get_history_line(command_script))
class Bash(Generic):
def _parse_alias(self, alias):
@@ -49,6 +64,13 @@ class Bash(Generic):
for alias in proc.stdout.read().decode('utf-8').split('\n')
if alias)
def _get_history_file_name(self):
return os.environ.get("HISTFILE",
os.path.expanduser('~/.bash_history'))
def _get_history_line(self, command_script):
return u'{}\n'.format(command_script)
class Zsh(Generic):
def _parse_alias(self, alias):
@@ -64,6 +86,13 @@ class Zsh(Generic):
for alias in proc.stdout.read().decode('utf-8').split('\n')
if alias)
def _get_history_file_name(self):
return os.environ.get("HISTFILE",
os.path.expanduser('~/.zsh_history'))
def _get_history_line(self, command_script):
return u': {}:0;{}\n'.format(int(time()), command_script)
shells = defaultdict(lambda: Generic(), {
'bash': Bash(),
@@ -85,3 +114,7 @@ def to_shell(command):
def app_alias():
return _get_shell().app_alias()
def put_to_history(command):
return _get_shell().put_to_history(command)