fastfile.plugin.zsh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ################################################################################
  2. # FILE: fastfile.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Michael Varner (musikmichael@web.de)
  5. # VERSION: 1.0.0
  6. #
  7. # This plugin adds the ability to on the fly generate and access file shortcuts.
  8. #
  9. ################################################################################
  10. ###########################
  11. # Settings
  12. # These can be overwritten any time.
  13. # If they are not set yet, they will be
  14. # overwritten with their default values
  15. default fastfile_dir "${HOME}/.fastfile/"
  16. default fastfile_var_prefix "§"
  17. ###########################
  18. # Impl
  19. #
  20. # Generate a shortcut
  21. #
  22. # Arguments:
  23. # 1. name - The name of the shortcut (default: name of the file)
  24. # 2. file - The file or directory to make the shortcut for
  25. # STDOUT:
  26. # => fastfle_print
  27. #
  28. function fastfile() {
  29. test "$2" || 2="."
  30. 2=$(readlink -f "$2")
  31. test "$1" || 1="$(basename "$2")"
  32. mkdir -p "${fastfile_dir}"
  33. echo "$2" > "$(fastfile_resolv "$1")"
  34. fastfile_sync
  35. fastfile_print "$1"
  36. }
  37. #
  38. # Resolve the location of a shortcut file (the database file, where the value is written!)
  39. #
  40. # Arguments:
  41. # 1. name - The name of the shortcut
  42. # STDOUT:
  43. # The path
  44. #
  45. function fastfile_resolv() {
  46. echo "${fastfile_dir}${1}"
  47. }
  48. #
  49. # Get the real path of a shortcut
  50. #
  51. # Arguments:
  52. # 1. name - The name of the shortcut
  53. # STDOUT:
  54. # The path
  55. #
  56. function fastfile_get() {
  57. cat "$(fastfile_resolv "$1")"
  58. }
  59. #
  60. # Print a shortcut
  61. #
  62. # Arguments:
  63. # 1. name - The name of the shortcut
  64. # STDOUT:
  65. # Name and value of the shortcut
  66. #
  67. function fastfile_print() {
  68. echo "${fastfile_var_prefix}${1} -> $(fastfile_get "$1")"
  69. }
  70. #
  71. # List all shortcuts
  72. #
  73. # STDOUT:
  74. # (=> fastfle_print) for each shortcut
  75. #
  76. function fastfile_ls() {
  77. for f in $(ls "${fastfile_dir}"); do
  78. fastfile_print "$f"
  79. done | column -t
  80. }
  81. #
  82. # Remove a shortcut
  83. #
  84. # Arguments:
  85. # 1. name - The name of the shortcut (default: name of the file)
  86. # 2. file - The file or directory to make the shortcut for
  87. # STDOUT:
  88. # => fastfle_print
  89. #
  90. function fastfile_rm() {
  91. fastfile_print "$1"
  92. rm "$(fastfile_resolv "$1")"
  93. }
  94. #
  95. # Generate the aliases for the shortcuts
  96. #
  97. function fastfile_sync() {
  98. for f in $(ls "${fastfile_dir}"); do
  99. alias -g "${fastfile_var_prefix}${f}"="$(fastfile_get "$f")"
  100. done
  101. }
  102. ##################################
  103. # Shortcuts
  104. alias ff=fastfile
  105. alias ffp=fastfile_print
  106. alias ffrm=fastfile_rm
  107. alias ffls=fastfile_ls
  108. alias ffsync=fastfile_sync
  109. ##################################
  110. # Init
  111. fastfile_sync