wd.sh 12 KB

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