fastfile.plugin.zsh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. file=$(readlink -f "$2")
  31. test "$1" || 1="$(basename "$file")"
  32. name=$(echo "$1" | tr " " "_")
  33. mkdir -p "${fastfile_dir}"
  34. echo "$file" > "$(fastfile_resolv "$name")"
  35. fastfile_sync
  36. fastfile_print "$name"
  37. }
  38. #
  39. # Resolve the location of a shortcut file (the database file, where the value is written!)
  40. #
  41. # Arguments:
  42. # 1. name - The name of the shortcut
  43. # STDOUT:
  44. # The path
  45. #
  46. function fastfile_resolv() {
  47. echo "${fastfile_dir}${1}"
  48. }
  49. #
  50. # Get the real path of a shortcut
  51. #
  52. # Arguments:
  53. # 1. name - The name of the shortcut
  54. # STDOUT:
  55. # The path
  56. #
  57. function fastfile_get() {
  58. cat "$(fastfile_resolv "$1")"
  59. }
  60. #
  61. # Print a shortcut
  62. #
  63. # Arguments:
  64. # 1. name - The name of the shortcut
  65. # STDOUT:
  66. # Name and value of the shortcut
  67. #
  68. function fastfile_print() {
  69. echo "${fastfile_var_prefix}${1} -> $(fastfile_get "$1")"
  70. }
  71. #
  72. # List all shortcuts
  73. #
  74. # STDOUT:
  75. # (=> fastfle_print) for each shortcut
  76. #
  77. function fastfile_ls() {
  78. for f in "${fastfile_dir}"/*; do
  79. file=`basename "$f"` # To enable simpler handeling of spaces in file names
  80. varkey=`echo "$file" | tr " " "_"`
  81. # Special format for colums
  82. echo "${fastfile_var_prefix}${varkey}|->|$(fastfile_get "$file")"
  83. done | column -t -s "|"
  84. }
  85. #
  86. # Remove a shortcut
  87. #
  88. # Arguments:
  89. # 1. name - The name of the shortcut (default: name of the file)
  90. # 2. file - The file or directory to make the shortcut for
  91. # STDOUT:
  92. # => fastfle_print
  93. #
  94. function fastfile_rm() {
  95. fastfile_print "$1"
  96. rm "$(fastfile_resolv "$1")"
  97. }
  98. #
  99. # Generate the aliases for the shortcuts
  100. #
  101. function fastfile_sync() {
  102. for f in "${fastfile_dir}"/*; do
  103. file=`basename "$f"` # To enable simpler handeling of spaces in file names
  104. varkey=`echo "$file" | tr " " "_"`
  105. alias -g "${fastfile_var_prefix}${varkey}"="'$(fastfile_get "$file")'"
  106. done
  107. }
  108. ##################################
  109. # Shortcuts
  110. alias ff=fastfile
  111. alias ffp=fastfile_print
  112. alias ffrm=fastfile_rm
  113. alias ffls=fastfile_ls
  114. alias ffsync=fastfile_sync
  115. ##################################
  116. # Init
  117. fastfile_sync