wd.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.2
  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]/#\~/$HOME}
  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/#$HOME/~}" >> $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. entries=$(sed "s:${HOME}:~:g" $WD_CONFIG)
  175. max_warp_point_length=0
  176. while IFS= read -r line
  177. do
  178. arr=(${(s,:,)line})
  179. key=${arr[1]}
  180. length=${#key}
  181. if [[ length -gt max_warp_point_length ]]
  182. then
  183. max_warp_point_length=$length
  184. fi
  185. done <<< $entries
  186. while IFS= read -r line
  187. do
  188. if [[ $line != "" ]]
  189. then
  190. arr=(${(s,:,)line})
  191. key=${arr[1]}
  192. val=${arr[2]}
  193. if [[ -z $wd_quiet_mode ]]
  194. then
  195. printf "%${max_warp_point_length}s -> %s\n" $key $val
  196. fi
  197. fi
  198. done <<< $entries
  199. }
  200. wd_ls()
  201. {
  202. wd_getdir $1
  203. ls ${dir/#\~/$HOME}
  204. }
  205. wd_path()
  206. {
  207. wd_getdir $1
  208. echo $(echo $dir | sed "s:${HOME}:~:g")
  209. }
  210. wd_show()
  211. {
  212. local name_arg=$1
  213. # if there's an argument we look up the value
  214. if [[ ! -z $name_arg ]]
  215. then
  216. if [[ -z $points[$name_arg] ]]
  217. then
  218. wd_print_msg $WD_BLUE "No warp point named $name_arg"
  219. else
  220. wd_print_msg $WD_GREEN "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
  221. fi
  222. else
  223. # hax to create a local empty array
  224. local wd_matches
  225. wd_matches=()
  226. # do a reverse lookup to check whether PWD is in $points
  227. PWD="${PWD/$HOME/~}"
  228. if [[ ${points[(r)$PWD]} == $PWD ]]
  229. then
  230. for name in ${(k)points}
  231. do
  232. if [[ $points[$name] == $PWD ]]
  233. then
  234. wd_matches[$(($#wd_matches+1))]=$name
  235. fi
  236. done
  237. wd_print_msg $WD_BLUE "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
  238. else
  239. wd_print_msg $WD_YELLOW "No warp point to $(echo $PWD | sed "s:$HOME:~:")"
  240. fi
  241. fi
  242. }
  243. wd_clean() {
  244. local force=$1
  245. local count=0
  246. local wd_tmp=""
  247. while read line
  248. do
  249. if [[ $line != "" ]]
  250. then
  251. arr=(${(s,:,)line})
  252. key=${arr[1]}
  253. val=${arr[2]}
  254. if [ -d "$val" ]
  255. then
  256. wd_tmp=$wd_tmp"\n"`echo $line`
  257. else
  258. wd_print_msg $WD_YELLOW "Nonexistent directory: ${key} -> ${val}"
  259. count=$((count+1))
  260. fi
  261. fi
  262. done < $WD_CONFIG
  263. if [[ $count -eq 0 ]]
  264. then
  265. wd_print_msg $WD_BLUE "No warp points to clean, carry on!"
  266. else
  267. if $force || wd_yesorno "Removing ${count} warp points. Continue? (Y/n)"
  268. then
  269. echo $wd_tmp >! $WD_CONFIG
  270. wd_print_msg $WD_GREEN "Cleanup complete. ${count} warp point(s) removed"
  271. else
  272. wd_print_msg $WD_BLUE "Cleanup aborted"
  273. fi
  274. fi
  275. }
  276. local WD_CONFIG=$HOME/.warprc
  277. local WD_QUIET=0
  278. local WD_EXIT_CODE=0
  279. local WD_DEBUG=0
  280. # Parse 'meta' options first to avoid the need to have them before
  281. # other commands. The `-D` flag consumes recognized options so that
  282. # the actual command parsing won't be affected.
  283. zparseopts -D -E \
  284. c:=wd_alt_config -config:=wd_alt_config \
  285. q=wd_quiet_mode -quiet=wd_quiet_mode \
  286. v=wd_print_version -version=wd_print_version \
  287. d=wd_debug_mode -debug=wd_debug_mode
  288. if [[ ! -z $wd_print_version ]]
  289. then
  290. echo "wd version $WD_VERSION"
  291. fi
  292. if [[ ! -z $wd_alt_config ]]
  293. then
  294. WD_CONFIG=$wd_alt_config[2]
  295. fi
  296. # check if config file exists
  297. if [ ! -e $WD_CONFIG ]
  298. then
  299. # if not, create config file
  300. touch $WD_CONFIG
  301. fi
  302. # load warp points
  303. typeset -A points
  304. while read -r line
  305. do
  306. arr=(${(s,:,)line})
  307. key=${arr[1]}
  308. val=${arr[2]}
  309. points[$key]=$val
  310. done < $WD_CONFIG
  311. # get opts
  312. args=$(getopt -o a:r:c:lhs -l add:,rm:,clean\!,list,ls:,path:,help,show -- $*)
  313. # check if no arguments were given, and that version is not set
  314. if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
  315. then
  316. wd_print_usage
  317. # check if config file is writeable
  318. elif [ ! -w $WD_CONFIG ]
  319. then
  320. # do nothing
  321. # can't run `exit`, as this would exit the executing shell
  322. wd_exit_fail "\'$WD_CONFIG\' is not writeable."
  323. else
  324. # parse rest of options
  325. for o
  326. do
  327. case "$o"
  328. in
  329. -a|--add|add)
  330. wd_add false $2
  331. break
  332. ;;
  333. -a!|--add!|add!)
  334. wd_add true $2
  335. break
  336. ;;
  337. -r|--remove|rm)
  338. wd_remove $2
  339. break
  340. ;;
  341. -l|list)
  342. wd_list_all
  343. break
  344. ;;
  345. -ls|ls)
  346. wd_ls $2
  347. break
  348. ;;
  349. -p|--path|path)
  350. wd_path $2
  351. break
  352. ;;
  353. -h|--help|help)
  354. wd_print_usage
  355. break
  356. ;;
  357. -s|--show|show)
  358. wd_show $2
  359. break
  360. ;;
  361. -c|--clean|clean)
  362. wd_clean false
  363. break
  364. ;;
  365. -c!|--clean!|clean!)
  366. wd_clean true
  367. break
  368. ;;
  369. *)
  370. wd_warp $o
  371. break
  372. ;;
  373. --)
  374. break
  375. ;;
  376. esac
  377. done
  378. fi
  379. ## garbage collection
  380. # if not, next time warp will pick up variables from this run
  381. # remember, there's no sub shell
  382. unset wd_warp
  383. unset wd_add
  384. unset wd_remove
  385. unset wd_show
  386. unset wd_list_all
  387. unset wd_print_msg
  388. unset wd_yesorno
  389. unset wd_print_usage
  390. unset wd_alt_config
  391. unset wd_quiet_mode
  392. unset wd_print_version
  393. unset args
  394. unset points
  395. unset val &> /dev/null # fixes issue #1
  396. if [[ ! -z $wd_debug_mode ]]
  397. then
  398. exit $WD_EXIT_CODE
  399. else
  400. unset wd_debug_mode
  401. fi