Skip to content

Commit

Permalink
Merge pull request nvbn#474 from scorphus/alias-variables
Browse files Browse the repository at this point in the history
nvbn#301: Set variables within the alias
  • Loading branch information
nvbn committed Mar 10, 2016
2 parents d92765d + bb5f6bb commit 4fb99fd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
7 changes: 7 additions & 0 deletions tests/shells/test_bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ def test_app_alias(self, shell):
assert 'TF_ALIAS=fuck' in shell.app_alias('fuck')
assert 'PYTHONIOENCODING=utf-8' in shell.app_alias('fuck')

def test_app_alias_variables_correctly_set(self, shell):
alias = shell.app_alias('fuck')
assert "alias fuck='TF_CMD=$(TF_ALIAS" in alias
assert '$(TF_ALIAS=fuck PYTHONIOENCODING' in alias
assert 'PYTHONIOENCODING=utf-8 TF_SHELL_ALIASES' in alias
assert 'ALIASES=$(alias) thefuck' in alias

def test_get_history(self, history_lines, shell):
history_lines(['ls', 'rm'])
assert list(shell.get_history()) == ['ls', 'rm']
7 changes: 7 additions & 0 deletions tests/shells/test_zsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def test_app_alias(self, shell):
assert 'thefuck' in shell.app_alias('fuck')
assert 'PYTHONIOENCODING' in shell.app_alias('fuck')

def test_app_alias_variables_correctly_set(self, shell):
alias = shell.app_alias('fuck')
assert "alias fuck='TF_CMD=$(TF_ALIAS" in alias
assert '$(TF_ALIAS=fuck PYTHONIOENCODING' in alias
assert 'PYTHONIOENCODING=utf-8 TF_SHELL_ALIASES' in alias
assert 'ALIASES=$(alias) thefuck' in alias

def test_get_history(self, history_lines, shell):
history_lines([': 1432613911:0;ls', ': 1432613916:0;rm'])
assert list(shell.get_history()) == ['ls', 'rm']
8 changes: 5 additions & 3 deletions thefuck/shells/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

class Bash(Generic):
def app_alias(self, fuck):
alias = "TF_ALIAS={0}" \
" alias {0}='PYTHONIOENCODING=utf-8" \
" TF_CMD=$(TF_SHELL_ALIASES=$(alias) thefuck $(fc -ln -1)) && " \
# It is VERY important to have the variables declared WITHIN the alias
alias = "alias {0}='TF_CMD=$(TF_ALIAS={0}" \
" PYTHONIOENCODING=utf-8" \
" TF_SHELL_ALIASES=$(alias)" \
" thefuck $(fc -ln -1)) &&" \
" eval $TF_CMD".format(fuck)

if settings.alter_history:
Expand Down
1 change: 1 addition & 0 deletions thefuck/shells/fish.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def _get_overridden_aliases(self):
return ['cd', 'grep', 'ls', 'man', 'open']

def app_alias(self, fuck):
# It is VERY important to have the variables declared WITHIN the alias
return ('function {0} -d "Correct your previous console command"\n'
' set -l fucked_up_command $history[1]\n'
' env TF_ALIAS={0} PYTHONIOENCODING=utf-8'
Expand Down
7 changes: 4 additions & 3 deletions thefuck/shells/zsh.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

class Zsh(Generic):
def app_alias(self, alias_name):
alias = "alias {0}='TF_ALIAS={0}" \
# It is VERY important to have the variables declared WITHIN the alias
alias = "alias {0}='TF_CMD=$(TF_ALIAS={0}" \
" PYTHONIOENCODING=utf-8" \
' TF_SHELL_ALIASES=$(alias)' \
" TF_CMD=$(thefuck $(fc -ln -1 | tail -n 1)) &&" \
" TF_SHELL_ALIASES=$(alias)" \
" thefuck $(fc -ln -1 | tail -n 1)) &&" \
" eval $TF_CMD".format(alias_name)

if settings.alter_history:
Expand Down
2 changes: 1 addition & 1 deletion thefuck/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,5 +282,5 @@ def run(self, old_cmd):
compatibility_call(self.side_effect, old_cmd, self.script)
# This depends on correct setting of PYTHONIOENCODING by the alias:
logs.debug(u'PYTHONIOENCODING: {}'.format(
os.environ.get('PYTHONIOENCODING', '>-not-set-<')))
os.environ.get('PYTHONIOENCODING', '!!not-set!!')))
print(self.script)

0 comments on commit 4fb99fd

Please sign in to comment.