wd.sh 9.6 KB

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