wd.sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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.6.1
  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:-$WD_BLUE}" # Default to blue if no color is provided
  52. local msg="$2"
  53. if [[ -z "$msg" ]]; then
  54. print "${WD_RED}*${WD_NOC} Could not print message. Sorry!"
  55. else
  56. print " ${color}*${WD_NOC} ${msg}"
  57. fi
  58. fi
  59. }
  60. wd_print_usage()
  61. {
  62. command cat <<- EOF
  63. Usage: wd [command] [point]
  64. Commands:
  65. <point> Warps to the directory specified by the warp point
  66. <point> <path> Warps to the directory specified by the warp point with path appended
  67. add <point> Adds the current working directory to your warp points
  68. add Adds the current working directory to your warp points with current directory's name
  69. rm <point> Removes the given warp point
  70. rm Removes the given warp point with current directory's name
  71. show <point> Print path to given warp point
  72. show Print warp points to current directory
  73. list Print all stored warp points
  74. ls <point> Show files from given warp point (ls)
  75. path <point> Show the path to given warp point (pwd)
  76. clean Remove points warping to nonexistent directories (will prompt unless --force is used)
  77. -v | --version Print version
  78. -d | --debug Exit after execution with exit codes (for testing)
  79. -c | --config Specify config file (default ~/.warprc)
  80. -q | --quiet Suppress all output
  81. -f | --force Allows overwriting without warning (for add & clean)
  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 point=$1
  139. local force=$2
  140. cmdnames=(add rm show list ls path clean help)
  141. if [[ $point == "" ]]
  142. then
  143. point=$(basename "$PWD")
  144. fi
  145. if [[ $point =~ "^[\.]+$" ]]
  146. then
  147. wd_exit_fail "Warp point cannot be just dots"
  148. elif [[ $point =~ "[[:space:]]+" ]]
  149. then
  150. wd_exit_fail "Warp point should not contain whitespace"
  151. elif [[ $point =~ : ]] || [[ $point =~ / ]]
  152. then
  153. wd_exit_fail "Warp point contains illegal character (:/)"
  154. elif (($cmdnames[(Ie)$point]))
  155. then
  156. wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)"
  157. elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ]
  158. then
  159. wd_remove "$point" > /dev/null
  160. printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$WD_CONFIG"
  161. if (whence sort >/dev/null); then
  162. local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
  163. # use 'cat' below to ensure we respect $WD_CONFIG as a symlink
  164. command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" >| "$WD_CONFIG" && command rm "${config_tmp}"
  165. fi
  166. wd_export_static_named_directories
  167. wd_print_msg "$WD_GREEN" "Warp point added"
  168. # override exit code in case wd_remove did not remove any points
  169. # TODO: we should handle this kind of logic better
  170. WD_EXIT_CODE=0
  171. else
  172. wd_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite."
  173. fi
  174. }
  175. wd_remove()
  176. {
  177. local point_list=$1
  178. if [[ "$point_list" == "" ]]
  179. then
  180. point_list=$(basename "$PWD")
  181. fi
  182. for point_name in $point_list ; do
  183. if [[ ${points[$point_name]} != "" ]]
  184. then
  185. local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
  186. # Copy and delete in two steps in order to preserve symlinks
  187. if sed -n "/^${point_name}:.*$/!p" "$WD_CONFIG" >| "$config_tmp" && command cp "$config_tmp" "$WD_CONFIG" && command rm "$config_tmp"
  188. then
  189. wd_print_msg "$WD_GREEN" "Warp point removed"
  190. else
  191. wd_exit_fail "Something bad happened! Sorry."
  192. fi
  193. else
  194. wd_exit_fail "Warp point was not found"
  195. fi
  196. done
  197. }
  198. wd_browse() {
  199. if ! command -v fzf >/dev/null; then
  200. echo "This functionality requires fzf. Please install fzf first."
  201. return 1
  202. fi
  203. local entries=("${(@f)$(sed "s:${HOME}:~:g" "$WD_CONFIG" | awk -F ':' '{print $1 " -> " $2}')}")
  204. local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf --height 40% --reverse)
  205. if [[ -n $selected_entry ]]; then
  206. local selected_point="${selected_entry%% ->*}"
  207. selected_point=$(echo "$selected_point" | xargs)
  208. wd $selected_point
  209. fi
  210. }
  211. wd_list_all()
  212. {
  213. wd_print_msg "$WD_BLUE" "All warp points:"
  214. entries=$(sed "s:${HOME}:~:g" "$WD_CONFIG")
  215. max_warp_point_length=0
  216. while IFS= read -r line
  217. do
  218. arr=(${(s,:,)line})
  219. key=${arr[1]}
  220. length=${#key}
  221. if [[ length -gt max_warp_point_length ]]
  222. then
  223. max_warp_point_length=$length
  224. fi
  225. done <<< "$entries"
  226. while IFS= read -r line
  227. do
  228. if [[ $line != "" ]]
  229. then
  230. arr=(${(s,:,)line})
  231. key=${arr[1]}
  232. val=${line#"${arr[1]}:"}
  233. if [[ -z $wd_quiet_mode ]]
  234. then
  235. printf "%${max_warp_point_length}s -> %s\n" "$key" "$val"
  236. fi
  237. fi
  238. done <<< "$entries"
  239. }
  240. wd_ls()
  241. {
  242. wd_getdir "$1"
  243. ls "${dir/#\~/$HOME}"
  244. }
  245. wd_path()
  246. {
  247. wd_getdir "$1"
  248. echo "$(echo "$dir" | sed "s:~:${HOME}:g")"
  249. }
  250. wd_show()
  251. {
  252. local name_arg=$1
  253. # if there's an argument we look up the value
  254. if [[ -n $name_arg ]]
  255. then
  256. if [[ -z $points[$name_arg] ]]
  257. then
  258. wd_print_msg "$WD_BLUE" "No warp point named $name_arg"
  259. else
  260. wd_print_msg "$WD_GREEN" "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
  261. fi
  262. else
  263. # hax to create a local empty array
  264. local wd_matches
  265. wd_matches=()
  266. # do a reverse lookup to check whether PWD is in $points
  267. PWD="${PWD/$HOME/~}"
  268. if [[ ${points[(r)$PWD]} == "$PWD" ]]
  269. then
  270. for name in ${(k)points}
  271. do
  272. if [[ $points[$name] == "$PWD" ]]
  273. then
  274. wd_matches[$(($#wd_matches+1))]=$name
  275. fi
  276. done
  277. wd_print_msg "$WD_BLUE" "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
  278. else
  279. wd_print_msg "$WD_YELLOW" "No warp point to $(echo "$PWD" | sed "s:$HOME:~:")"
  280. fi
  281. fi
  282. }
  283. wd_clean() {
  284. local force=$1
  285. local count=0
  286. local wd_tmp=""
  287. while read -r line
  288. do
  289. if [[ $line != "" ]]
  290. then
  291. arr=(${(s,:,)line})
  292. key=${arr[1]}
  293. val=${arr[2]}
  294. if [ -d "${val/#\~/$HOME}" ]
  295. then
  296. wd_tmp=$wd_tmp"\n"`echo "$line"`
  297. else
  298. wd_print_msg "$WD_YELLOW" "Nonexistent directory: ${key} -> ${val}"
  299. count=$((count+1))
  300. fi
  301. fi
  302. done < "$WD_CONFIG"
  303. if [[ $count -eq 0 ]]
  304. then
  305. wd_print_msg "$WD_BLUE" "No warp points to clean, carry on!"
  306. else
  307. if [ ! -z "$force" ] || wd_yesorno "Removing ${count} warp points. Continue? (y/n)"
  308. then
  309. echo "$wd_tmp" >! "$WD_CONFIG"
  310. wd_print_msg "$WD_GREEN" "Cleanup complete. ${count} warp point(s) removed"
  311. else
  312. wd_print_msg "$WD_BLUE" "Cleanup aborted"
  313. fi
  314. fi
  315. }
  316. wd_export_static_named_directories() {
  317. if [[ ! -z $WD_EXPORT ]]
  318. then
  319. command grep '^[0-9a-zA-Z_-]\+:' "$WD_CONFIG" | sed -e "s,~,$HOME," -e 's/:/=/' | while read -r warpdir ; do
  320. hash -d "$warpdir"
  321. done
  322. fi
  323. }
  324. local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
  325. local WD_QUIET=0
  326. local WD_EXIT_CODE=0
  327. local WD_DEBUG=0
  328. # Parse 'meta' options first to avoid the need to have them before
  329. # other commands. The `-D` flag consumes recognized options so that
  330. # the actual command parsing won't be affected.
  331. zparseopts -D -E \
  332. c:=wd_alt_config -config:=wd_alt_config \
  333. q=wd_quiet_mode -quiet=wd_quiet_mode \
  334. v=wd_print_version -version=wd_print_version \
  335. d=wd_debug_mode -debug=wd_debug_mode \
  336. f=wd_force_mode -force=wd_force_mode
  337. if [[ ! -z $wd_print_version ]]
  338. then
  339. echo "wd version $WD_VERSION"
  340. fi
  341. if [[ ! -z $wd_alt_config ]]
  342. then
  343. WD_CONFIG=$wd_alt_config[2]
  344. fi
  345. # check if config file exists
  346. if [ ! -e "$WD_CONFIG" ]
  347. then
  348. # if not, create config file
  349. touch "$WD_CONFIG"
  350. else
  351. wd_export_static_named_directories
  352. fi
  353. # disable extendedglob for the complete wd execution time
  354. setopt | grep -q extendedglob
  355. wd_extglob_is_set=$?
  356. if (( wd_extglob_is_set == 0 )); then
  357. setopt noextendedglob
  358. fi
  359. # load warp points
  360. typeset -A points
  361. while read -r line
  362. do
  363. arr=(${(s,:,)line})
  364. key=${arr[1]}
  365. # join the rest, in case the path contains colons
  366. val=${(j,:,)arr[2,-1]}
  367. points[$key]=$val
  368. done < "$WD_CONFIG"
  369. # get opts
  370. args=$(getopt -o a:r:c:lhs -l add:,rm:,clean,list,ls:,path:,help,show -- $*)
  371. # check if no arguments were given, and that version is not set
  372. if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
  373. then
  374. wd_print_usage
  375. # check if config file is writeable
  376. elif [ ! -w "$WD_CONFIG" ]
  377. then
  378. # do nothing
  379. # can't run `exit`, as this would exit the executing shell
  380. wd_exit_fail "\'$WD_CONFIG\' is not writeable."
  381. else
  382. # parse rest of options
  383. local wd_o
  384. for wd_o
  385. do
  386. case "$wd_o"
  387. in
  388. "-a"|"--add"|"add")
  389. wd_add "$2" "$wd_force_mode"
  390. break
  391. ;;
  392. "-b"|"browse")
  393. wd_browse
  394. break
  395. ;;
  396. "-e"|"export")
  397. wd_export_static_named_directories
  398. break
  399. ;;
  400. "-r"|"--remove"|"rm")
  401. # Passes all the arguments as a single string separated by whitespace to wd_remove
  402. wd_remove "${@:2}"
  403. break
  404. ;;
  405. "-l"|"list")
  406. wd_list_all
  407. break
  408. ;;
  409. "-ls"|"ls")
  410. wd_ls "$2"
  411. break
  412. ;;
  413. "-p"|"--path"|"path")
  414. wd_path "$2"
  415. break
  416. ;;
  417. "-h"|"--help"|"help")
  418. wd_print_usage
  419. break
  420. ;;
  421. "-s"|"--show"|"show")
  422. wd_show "$2"
  423. break
  424. ;;
  425. "-c"|"--clean"|"clean")
  426. wd_clean "$wd_force_mode"
  427. break
  428. ;;
  429. *)
  430. wd_warp "$wd_o" "$2"
  431. break
  432. ;;
  433. --)
  434. break
  435. ;;
  436. esac
  437. done
  438. fi
  439. ## garbage collection
  440. # if not, next time warp will pick up variables from this run
  441. # remember, there's no sub shell
  442. if (( wd_extglob_is_set == 0 )); then
  443. setopt extendedglob
  444. fi
  445. unset wd_extglob_is_set
  446. unset wd_warp
  447. unset wd_add
  448. unset wd_remove
  449. unset wd_show
  450. unset wd_list_all
  451. unset wd_print_msg
  452. unset wd_yesorno
  453. unset wd_print_usage
  454. unset wd_alt_config
  455. unset wd_quiet_mode
  456. unset wd_print_version
  457. unset wd_export_static_named_directories
  458. unset wd_o
  459. unset args
  460. unset points
  461. unset val &> /dev/null # fixes issue #1
  462. if [[ -n $wd_debug_mode ]]
  463. then
  464. exit $WD_EXIT_CODE
  465. else
  466. unset wd_debug_mode
  467. fi