cheatsheet.py 3.0 KB

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