cheatsheet.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. cheatsheet = {'_default': []}
  17. for key, group in itertools.groupby(exps, lambda exp:exp[2]):
  18. group_list = [ item for item in group ]
  19. if len(group_list)==1:
  20. target_aliases = cheatsheet['_default']
  21. else:
  22. if key not in cheatsheet:
  23. cheatsheet[key] = []
  24. target_aliases = cheatsheet[key]
  25. target_aliases.extend(group_list)
  26. return cheatsheet
  27. def pretty_print_group(key, aliases, highlight=None, only_groupname=False):
  28. if len(aliases) == 0:
  29. return
  30. group_hl_formatter = lambda g, hl: termcolor.colored(hl, 'yellow').join([termcolor.colored(part, 'red') for part in ('[%s]' % g).split(hl)])
  31. 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)])
  32. group_formatter = lambda g: termcolor.colored('[%s]' % g, 'red')
  33. alias_formatter = lambda alias: termcolor.colored('\t%s = %s' % alias[0:2], 'green')
  34. if highlight and len(highlight)>0:
  35. print (group_hl_formatter(key, highlight))
  36. if not only_groupname:
  37. print ('\n'.join([alias_hl_formatter(alias, highlight) for alias in aliases]))
  38. else:
  39. print (group_formatter(key))
  40. if not only_groupname:
  41. print ('\n'.join([alias_formatter(alias) for alias in aliases]))
  42. print ('')
  43. def pretty_print(cheatsheet, wfilter, group_list=None, groups_only=False):
  44. sorted_key = sorted(cheatsheet.keys())
  45. for key in sorted_key:
  46. if group_list and key not in group_list:
  47. continue
  48. aliases = cheatsheet.get(key)
  49. if not wfilter:
  50. pretty_print_group(key, aliases, wfilter, groups_only)
  51. else:
  52. pretty_print_group(key, [ alias for alias in aliases if alias[0].find(wfilter)>-1 or alias[1].find(wfilter)>-1], wfilter)
  53. if __name__ == '__main__':
  54. parser = argparse.ArgumentParser(description="Pretty print aliases.")
  55. parser.add_argument('filter', nargs="*", help="search aliases matching string")
  56. parser.add_argument('-g', '--group', dest="group_list", action='append', help="only print aliases in given groups")
  57. parser.add_argument('--groups', dest='groups_only', action='store_true', help="only print alias groups")
  58. args = parser.parse_args()
  59. lines = sys.stdin.readlines()
  60. group_list = args.group_list or None
  61. wfilter = " ".join(args.filter) or None
  62. pretty_print(cheatsheet(lines), wfilter, group_list, args.groups_only)