wd.sh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. # version
  9. readonly WD_VERSION=0.4
  10. # colors
  11. readonly WD_BLUE="\033[96m"
  12. readonly WD_GREEN="\033[92m"
  13. readonly WD_YELLOW="\033[93m"
  14. readonly WD_RED="\033[91m"
  15. readonly WD_NOC="\033[m"
  16. ## functions
  17. # helpers
  18. wd_yesorno()
  19. {
  20. # variables
  21. local question="${1}"
  22. local prompt="${question} "
  23. local yes_RETVAL="0"
  24. local no_RETVAL="3"
  25. local RETVAL=""
  26. local answer=""
  27. # read-eval loop
  28. while true ; do
  29. printf $prompt
  30. read -r answer
  31. case ${answer:=${default}} in
  32. Y|y|YES|yes|Yes )
  33. RETVAL=${yes_RETVAL} && \
  34. break
  35. ;;
  36. N|n|NO|no|No )
  37. RETVAL=${no_RETVAL} && \
  38. break
  39. ;;
  40. * )
  41. echo "Please provide a valid answer (y or n)"
  42. ;;
  43. esac
  44. done
  45. return ${RETVAL}
  46. }
  47. wd_print_msg()
  48. {
  49. if [[ -z $wd_quiet_mode ]]
  50. then
  51. local color=$1
  52. local msg=$2
  53. if [[ $color == "" || $msg == "" ]]
  54. then
  55. print " ${WD_RED}*${WD_NOC} Could not print message. Sorry!"
  56. else
  57. print " ${color}*${WD_NOC} ${msg}"
  58. fi
  59. fi
  60. }
  61. wd_print_usage()
  62. {
  63. cat <<- EOF
  64. Usage: wd [command] <point>
  65. Commands:
  66. add <point> Adds the current working directory to your warp points
  67. add! <point> Overwrites existing warp point
  68. rm <point> Removes the given warp point
  69. show Print warp points to current directory
  70. show <point> Print path to given warp point
  71. ls Print all stored warp points
  72. clean! Remove points warping to nonexistent directories
  73. -v | --version Print version
  74. -d | --debug Exit after execution with exit codes (for testing)
  75. -c | --config Specify config file (default ~/.warprc)
  76. -q | --quiet Suppress all output
  77. help Show this extremely helpful text
  78. EOF
  79. }
  80. wd_exit_fail()
  81. {
  82. local msg=$1
  83. wd_print_msg $WD_RED $1
  84. WD_EXIT_CODE=1
  85. }
  86. wd_exit_warn()
  87. {
  88. local msg=$1
  89. wd_print_msg $WD_YELLOW $msg
  90. WD_EXIT_CODE=1
  91. }
  92. # core
  93. wd_warp()
  94. {
  95. local point=$1
  96. if [[ $point =~ "^\.+$" ]]
  97. then
  98. if [ $#1 < 2 ]
  99. then
  100. wd_exit_warn "Warping to current directory?"
  101. else
  102. (( n = $#1 - 1 ))
  103. cd -$n > /dev/null
  104. fi
  105. elif [[ ${points[$point]} != "" ]]
  106. then
  107. cd ${points[$point]}
  108. else
  109. wd_exit_fail "Unknown warp point '${point}'"
  110. fi
  111. }
  112. wd_add()
  113. {
  114. local force=$1
  115. local point=$2
  116. if [[ $point =~ "^[\.]+$" ]]
  117. then
  118. wd_exit_fail "Warp point cannot be just dots"
  119. elif [[ $point =~ "(\s|\ )+" ]]
  120. then
  121. wd_exit_fail "Warp point should not contain whitespace"
  122. elif [[ $point == *:* ]]
  123. then
  124. wd_exit_fail "Warp point cannot contain colons"
  125. elif [[ $point == "" ]]
  126. then
  127. wd_exit_fail "Warp point cannot be empty"
  128. elif [[ ${points[$2]} == "" ]] || $force
  129. then
  130. wd_remove $point > /dev/null
  131. printf "%q:%q\n" "${point}" "${PWD}" >> $WD_CONFIG
  132. wd_print_msg $WD_GREEN "Warp point added"
  133. # override exit code in case wd_remove did not remove any points
  134. # TODO: we should handle this kind of logic better
  135. WD_EXIT_CODE=0
  136. else
  137. wd_exit_warn "Warp point '${point}' already exists. Use 'add!' to overwrite."
  138. fi
  139. }
  140. wd_remove()
  141. {
  142. local point=$1
  143. if [[ ${points[$point]} != "" ]]
  144. then
  145. local config_tmp=$WD_CONFIG.tmp
  146. if sed -n "/^${point}:.*$/!p" $WD_CONFIG > $config_tmp && mv $config_tmp $WD_CONFIG
  147. then
  148. wd_print_msg $WD_GREEN "Warp point removed"
  149. else
  150. wd_exit_fail "Something bad happened! Sorry."
  151. fi
  152. else
  153. wd_exit_fail "Warp point was not found"
  154. fi
  155. }
  156. wd_list_all()
  157. {
  158. wd_print_msg $WD_BLUE "All warp points:"
  159. while IFS= read -r line
  160. do
  161. if [[ $line != "" ]]
  162. then
  163. arr=(${(s,:,)line})
  164. key=${arr[1]}
  165. val=${arr[2]}
  166. if [[ -z $wd_quiet_mode ]]
  167. then
  168. printf "%20s -> %s\n" $key $val
  169. fi
  170. fi
  171. done <<< $(sed "s:${HOME}:~:g" $WD_CONFIG)
  172. }
  173. wd_show()
  174. {
  175. local name_arg=$1
  176. # if there's an argument we look up the value
  177. if [[ ! -z $name_arg ]]
  178. then
  179. if [[ -z $points[$name_arg] ]]
  180. then
  181. wd_print_msg $WD_BLUE "No warp point named $name_arg"
  182. else
  183. wd_print_msg $WD_GREEN "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
  184. fi
  185. else
  186. # hax to create a local empty array
  187. local wd_matches
  188. wd_matches=()
  189. # do a reverse lookup to check whether PWD is in $points
  190. if [[ ${points[(r)$PWD]} == $PWD ]]
  191. then
  192. for name in ${(k)points}
  193. do
  194. if [[ $points[$name] == $PWD ]]
  195. then
  196. wd_matches[$(($#wd_matches+1))]=$name
  197. fi
  198. done
  199. wd_print_msg $WD_BLUE "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
  200. else
  201. wd_print_msg $WD_YELLOW "No warp point to $(echo $PWD | sed "s:$HOME:~:")"
  202. fi
  203. fi
  204. }
  205. wd_clean() {
  206. local force=$1
  207. local count=0
  208. local wd_tmp=""
  209. while read line
  210. do
  211. if [[ $line != "" ]]
  212. then
  213. arr=(${(s,:,)line})
  214. key=${arr[1]}
  215. val=${arr[2]}
  216. if [ -d "$val" ]
  217. then
  218. wd_tmp=$wd_tmp"\n"`echo $line`
  219. else
  220. wd_print_msg $WD_YELLOW "Nonexistent directory: ${key} -> ${val}"
  221. count=$((count+1))
  222. fi
  223. fi
  224. done < $WD_CONFIG
  225. if [[ $count -eq 0 ]]
  226. then
  227. wd_print_msg $WD_BLUE "No warp points to clean, carry on!"
  228. else
  229. if $force || wd_yesorno "Removing ${count} warp points. Continue? (Y/n)"
  230. then
  231. echo $wd_tmp >! $WD_CONFIG
  232. wd_print_msg $WD_GREEN "Cleanup complete. ${count} warp point(s) removed"
  233. else
  234. wd_print_msg $WD_BLUE "Cleanup aborted"
  235. fi
  236. fi
  237. }
  238. local WD_CONFIG=$HOME/.warprc
  239. local WD_QUIET=0
  240. local WD_EXIT_CODE=0
  241. local WD_DEBUG=0
  242. # Parse 'meta' options first to avoid the need to have them before
  243. # other commands. The `-D` flag consumes recognized options so that
  244. # the actual command parsing won't be affected.
  245. zparseopts -D -E \
  246. c:=wd_alt_config -config:=wd_alt_config \
  247. q=wd_quiet_mode -quiet=wd_quiet_mode \
  248. v=wd_print_version -version=wd_print_version \
  249. d=wd_debug_mode -debug=wd_debug_mode
  250. if [[ ! -z $wd_print_version ]]
  251. then
  252. echo "wd version $WD_VERSION"
  253. fi
  254. if [[ ! -z $wd_alt_config ]]
  255. then
  256. WD_CONFIG=$wd_alt_config[2]
  257. fi
  258. # check if config file exists
  259. if [ ! -e $WD_CONFIG ]
  260. then
  261. # if not, create config file
  262. touch $WD_CONFIG
  263. fi
  264. # load warp points
  265. typeset -A points
  266. while read -r line
  267. do
  268. arr=(${(s,:,)line})
  269. key=${arr[1]}
  270. val=${arr[2]}
  271. points[$key]=$val
  272. done < $WD_CONFIG
  273. # get opts
  274. args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,ls,help,show -- $*)
  275. # check if no arguments were given, and that version is not set
  276. if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
  277. then
  278. wd_print_usage
  279. # check if config file is writeable
  280. elif [ ! -w $WD_CONFIG ]
  281. then
  282. # do nothing
  283. # can't run `exit`, as this would exit the executing shell
  284. wd_exit_fail "\'$WD_CONFIG\' is not writeable."
  285. else
  286. # parse rest of options
  287. for o
  288. do
  289. case "$o"
  290. in
  291. -a|--add|add)
  292. wd_add false $2
  293. break
  294. ;;
  295. -a!|--add!|add!)
  296. wd_add true $2
  297. break
  298. ;;
  299. -r|--remove|rm)
  300. wd_remove $2
  301. break
  302. ;;
  303. -l|--list|ls)
  304. wd_list_all
  305. break
  306. ;;
  307. -h|--help|help)
  308. wd_print_usage
  309. break
  310. ;;
  311. -s|--show|show)
  312. wd_show $2
  313. break
  314. ;;
  315. -c|--clean|clean)
  316. wd_clean false
  317. break
  318. ;;
  319. -c!|--clean!|clean!)
  320. wd_clean true
  321. break
  322. ;;
  323. *)
  324. wd_warp $o
  325. break
  326. ;;
  327. --)
  328. break
  329. ;;
  330. esac
  331. done
  332. fi
  333. ## garbage collection
  334. # if not, next time warp will pick up variables from this run
  335. # remember, there's no sub shell
  336. unset wd_warp
  337. unset wd_add
  338. unset wd_remove
  339. unset wd_show
  340. unset wd_list_all
  341. unset wd_print_msg
  342. unset wd_yesorno
  343. unset wd_print_usage
  344. unset wd_alt_config
  345. unset wd_quiet_mode
  346. unset wd_print_version
  347. unset args
  348. unset points
  349. unset val &> /dev/null # fixes issue #1
  350. if [[ ! -z $wd_debug_mode ]]
  351. then
  352. exit $WD_EXIT_CODE
  353. else
  354. unset wd_debug_mode
  355. fi