Skip to content

Commit ed084cf

Browse files
committed
add nushell support
1 parent c7e7e1d commit ed084cf

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

thefuck/shells/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
from .tcsh import Tcsh
1111
from .zsh import Zsh
1212
from .powershell import Powershell
13+
from .nushell import Nushell
1314

1415
shells = {'bash': Bash,
1516
'fish': Fish,
1617
'zsh': Zsh,
1718
'csh': Tcsh,
1819
'tcsh': Tcsh,
1920
'powershell': Powershell,
20-
'pwsh': Powershell}
21+
'pwsh': Powershell,
22+
'nu': Nushell}
2123

2224

2325
def _get_shell_from_env():

thefuck/shells/nushell.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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")

thefuck/types.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2+
import subprocess
23
import sys
4+
from .shells import Nushell
35
from . import logs
46
from .shells import shell
57
from .conf import settings, load_source
@@ -257,5 +259,10 @@ def run(self, old_cmd):
257259
# This depends on correct setting of PYTHONIOENCODING by the alias:
258260
logs.debug(u'PYTHONIOENCODING: {}'.format(
259261
os.environ.get('PYTHONIOENCODING', '!!not-set!!')))
260-
261-
sys.stdout.write(self._get_script())
262+
script = self._get_script()
263+
if isinstance(shell, Nushell):
264+
# TODO: fix for better option, the lack of '-l' flag
265+
# means we lose any ENV that could be needed
266+
subprocess.run(["nu", "-c", script])
267+
else:
268+
sys.stdout.write(script)

0 commit comments

Comments
 (0)