wd.sh 11 KB

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