Skip to content

Commit 85a0f7f

Browse files
authored
Merge pull request #35 from orestisf1993/master
Add option to abort when alias can be used
2 parents c2e1ecf + b6c82c2 commit 85a0f7f

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ export ZSH_PLUGINS_ALIAS_TIPS_EXPAND=0
157157
```
158158

159159

160+
### Force Alias Use
161+
162+
If you want to force yourself to use the aliases you set you can enable this
163+
option through this environmental variable:
164+
165+
```
166+
:
167+
export ZSH_PLUGINS_ALIAS_TIPS_FORCE=1
168+
:
169+
```
170+
171+
This will make alias-tips to abort the command you have entered if there exists
172+
an alias for it.
173+
174+
160175
# Limitations
161176
162177
- *Suffix* and *Global* aliases are currently not supported. Only *Prefix*

alias-tips.plugin.zsh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ _alias_tips__preexec () {
2727

2828
shell_functions=$(functions | grep '^[a-zA-Z].\+ () {$')
2929

30+
# Exit code returned from python script when we want to force use of aliases.
31+
local force_exit_code=10
3032
echo $shell_functions "\n" $git_aliases "\n" $shell_aliases | \
3133
python ${_alias_tips__PLUGIN_DIR}/alias-tips.py $*
34+
ret=$?
35+
if [[ $ret = $force_exit_code ]]; then exit $force_exit_code; fi
3236
}
3337

3438
autoload -Uz add-zsh-hook

alias-tips.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import sys
55

6+
FORCE_EXIT_CODE = 10
7+
68

79
def format_tip(s, prefix):
810
color_blue_normal = '\033[94m'
@@ -92,6 +94,7 @@ def main(args):
9294
prefix = os.getenv('ZSH_PLUGINS_ALIAS_TIPS_TEXT', 'Alias tip: ')
9395
expand = os.getenv('ZSH_PLUGINS_ALIAS_TIPS_EXPAND', '1') == '1'
9496
excludes = os.getenv('ZSH_PLUGINS_ALIAS_TIPS_EXCLUDES', '')
97+
force = os.getenv('ZSH_PLUGINS_ALIAS_TIPS_FORCE', '0') == '1'
9598
input = args[0].strip() # Other args are resolved aliases
9699
als, fns = split(sys.stdin.readlines())
97100

@@ -105,6 +108,8 @@ def main(args):
105108

106109
if len(alias) < len(input) and alias != input:
107110
print(format_tip(alias, prefix))
111+
if force:
112+
sys.exit(FORCE_EXIT_CODE)
108113
else:
109114
sys.exit(1)
110115

test_alias-tips.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,21 @@
88

99

1010
def run_blackboxed(args, aliases):
11-
p = subprocess.Popen(['python', './alias-tips.py', args], stdout=PIPE, stdin=PIPE, stderr=PIPE)
11+
p = run_process(args)
1212
stdout, _ = p.communicate(input=aliases.encode())
1313
return stdout
1414

1515

16+
def run_blackboxed_exit_code(args, aliases):
17+
p = run_process(args)
18+
stdout, _ = p.communicate(input=aliases.encode())
19+
return stdout, p.returncode
20+
21+
22+
def run_process(args):
23+
return subprocess.Popen(['python', './alias-tips.py', args], stdout=PIPE, stdin=PIPE, stderr=PIPE)
24+
25+
1626
class TestAliasTipFormatting(TestCase):
1727
def test_no_prefix(self):
1828
self.assertEqual(alias_tips.format_tip('foo', ''), '\x1b[94m\x1b[1;94mfoo\x1b[0m')
@@ -159,3 +169,13 @@ def test_expand_env(self):
159169
self.assertEqual(run_blackboxed('gR -v', 'gRv=\'git remote -v\'\ngR=\'git remote\''), b'\x1b[94m\x1b[1;94mgRv\x1b[0m\n')
160170
self.assertEqual(run_blackboxed('gR -v -foo', 'gRv=\'git remote -v\'\ngR=\'git remote\''), b'\x1b[94m\x1b[1;94mgRv -foo\x1b[0m\n')
161171
self.assertEqual(run_blackboxed('g status -sb', 'g=\'git\'\ngit st = git status -sb'), b'\x1b[94m\x1b[1;94mg st\x1b[0m\n')
172+
173+
def test_force_abort(self):
174+
os.putenv('ZSH_PLUGINS_ALIAS_TIPS_FORCE', '1')
175+
os.putenv('ZSH_PLUGINS_ALIAS_TIPS_TEXT', '')
176+
stdout, returncode = run_blackboxed_exit_code('foo', 'b=foo')
177+
self.assertEqual(returncode, alias_tips.FORCE_EXIT_CODE)
178+
self.assertEqual(stdout, b'\x1b[94m\x1b[1;94mb\x1b[0m\n')
179+
stdout, returncode = run_blackboxed_exit_code('foo', '')
180+
self.assertNotEqual(returncode, alias_tips.FORCE_EXIT_CODE)
181+
self.assertEqual(stdout, b'')

0 commit comments

Comments
 (0)