wd.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #!/bin/zsh
  2. # WARP DIRECTORY
  3. # ==============
  4. # Jump to custom directories in terminal
  5. # because `cd` takes too long...
  6. #
  7. # @github.com/mfaerevaag/wd
  8. ## variables
  9. readonly CONFIG=$HOME/.warprc
  10. # colors
  11. readonly BLUE="\033[96m"
  12. readonly GREEN="\033[92m"
  13. readonly YELLOW="\033[93m"
  14. readonly RED="\033[91m"
  15. readonly NOC="\033[m"
  16. ## init
  17. # check if config file exists
  18. if [ ! -e $CONFIG ]
  19. then
  20. # if not, create config file
  21. touch $CONFIG
  22. fi
  23. # load warp points
  24. typeset -A points
  25. while read -r line
  26. do
  27. arr=(${(s,:,)line})
  28. key=${arr[1]}
  29. val=${arr[2]}
  30. points[$key]=$val
  31. done < $CONFIG
  32. ## functions
  33. wd_warp()
  34. {
  35. local point=$1
  36. if [[ $point =~ "^\.+$" ]]
  37. then
  38. if [ $#1 < 2 ]
  39. then
  40. wd_print_msg $YELLOW "Warping to current directory?"
  41. else
  42. (( n = $#1 - 1 ))
  43. cd -$n > /dev/null
  44. fi
  45. elif [[ ${points[$point]} != "" ]]
  46. then
  47. cd ${points[$point]}
  48. else
  49. wd_print_msg $RED "Unknown warp point '${point}'"
  50. fi
  51. }
  52. wd_add()
  53. {
  54. local force=$1
  55. local point=$2
  56. if [[ $point =~ "^[\.]+$" ]]
  57. then
  58. wd_print_msg $RED "Warp point cannot be just dots"
  59. elif [[ $point =~ "(\s|\ )+" ]]
  60. then
  61. wd_print_msg $RED "Warp point should not contain whitespace"
  62. elif [[ $point == *:* ]]
  63. then
  64. wd_print_msg $RED "Warp point cannot contain colons"
  65. elif [[ $point == "" ]]
  66. then
  67. wd_print_msg $RED "Warp point cannot be empty"
  68. elif [[ ${points[$2]} == "" ]] || $force
  69. then
  70. wd_remove $point > /dev/null
  71. printf "%q:%q\n" "${point}" "${PWD}" >> $CONFIG
  72. wd_print_msg $GREEN "Warp point added"
  73. else
  74. wd_print_msg $YELLOW "Warp point '${point}' already exists. Use 'add!' to overwrite."
  75. fi
  76. }
  77. wd_remove()
  78. {
  79. local point=$1
  80. if [[ ${points[$point]} != "" ]]
  81. then
  82. if sed -i.bak "s,^${point}:.*$,,g" $CONFIG
  83. then
  84. wd_print_msg $GREEN "Warp point removed"
  85. else
  86. wd_print_msg $RED "Something bad happened! Sorry."
  87. fi
  88. else
  89. wd_print_msg $RED "Warp point was not found"
  90. fi
  91. }
  92. wd_list_all()
  93. {
  94. wd_print_msg $BLUE "All warp points:"
  95. while IFS= read -r line
  96. do
  97. if [[ $line != "" ]]
  98. then
  99. arr=(${(s,:,)line})
  100. key=${arr[1]}
  101. val=${arr[2]}
  102. printf "%20s -> %s\n" $key $val
  103. fi
  104. done <<< $(sed "s:${HOME}:~:g" $CONFIG)
  105. }
  106. wd_show()
  107. {
  108. local cwd=$(print $PWD | sed "s:^${HOME}:~:")
  109. wd_print_msg $BLUE "Warp points to current directory:"
  110. wd_list_all | grep -e "${cwd}$"
  111. }
  112. wd_print_msg()
  113. {
  114. local color=$1
  115. local msg=$2
  116. if [[ $color == "" || $msg == "" ]]
  117. then
  118. print " ${RED}*${NOC} Could not print message. Sorry!"
  119. else
  120. print " ${color}*${NOC} ${msg}"
  121. fi
  122. }
  123. wd_print_usage()
  124. {
  125. cat <<- EOF
  126. Usage: wd [add|-a|--add] [rm|-r|--remove] <point>
  127. Commands:
  128. add Adds the current working directory to your warp points
  129. add! Overwrites existing warp point
  130. rm Removes the given warp point
  131. show Outputs warp points to current directory
  132. ls Outputs all stored warp points
  133. help Show this extremely helpful text
  134. EOF
  135. }
  136. ## run
  137. # get opts
  138. args=$(getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*)
  139. # check if no arguments were given
  140. if [[ $? -ne 0 || $#* -eq 0 ]]
  141. then
  142. wd_print_usage
  143. # check if config file is writeable
  144. elif [ ! -w $CONFIG ]
  145. then
  146. # do nothing
  147. # can't run `exit`, as this would exit the executing shell
  148. wd_print_msg $RED "\'$CONFIG\' is not writeable."
  149. else
  150. for o
  151. do
  152. case "$o"
  153. in
  154. -a|--add|add)
  155. wd_add false $2
  156. break
  157. ;;
  158. -a!|--add!|add!)
  159. wd_add true $2
  160. break
  161. ;;
  162. -r|--remove|rm)
  163. wd_remove $2
  164. break
  165. ;;
  166. -l|--list|ls)
  167. wd_list_all
  168. break
  169. ;;
  170. -h|--help|help)
  171. wd_print_usage
  172. break
  173. ;;
  174. -s|--show|show)
  175. wd_show
  176. break
  177. ;;
  178. *)
  179. wd_warp $o
  180. break
  181. ;;
  182. --)
  183. break
  184. ;;
  185. esac
  186. done
  187. fi
  188. ## garbage collection
  189. # if not, next time warp will pick up variables from this run
  190. # remember, there's no sub shell
  191. unset wd_warp
  192. unset wd_add
  193. unset wd_remove
  194. unset wd_show
  195. unset wd_list_all
  196. unset wd_print_msg
  197. unset wd_print_usage
  198. unset args
  199. unset points
  200. unset val &> /dev/null # fixes issue #1