2022-04-09 14:45:42 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-06-12 04:45:05 +02:00
|
|
|
import sys
|
|
|
|
import itertools
|
|
|
|
import termcolor
|
2022-10-18 19:40:02 +02:00
|
|
|
import argparse
|
2021-06-12 04:45:05 +02:00
|
|
|
|
|
|
|
def parse(line):
|
|
|
|
left = line[0:line.find('=')].strip()
|
|
|
|
right = line[line.find('=')+1:].strip('\'"\n ')
|
|
|
|
try:
|
|
|
|
cmd = next(part for part in right.split() if len([char for char in '=<>' if char in part])==0)
|
|
|
|
except StopIteration:
|
|
|
|
cmd = right
|
|
|
|
return (left, right, cmd)
|
|
|
|
|
|
|
|
def cheatsheet(lines):
|
|
|
|
exps = [ parse(line) for line in lines ]
|
|
|
|
cheatsheet = {'_default': []}
|
|
|
|
for key, group in itertools.groupby(exps, lambda exp:exp[2]):
|
|
|
|
group_list = [ item for item in group ]
|
|
|
|
if len(group_list)==1:
|
|
|
|
target_aliases = cheatsheet['_default']
|
|
|
|
else:
|
|
|
|
if key not in cheatsheet:
|
|
|
|
cheatsheet[key] = []
|
|
|
|
target_aliases = cheatsheet[key]
|
|
|
|
target_aliases.extend(group_list)
|
|
|
|
return cheatsheet
|
|
|
|
|
2022-10-18 19:40:02 +02:00
|
|
|
def pretty_print_group(key, aliases, highlight=None, only_groupname=False):
|
2021-06-12 04:45:05 +02:00
|
|
|
if len(aliases) == 0:
|
|
|
|
return
|
|
|
|
group_hl_formatter = lambda g, hl: termcolor.colored(hl, 'yellow').join([termcolor.colored(part, 'red') for part in ('[%s]' % g).split(hl)])
|
|
|
|
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)])
|
|
|
|
group_formatter = lambda g: termcolor.colored('[%s]' % g, 'red')
|
|
|
|
alias_formatter = lambda alias: termcolor.colored('\t%s = %s' % alias[0:2], 'green')
|
2021-12-01 12:20:31 +01:00
|
|
|
if highlight and len(highlight)>0:
|
|
|
|
print (group_hl_formatter(key, highlight))
|
2022-10-18 19:40:02 +02:00
|
|
|
if not only_groupname:
|
|
|
|
print ('\n'.join([alias_hl_formatter(alias, highlight) for alias in aliases]))
|
2021-06-12 04:45:05 +02:00
|
|
|
else:
|
|
|
|
print (group_formatter(key))
|
2022-10-18 19:40:02 +02:00
|
|
|
if not only_groupname:
|
|
|
|
print ('\n'.join([alias_formatter(alias) for alias in aliases]))
|
2021-06-12 04:45:05 +02:00
|
|
|
print ('')
|
|
|
|
|
2022-10-18 19:40:02 +02:00
|
|
|
def pretty_print(cheatsheet, wfilter, group_list=None, groups_only=False):
|
2021-06-12 04:45:05 +02:00
|
|
|
sorted_key = sorted(cheatsheet.keys())
|
|
|
|
for key in sorted_key:
|
2022-10-18 19:40:02 +02:00
|
|
|
if group_list and key not in group_list:
|
|
|
|
continue
|
2021-06-12 04:45:05 +02:00
|
|
|
aliases = cheatsheet.get(key)
|
|
|
|
if not wfilter:
|
2023-02-27 20:39:38 +01:00
|
|
|
pretty_print_group(key, aliases, only_groupname=groups_only)
|
2021-06-12 04:45:05 +02:00
|
|
|
else:
|
2023-02-27 20:39:38 +01:00
|
|
|
pretty_print_group(key, [ alias for alias in aliases if wfilter in alias[0] or wfilter in alias[1] ], wfilter)
|
2021-06-12 04:45:05 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-02-27 20:39:38 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Pretty print aliases.", prog="acs")
|
|
|
|
parser.add_argument('filter', nargs="*", metavar="<keyword>", help="search aliases matching keywords")
|
2022-10-18 19:40:02 +02:00
|
|
|
parser.add_argument('-g', '--group', dest="group_list", action='append', help="only print aliases in given groups")
|
|
|
|
parser.add_argument('--groups', dest='groups_only', action='store_true', help="only print alias groups")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-06-12 04:45:05 +02:00
|
|
|
lines = sys.stdin.readlines()
|
2022-10-18 19:40:02 +02:00
|
|
|
group_list = args.group_list or None
|
2023-02-27 20:39:38 +01:00
|
|
|
wfilter = " ".join(args.filter[1:]) if args.filter else None
|
2022-10-18 19:40:02 +02:00
|
|
|
pretty_print(cheatsheet(lines), wfilter, group_list, args.groups_only)
|