fastfile.plugin.zsh 2.6 KB

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