My dotfiles for my Linux rice managed with stow and make
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

shortcuts.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import csv
  4. from pathlib import Path
  5. from re import sub
  6. from re import compile
  7. ranger_shortcuts = ""
  8. zsh_shortcuts = ""
  9. home = str(Path.home()) + "/"
  10. ranger_location = home + ".config/ranger/rc.conf"
  11. zsh_location = home + ".zsh_aliases"
  12. # These are the labels that demarcate where the shortcuts
  13. # go in the config files.
  14. beg = "# DO NOT DELETE LMAO\n"
  15. end = "# DO NOT DELETE LMAO"
  16. # First we open the list of folder shortcuts and go down each line adding each
  17. # in the required syntax to each of the three configs:
  18. with open(home+".config/Scripts/folders") as fold:
  19. for line in csv.reader(fold, dialect="excel-tab"):
  20. # Adds the ranger go, tab, move and yank commands:
  21. ranger_shortcuts += ("map g" + line[0]
  22. + " cd " + line[1]
  23. + "\n")
  24. ranger_shortcuts += ("map t" + line[0]
  25. + " tab_new " + line[1]
  26. + "\n")
  27. ranger_shortcuts += ("map m" + line[0]
  28. + " shell mv %s " + line[1]
  29. + "\n")
  30. ranger_shortcuts += ("map Y" + line[0]
  31. + " shell cp -r %s " + line[1]
  32. + "\n")
  33. # Adds the zsh_shortcuts shortcuts:
  34. zsh_shortcuts += ("alias " + line[0]
  35. + "=\"cd " + line[1]
  36. + "; echo \$PWD\""
  37. + "\n")
  38. # Goes thru the config file file and adds the shortcuts to both
  39. # zsh_shortcuts and ranger.
  40. with open(home + ".config/Scripts/configs") as conf:
  41. for line in csv.reader(conf, dialect="excel-tab"):
  42. zsh_shortcuts += ("alias " + line[0]
  43. + "=\"vim " + line[1]
  44. + "\""
  45. + "\n")
  46. ranger_shortcuts += ("map " + line[0] + " shell vim " + line[1] + "\n")
  47. def replaceInMarkers(text, shortcuts):
  48. markers = compile(beg+"(.|\s)*"+end)
  49. replacement = beg+shortcuts+end
  50. return sub(markers, replacement, text)
  51. def writeShortcuts(location, shortcuts):
  52. with open(location, "r+") as input:
  53. final = ""
  54. final += input.read()
  55. final = replaceInMarkers(final, shortcuts)
  56. input.seek(0)
  57. input.write(final)
  58. input.truncate()
  59. def main():
  60. writeShortcuts(ranger_location, ranger_shortcuts)
  61. writeShortcuts(zsh_location, zsh_shortcuts)
  62. if __name__ == '__main__':
  63. main()