|
| 1 | +from subprocess import Popen, PIPE |
| 2 | +import os |
| 3 | +import six |
| 4 | +import sys |
| 5 | +from .. import logs |
| 6 | +from ..utils import DEVNULL |
| 7 | +from .generic import Generic |
| 8 | + |
| 9 | + |
| 10 | +class Nushell(Generic): |
| 11 | + friendly_name = 'Nushell Shell' |
| 12 | + |
| 13 | + def get_aliases(self): |
| 14 | + aliases = {} |
| 15 | + command = 'help aliases | select name expansion | each { |row| $row.name + " ; " + $row.expansion } | str join (char nl)' |
| 16 | + proc = Popen(['nu', '-l', '-c', command], stdout=PIPE, stderr=DEVNULL) |
| 17 | + if proc.stdout is None: |
| 18 | + return aliases |
| 19 | + alias_out = proc.stdout.read().decode('utf-8').strip() |
| 20 | + for alias in alias_out.split('\n'): |
| 21 | + split_alias = alias.split(" ; ") |
| 22 | + if len(split_alias) == 2: |
| 23 | + name, value = split_alias |
| 24 | + aliases[name] = value |
| 25 | + return aliases |
| 26 | + |
| 27 | + def app_alias(self, alias_name): |
| 28 | + return 'alias {0} = thefuck $"(history | last 1 | get command | get 0)"'.format(alias_name) |
| 29 | + |
| 30 | + def _get_history_file_name(self): |
| 31 | + return os.path.expanduser('~/.config/nushell/history.txt') |
| 32 | + |
| 33 | + def _get_history_line(self, command_script): |
| 34 | + return command_script |
| 35 | + |
| 36 | + def and_(self, *commands): |
| 37 | + return u' and '.join(commands) |
| 38 | + |
| 39 | + def or_(self, *commands): |
| 40 | + return u' or '.join(commands) |
| 41 | + |
| 42 | + def how_to_configure(self): |
| 43 | + return self._create_shell_configuration( |
| 44 | + content='alias fuck = thefuck $"(history | last 1 | get command | get 0)"', |
| 45 | + path="$nu.config-path", |
| 46 | + reload="source $nu.config-path" |
| 47 | + ) |
| 48 | + |
| 49 | + def _script_from_history(self, line): |
| 50 | + return line |
| 51 | + |
| 52 | + def put_to_history(self, command): |
| 53 | + """Adds fixed command to shell history.""" |
| 54 | + try: |
| 55 | + history_file_name = self._get_history_file_name() |
| 56 | + if os.path.isfile(history_file_name): |
| 57 | + with open(history_file_name, 'a') as history: |
| 58 | + entry = self._get_history_line(command) |
| 59 | + if six.PY2: |
| 60 | + history.write(entry.encode('utf-8')) |
| 61 | + else: |
| 62 | + history.write(entry) |
| 63 | + except IOError: |
| 64 | + logs.exception("Can't update history", sys.exc_info()) |
| 65 | + |
| 66 | + def _get_version(self): |
| 67 | + """Returns the version of the current shell""" |
| 68 | + proc = Popen(['nu', '--version'], stdout=PIPE, stderr=DEVNULL) |
| 69 | + if proc.stdout: |
| 70 | + return proc.stdout.read().decode('utf-8') |
| 71 | + else: |
| 72 | + raise Exception("Could not get the version of the current shell") |
0 commit comments