e47a8e2321be9e7af49872588619c031a757d6ea

Author
hqingyi <huangb.qingy@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(plugins): add aliases cheatsheet plugin (#4662)

* add: explore alias quickly with aliases plugin.

* change: add compatibility with python2 & python3.

- add compatibility.
- add termcolor.py.
- remove aliass(search), just use acs.
- detect python.

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/aliases/aliases.plugin.zsh b/plugins/aliases/aliases.plugin.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..28d8fba241d8882dc33faaff3f6acc375499cfc0
  4--- /dev/null
  5+++ b/plugins/aliases/aliases.plugin.zsh
  6@@ -0,0 +1,10 @@
  7+# with lots of 3rd-party amazing aliases installed, just need something to explore it quickly.
  8+#
  9+# - acs: alias cheatsheet
 10+#   group alias by command, pass addition argv to grep.
 11+ALIASES_PLUGIN_ROOT=$(cd `dirname $0` && pwd)
 12+function acs(){
 13+    which python >>/dev/null
 14+    [[ $? -eq 1 ]] && echo "[error]no python executable detected!" && return
 15+    alias | python $ALIASES_PLUGIN_ROOT/cheatsheet.py $@
 16+}
 17diff --git a/plugins/aliases/cheatsheet.py b/plugins/aliases/cheatsheet.py
 18new file mode 100644
 19index 0000000000000000000000000000000000000000..d6d507b92f76788083da7c59f5b63e2c5f22b760
 20--- /dev/null
 21+++ b/plugins/aliases/cheatsheet.py
 22@@ -0,0 +1,55 @@
 23+#!/usr/bin/env python
 24+import sys
 25+import itertools
 26+import termcolor
 27+
 28+def parse(line):
 29+    left = line[0:line.find('=')].strip()
 30+    right = line[line.find('=')+1:].strip('\'"\n ')
 31+    try:
 32+        cmd = next(part for part in right.split() if len([char for char in '=<>' if char in part])==0)
 33+    except StopIteration:
 34+        cmd = right
 35+    return (left, right, cmd)
 36+
 37+def cheatsheet(lines):
 38+    exps = [ parse(line) for line in lines ]
 39+    cheatsheet = {'_default': []}
 40+    for key, group in itertools.groupby(exps, lambda exp:exp[2]):
 41+        group_list = [ item for item in group ]
 42+        if len(group_list)==1:
 43+            target_aliases = cheatsheet['_default']
 44+        else:
 45+            if key not in cheatsheet:
 46+                cheatsheet[key] = []
 47+            target_aliases = cheatsheet[key]
 48+        target_aliases.extend(group_list)
 49+    return cheatsheet
 50+
 51+def pretty_print_group(key, aliases, hightlight=None):
 52+    if len(aliases) == 0:
 53+        return
 54+    group_hl_formatter = lambda g, hl: termcolor.colored(hl, 'yellow').join([termcolor.colored(part, 'red') for part in ('[%s]' % g).split(hl)])
 55+    alias_hl_formatter = lambda alias, hl: termcolor.colored(hl, 'yellow').join([termcolor.colored(part, 'green') for part in ('\t%s = %s' % alias[0:2]).split(hl)])
 56+    group_formatter = lambda g: termcolor.colored('[%s]' % g, 'red')
 57+    alias_formatter = lambda alias: termcolor.colored('\t%s = %s' % alias[0:2], 'green')
 58+    if hightlight and len(hightlight)>0:
 59+        print (group_hl_formatter(key, hightlight))
 60+        print ('\n'.join([alias_hl_formatter(alias, hightlight) for alias in aliases]))
 61+    else:
 62+        print (group_formatter(key))
 63+        print ('\n'.join([alias_formatter(alias) for alias in aliases]))
 64+    print ('')
 65+
 66+def pretty_print(cheatsheet, wfilter):
 67+    sorted_key = sorted(cheatsheet.keys())
 68+    for key in sorted_key:
 69+        aliases = cheatsheet.get(key)
 70+        if not wfilter:
 71+            pretty_print_group(key, aliases, wfilter)
 72+        else:
 73+            pretty_print_group(key, [ alias for alias in aliases if alias[0].find(wfilter)>-1 or alias[1].find(wfilter)>-1], wfilter)
 74+
 75+if __name__ == '__main__':
 76+    lines = sys.stdin.readlines()
 77+    pretty_print(cheatsheet(lines), sys.argv[1] if len(sys.argv)>1 else None)
 78diff --git a/plugins/aliases/termcolor.py b/plugins/aliases/termcolor.py
 79new file mode 100644
 80index 0000000000000000000000000000000000000000..f11b824b287fd9b93c3d7a4526f909bba7698fae
 81--- /dev/null
 82+++ b/plugins/aliases/termcolor.py
 83@@ -0,0 +1,168 @@
 84+# coding: utf-8
 85+# Copyright (c) 2008-2011 Volvox Development Team
 86+#
 87+# Permission is hereby granted, free of charge, to any person obtaining a copy
 88+# of this software and associated documentation files (the "Software"), to deal
 89+# in the Software without restriction, including without limitation the rights
 90+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 91+# copies of the Software, and to permit persons to whom the Software is
 92+# furnished to do so, subject to the following conditions:
 93+#
 94+# The above copyright notice and this permission notice shall be included in
 95+# all copies or substantial portions of the Software.
 96+#
 97+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 98+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 99+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
100+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
101+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
102+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
103+# THE SOFTWARE.
104+#
105+# Author: Konstantin Lepa <konstantin.lepa@gmail.com>
106+
107+"""ANSII Color formatting for output in terminal."""
108+
109+from __future__ import print_function
110+import os
111+
112+
113+__ALL__ = [ 'colored', 'cprint' ]
114+
115+VERSION = (1, 1, 0)
116+
117+ATTRIBUTES = dict(
118+        list(zip([
119+            'bold',
120+            'dark',
121+            '',
122+            'underline',
123+            'blink',
124+            '',
125+            'reverse',
126+            'concealed'
127+            ],
128+            list(range(1, 9))
129+            ))
130+        )
131+del ATTRIBUTES['']
132+
133+
134+HIGHLIGHTS = dict(
135+        list(zip([
136+            'on_grey',
137+            'on_red',
138+            'on_green',
139+            'on_yellow',
140+            'on_blue',
141+            'on_magenta',
142+            'on_cyan',
143+            'on_white'
144+            ],
145+            list(range(40, 48))
146+            ))
147+        )
148+
149+
150+COLORS = dict(
151+        list(zip([
152+            'grey',
153+            'red',
154+            'green',
155+            'yellow',
156+            'blue',
157+            'magenta',
158+            'cyan',
159+            'white',
160+            ],
161+            list(range(30, 38))
162+            ))
163+        )
164+
165+
166+RESET = '\033[0m'
167+
168+
169+def colored(text, color=None, on_color=None, attrs=None):
170+    """Colorize text.
171+
172+    Available text colors:
173+        red, green, yellow, blue, magenta, cyan, white.
174+
175+    Available text highlights:
176+        on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white.
177+
178+    Available attributes:
179+        bold, dark, underline, blink, reverse, concealed.
180+
181+    Example:
182+        colored('Hello, World!', 'red', 'on_grey', ['blue', 'blink'])