cheatsheet.py
3153 bytes
1#!/usr/bin/env python3
2import sys
3import itertools
4import termcolor
5import argparse
6
7def parse(line):
8 left = line[0:line.find('=')].strip()
9 right = line[line.find('=')+1:].strip('\n ')
10 if len(right) >= 2 and right[0] == right[-1] and right[0] in '\'"':
11 right = right[1:-1]
12 try:
13 cmd = next(part for part in right.split() if len([char for char in '=<>' if char in part])==0)
14 except StopIteration:
15 cmd = right
16 return (left, right, cmd)
17
18def cheatsheet(lines):
19 exps = [ parse(line) for line in lines ]
20 exps.sort(key=lambda exp:exp[2])
21 cheatsheet = {'_default': []}
22 for key, group in itertools.groupby(exps, lambda exp:exp[2]):
23 group_list = [ item for item in group ]
24 if len(group_list)==1:
25 target_aliases = cheatsheet['_default']
26 else:
27 if key not in cheatsheet:
28 cheatsheet[key] = []
29 target_aliases = cheatsheet[key]
30 target_aliases.extend(group_list)
31 return cheatsheet
32
33def pretty_print_group(key, aliases, highlight=None, only_groupname=False):
34 if len(aliases) == 0:
35 return
36 group_hl_formatter = lambda g, hl: termcolor.colored(hl, 'yellow').join([termcolor.colored(part, 'red') for part in ('[%s]' % g).split(hl)])
37 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)])
38 group_formatter = lambda g: termcolor.colored('[%s]' % g, 'red')
39 alias_formatter = lambda alias: termcolor.colored('\t%s = %s' % alias[0:2], 'green')
40 if highlight and len(highlight)>0:
41 print (group_hl_formatter(key, highlight))
42 if not only_groupname:
43 print ('\n'.join([alias_hl_formatter(alias, highlight) for alias in aliases]))
44 else:
45 print (group_formatter(key))
46 if not only_groupname:
47 print ('\n'.join([alias_formatter(alias) for alias in aliases]))
48 print ('')
49
50def pretty_print(cheatsheet, wfilter, group_list=None, groups_only=False):
51 sorted_key = sorted(cheatsheet.keys())
52 for key in sorted_key:
53 if group_list and key not in group_list:
54 continue
55 aliases = cheatsheet.get(key)
56 if not wfilter:
57 pretty_print_group(key, aliases, wfilter, groups_only)
58 else:
59 pretty_print_group(key, [ alias for alias in aliases if alias[0].find(wfilter)>-1 or alias[1].find(wfilter)>-1], wfilter)
60
61if __name__ == '__main__':
62 parser = argparse.ArgumentParser(description="Pretty print aliases.", prog="als")
63 parser.add_argument('filter', nargs="*", metavar="<keyword>", help="search aliases matching keywords")
64 parser.add_argument('-g', '--group', dest="group_list", action='append', help="only print aliases in given groups")
65 parser.add_argument('--groups', dest='groups_only', action='store_true', help="only print alias groups")
66 args = parser.parse_args()
67
68 lines = sys.stdin.readlines()
69 group_list = args.group_list or None
70 wfilter = " ".join(args.filter) or None
71 pretty_print(cheatsheet(lines), wfilter, group_list, args.groups_only)