wd.sh 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. #!/usr/bin/env 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.9.3
  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. addcd <path> Adds a path to your warp points with the directory's name
  70. addcd <path> <point> Adds a path to your warp points with a custom name
  71. rm <point> Removes the given warp point
  72. rm Removes the given warp point with current directory's name
  73. show <point> Print path to given warp point
  74. show Print warp points to current directory
  75. list Print all stored warp points
  76. ls <point> Show files from given warp point (ls)
  77. path <point> Show the path to given warp point (pwd)
  78. clean Remove points warping to nonexistent directories (will prompt unless --force is used)
  79. -v | --version Print version
  80. -c | --config Specify config file (default ~/.warprc)
  81. -q | --quiet Suppress all output
  82. -f | --force Allows overwriting without warning (for add & clean)
  83. help Show this extremely helpful text
  84. EOF
  85. }
  86. wd_exit_fail()
  87. {
  88. local msg=$1
  89. wd_print_msg "$WD_RED" "$msg"
  90. WD_EXIT_CODE=1
  91. }
  92. wd_exit_warn()
  93. {
  94. local msg=$1
  95. wd_print_msg "$WD_YELLOW" "$msg"
  96. WD_EXIT_CODE=1
  97. }
  98. wd_getdir()
  99. {
  100. local name_arg=$1
  101. point=$(wd_show "$name_arg")
  102. dir=${point:28+$#name_arg+7}
  103. if [[ -z $name_arg ]]; then
  104. wd_exit_fail "You must enter a warp point"
  105. break
  106. elif [[ -z $dir ]]; then
  107. wd_exit_fail "Unknown warp point '${name_arg}'"
  108. break
  109. fi
  110. }
  111. # core
  112. wd_warp()
  113. {
  114. local point=$1
  115. local sub=$2
  116. if [[ $point =~ "^\.+$" ]]
  117. then
  118. if [[ $#1 < 2 ]]
  119. then
  120. wd_exit_warn "Warping to current directory?"
  121. else
  122. (( n = $#1 - 1 ))
  123. cd -$n > /dev/null
  124. WD_EXIT_CODE=$?
  125. fi
  126. elif [[ ${points[$point]} != "" ]]
  127. then
  128. if [[ $sub != "" ]]
  129. then
  130. cd ${points[$point]/#\~/$HOME}/$sub
  131. WD_EXIT_CODE=$?
  132. else
  133. cd ${points[$point]/#\~/$HOME}
  134. WD_EXIT_CODE=$?
  135. fi
  136. else
  137. wd_exit_fail "Unknown warp point '${point}'"
  138. fi
  139. }
  140. wd_add()
  141. {
  142. local point=$1
  143. local force=$2
  144. cmdnames=(add rm show list ls path clean help)
  145. if [[ $point == "" ]]
  146. then
  147. point=$(basename "$PWD")
  148. fi
  149. if [[ $point =~ "^[\.]+$" ]]
  150. then
  151. wd_exit_fail "Warp point cannot be just dots"
  152. elif [[ $point =~ "[[:space:]]+" ]]
  153. then
  154. wd_exit_fail "Warp point should not contain whitespace"
  155. elif [[ $point =~ : ]] || [[ $point =~ / ]]
  156. then
  157. wd_exit_fail "Warp point contains illegal character (:/)"
  158. elif (($cmdnames[(Ie)$point]))
  159. then
  160. wd_exit_fail "Warp point name cannot be a wd command (see wd -h for a full list)"
  161. elif [[ ${points[$point]} == "" ]] || [ ! -z "$force" ]
  162. then
  163. wd_remove "$point" > /dev/null
  164. printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$wd_config_file"
  165. if (whence sort >/dev/null); then
  166. local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
  167. # use 'cat' below to ensure we respect $wd_config_file as a symlink
  168. command sort -o "${config_tmp}" "$wd_config_file" && command cat "${config_tmp}" >| "$wd_config_file" && command rm "${config_tmp}"
  169. fi
  170. wd_export_static_named_directories
  171. wd_print_msg "$WD_GREEN" "Warp point added"
  172. # override exit code in case wd_remove did not remove any points
  173. # TODO: we should handle this kind of logic better
  174. WD_EXIT_CODE=0
  175. else
  176. wd_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite."
  177. fi
  178. }
  179. wd_addcd() {
  180. local folder="$1"
  181. local point=$2
  182. local force=$3
  183. local currentdir=$PWD
  184. if [[ -z "$folder" ]]; then
  185. wd_exit_fail "You must specify a path"
  186. return
  187. fi
  188. if [[ ! -d "$folder" ]]; then
  189. wd_exit_fail "The directory does not exist"
  190. return
  191. fi
  192. cd "$folder" || return
  193. wd_add "$point" "$force"
  194. cd "$currentdir" || return
  195. }
  196. wd_remove()
  197. {
  198. local point_list=$1
  199. if [[ "$point_list" == "" ]]
  200. then
  201. point_list=$(basename "$PWD")
  202. fi
  203. for point_name in $point_list ; do
  204. if [[ ${points[$point_name]} != "" ]]
  205. then
  206. local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
  207. # Copy and delete in two steps in order to preserve symlinks
  208. if sed -n "/^${point_name}:.*$/!p" "$wd_config_file" >| "$config_tmp" && command cp "$config_tmp" "$wd_config_file" && command rm "$config_tmp"
  209. then
  210. wd_print_msg "$WD_GREEN" "Warp point removed"
  211. else
  212. wd_exit_fail "Something bad happened! Sorry."
  213. fi
  214. else
  215. wd_exit_fail "Warp point was not found"
  216. fi
  217. done
  218. }
  219. wd_browse() {
  220. # Check if fzf is installed
  221. if ! command -v fzf >/dev/null; then
  222. wd_print_msg "$WD_RED" "This functionality requires fzf. Please install fzf first."
  223. return 1
  224. fi
  225. # Ensure wd_config_file is properly set
  226. if [[ -z $wd_config_file ]]; then
  227. wd_config_file="${WD_CONFIG:-$HOME/.warprc}"
  228. fi
  229. # Check if config file exists
  230. if [[ ! -f $wd_config_file ]]; then
  231. wd_print_msg "$WD_RED" "Config file $wd_config_file does not exist. Please create it first."
  232. return 1
  233. fi
  234. # Read entries from the config file
  235. local entries=("${(@f)$(sed "s:${HOME}:~:g" "$wd_config_file" | awk -F ':' '{print $1 " -> " $2}')}")
  236. if [[ -z $entries ]]; then
  237. wd_print_msg "$WD_YELLOW" "You don't have any warp points to browse"
  238. return 1
  239. fi
  240. # Temp file for remove operations
  241. local script_path="${${(%):-%x}:h}"
  242. local wd_remove_output=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
  243. # Create fzf bindings
  244. entries=("All warp points:" "Press enter to select. Press delete to remove" "${entries[@]}")
  245. local fzf_bind="delete:execute(echo {} | awk -F ' -> ' '{print \$1}' | xargs -I {} \"$script_path/wd.sh\" rm {} > \"$wd_remove_output\")+abort"
  246. # Run fzf
  247. local selected_entry=$(printf '%s\n' "${entries[@]}" | fzf --height 100% --reverse --header-lines=2 --bind="$fzf_bind")
  248. # Handle selection
  249. if [[ -e $wd_remove_output ]]; then
  250. cat "$wd_remove_output"
  251. rm -f "$wd_remove_output"
  252. fi
  253. if [[ -n $selected_entry ]]; then
  254. local selected_point="${selected_entry%% ->*}"
  255. selected_point=$(echo "$selected_point" | xargs)
  256. wd $selected_point
  257. fi
  258. }
  259. wd_browse_widget() {
  260. # Ensure wd_config_file is properly set
  261. if [[ -z $wd_config_file ]]; then
  262. wd_config_file="${WD_CONFIG:-$HOME/.warprc}"
  263. fi
  264. # Check if config file exists
  265. if [[ ! -f $wd_config_file ]]; then
  266. wd_print_msg "$WD_RED" "Config file $wd_config_file does not exist. Please create it first."
  267. return 1
  268. fi
  269. # Call wd_browse to handle the selection
  270. wd_browse
  271. # Restore the zsh buffer and cursor after running wd_browse
  272. saved_buffer=$BUFFER
  273. saved_cursor=$CURSOR
  274. BUFFER=
  275. zle redisplay
  276. zle accept-line
  277. }
  278. wd_restore_buffer() {
  279. if [[ -n $saved_buffer ]]; then
  280. BUFFER=$saved_buffer
  281. CURSOR=$saved_cursor
  282. fi
  283. saved_buffer=
  284. saved_cursor=1
  285. }
  286. wd_list_all()
  287. {
  288. wd_print_msg "$WD_BLUE" "All warp points:"
  289. entries=$(sed "s:${HOME}:~:g" "$wd_config_file")
  290. max_warp_point_length=0
  291. while IFS= read -r line
  292. do
  293. arr=(${(s,:,)line})
  294. key=${arr[1]}
  295. length=${#key}
  296. if [[ length -gt max_warp_point_length ]]
  297. then
  298. max_warp_point_length=$length
  299. fi
  300. done <<< "$entries"
  301. while IFS= read -r line
  302. do
  303. if [[ $line != "" ]]
  304. then
  305. arr=(${(s,:,)line})
  306. key=${arr[1]}
  307. val=${line#"${arr[1]}:"}
  308. if [[ -z $wd_quiet_mode ]]
  309. then
  310. printf "%${max_warp_point_length}s -> %s\n" "$key" "$val"
  311. fi
  312. fi
  313. done <<< "$entries"
  314. }
  315. wd_ls()
  316. {
  317. wd_getdir "$1"
  318. ls "${dir/#\~/$HOME}"
  319. }
  320. wd_path()
  321. {
  322. wd_getdir "$1"
  323. echo "$(echo "$dir" | sed "s:~:${HOME}:g")"
  324. }
  325. wd_show()
  326. {
  327. local name_arg=$1
  328. local show_pwd
  329. # if there's an argument we look up the value
  330. if [[ -n $name_arg ]]
  331. then
  332. if [[ -z $points[$name_arg] ]]
  333. then
  334. wd_print_msg "$WD_BLUE" "No warp point named $name_arg"
  335. else
  336. wd_print_msg "$WD_GREEN" "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
  337. fi
  338. else
  339. # hax to create a local empty array
  340. local wd_matches
  341. wd_matches=()
  342. # do a reverse lookup to check whether PWD is in $points
  343. show_pwd="${PWD/$HOME/~}"
  344. if [[ ${points[(r)$show_pwd]} == "$show_pwd" ]]
  345. then
  346. for name in ${(k)points}
  347. do
  348. if [[ $points[$name] == "$show_pwd" ]]
  349. then
  350. wd_matches[$(($#wd_matches+1))]=$name
  351. fi
  352. done
  353. wd_print_msg "$WD_BLUE" "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
  354. else
  355. wd_print_msg "$WD_YELLOW" "No warp point to $show_pwd"
  356. fi
  357. fi
  358. }
  359. wd_clean() {
  360. local force=$1
  361. local count=0
  362. local wd_tmp=""
  363. while read -r line
  364. do
  365. if [[ $line != "" ]]
  366. then
  367. arr=(${(s,:,)line})
  368. key=${arr[1]}
  369. val=${arr[2]}
  370. if [ -d "${val/#\~/$HOME}" ]
  371. then
  372. wd_tmp=$wd_tmp"\n"`echo "$line"`
  373. else
  374. wd_print_msg "$WD_YELLOW" "Nonexistent directory: ${key} -> ${val}"
  375. count=$((count+1))
  376. fi
  377. fi
  378. done < "$wd_config_file"
  379. if [[ $count -eq 0 ]]
  380. then
  381. wd_print_msg "$WD_BLUE" "No warp points to clean, carry on!"
  382. else
  383. if [ ! -z "$force" ] || wd_yesorno "Removing ${count} warp points. Continue? (y/n)"
  384. then
  385. echo "$wd_tmp" >! "$wd_config_file"
  386. wd_print_msg "$WD_GREEN" "Cleanup complete. ${count} warp point(s) removed"
  387. else
  388. wd_print_msg "$WD_BLUE" "Cleanup aborted"
  389. fi
  390. fi
  391. }
  392. wd_export_static_named_directories() {
  393. if [[ ! -z $WD_EXPORT ]]
  394. then
  395. command grep '^[0-9a-zA-Z_-]\+:' "$wd_config_file" | sed -e "s,~,$HOME," -e 's/:/=/' | while read -r warpdir ; do
  396. hash -d "$warpdir"
  397. done
  398. fi
  399. }
  400. WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
  401. local WD_QUIET=0
  402. local WD_EXIT_CODE=0
  403. # Parse 'meta' options first to avoid the need to have them before
  404. # other commands. The `-D` flag consumes recognized options so that
  405. # the actual command parsing won't be affected.
  406. zparseopts -D -E \
  407. c:=wd_alt_config -config:=wd_alt_config \
  408. q=wd_quiet_mode -quiet=wd_quiet_mode \
  409. v=wd_print_version -version=wd_print_version \
  410. f=wd_force_mode -force=wd_force_mode
  411. if [[ ! -z $wd_print_version ]]
  412. then
  413. echo "wd version $WD_VERSION"
  414. fi
  415. # set the config file from variable or default
  416. typeset wd_config_file=${WD_CONFIG:-$HOME/.warprc}
  417. if [[ ! -z $wd_alt_config ]]
  418. then
  419. # prefer the flag if provided
  420. wd_config_file=$wd_alt_config[2]
  421. fi
  422. # check if config file exists
  423. if [ ! -e "$wd_config_file" ]
  424. then
  425. # if not, create config file
  426. touch "$wd_config_file"
  427. else
  428. wd_export_static_named_directories
  429. fi
  430. # disable extendedglob for the complete wd execution time
  431. setopt | grep -q extendedglob
  432. wd_extglob_is_set=$?
  433. if (( wd_extglob_is_set == 0 )); then
  434. setopt noextendedglob
  435. fi
  436. # load warp points
  437. typeset -A points
  438. while read -r line
  439. do
  440. arr=(${(s,:,)line})
  441. key=${arr[1]}
  442. # join the rest, in case the path contains colons
  443. val=${(j,:,)arr[2,-1]}
  444. points[$key]=$val
  445. done < "$wd_config_file"
  446. # get opts
  447. args=$(getopt -o a:r:c:lhs -l add:,rm:,clean,list,ls:,path:,help,show -- $*)
  448. # check if no arguments were given, and that version is not set
  449. if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
  450. then
  451. wd_print_usage
  452. # check if config file is writeable
  453. elif [ ! -w "$wd_config_file" ]
  454. then
  455. # do nothing
  456. # can't run `exit`, as this would exit the executing shell
  457. wd_exit_fail "\'$wd_config_file\' is not writeable."
  458. else
  459. # parse rest of options
  460. local wd_o
  461. for wd_o
  462. do
  463. case "$wd_o"
  464. in
  465. "-a"|"--add"|"add")
  466. wd_add "$2" "$wd_force_mode"
  467. break
  468. ;;
  469. "-b"|"browse")
  470. wd_browse
  471. break
  472. ;;
  473. "-c"|"--addcd"|"addcd")
  474. wd_addcd "$2" "$3" "$wd_force_mode"
  475. break
  476. ;;
  477. "-e"|"export")
  478. wd_export_static_named_directories
  479. break
  480. ;;
  481. "-r"|"--remove"|"rm")
  482. # Passes all the arguments as a single string separated by whitespace to wd_remove
  483. wd_remove "${@:2}"
  484. break
  485. ;;
  486. "-l"|"list")
  487. wd_list_all
  488. break
  489. ;;
  490. "-ls"|"ls")
  491. wd_ls "$2"
  492. break
  493. ;;
  494. "-p"|"--path"|"path")
  495. wd_path "$2"
  496. break
  497. ;;
  498. "-h"|"--help"|"help")
  499. wd_print_usage
  500. break
  501. ;;
  502. "-s"|"--show"|"show")
  503. wd_show "$2"
  504. break
  505. ;;
  506. "-c"|"--clean"|"clean")
  507. wd_clean "$wd_force_mode"
  508. break
  509. ;;
  510. *)
  511. wd_warp "$wd_o" "$2"
  512. break
  513. ;;
  514. --)
  515. break
  516. ;;
  517. esac
  518. done
  519. fi
  520. ## garbage collection
  521. # if not, next time warp will pick up variables from this run
  522. # remember, there's no sub shell
  523. if (( wd_extglob_is_set == 0 )); then
  524. setopt extendedglob
  525. fi
  526. unset wd_extglob_is_set
  527. unset wd_warp
  528. unset wd_add
  529. unset wd_addcd
  530. unset wd_remove
  531. unset wd_show
  532. unset wd_list_all
  533. unset wd_print_msg
  534. unset wd_yesorno
  535. unset wd_print_usage
  536. unset wd_alt_config
  537. #unset wd_config_file do not unset this - breaks keybind
  538. unset wd_quiet_mode
  539. unset wd_print_version
  540. unset wd_force_mode
  541. unset wd_export_static_named_directories
  542. unset wd_o
  543. unset args
  544. unset points
  545. unset val &> /dev/null # fixes issue #1
  546. return $WD_EXIT_CODE