my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

501 lines
12 KiB

3 years ago
  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.5.0
  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. command 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. 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 (will prompt unless --force is used)
  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. -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. fi
  125. elif [[ ${points[$point]} != "" ]]
  126. then
  127. if [[ $sub != "" ]]
  128. then
  129. cd ${points[$point]/#\~/$HOME}/$sub
  130. else
  131. cd ${points[$point]/#\~/$HOME}
  132. fi
  133. else
  134. wd_exit_fail "Unknown warp point '${point}'"
  135. fi
  136. }
  137. wd_add()
  138. {
  139. local point=$1
  140. local force=$2
  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 [[ ${points[$point]} == "" ]] || [ ! -z "$force" ]
  155. then
  156. wd_remove "$point" > /dev/null
  157. printf "%q:%s\n" "${point}" "${PWD/#$HOME/~}" >> "$WD_CONFIG"
  158. if (whence sort >/dev/null); then
  159. local config_tmp=$(mktemp "${TMPDIR:-/tmp}/wd.XXXXXXXXXX")
  160. # use 'cat' below to ensure we respect $WD_CONFIG as a symlink
  161. command sort -o "${config_tmp}" "$WD_CONFIG" && command cat "${config_tmp}" > "$WD_CONFIG" && command rm "${config_tmp}"
  162. fi
  163. wd_export_static_named_directories
  164. wd_print_msg "$WD_GREEN" "Warp point added"
  165. # override exit code in case wd_remove did not remove any points
  166. # TODO: we should handle this kind of logic better
  167. WD_EXIT_CODE=0
  168. else
  169. wd_exit_warn "Warp point '${point}' already exists. Use 'add --force' to overwrite."
  170. fi
  171. }
  172. wd_remove()
  173. {
  174. local point_list=$1
  175. if [[ "$point_list" == "" ]]
  176. then
  177. point_list=$(basename "$PWD")
  178. fi
  179. for point_name in $point_list ; do
  180. if [[ ${points[$point_name]} != "" ]]
  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_name}:.*$/!p" "$WD_CONFIG" > "$config_tmp" && command cp "$config_tmp" "$WD_CONFIG" && command 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. done
  194. }
  195. wd_list_all()
  196. {
  197. wd_print_msg "$WD_BLUE" "All warp points:"
  198. entries=$(sed "s:${HOME}:~:g" "$WD_CONFIG")
  199. max_warp_point_length=0
  200. while IFS= read -r line
  201. do
  202. arr=(${(s,:,)line})
  203. key=${arr[1]}
  204. length=${#key}
  205. if [[ length -gt max_warp_point_length ]]
  206. then
  207. max_warp_point_length=$length
  208. fi
  209. done <<< "$entries"
  210. while IFS= read -r line
  211. do
  212. if [[ $line != "" ]]
  213. then
  214. arr=(${(s,:,)line})
  215. key=${arr[1]}
  216. val=${arr[2]}
  217. if [[ -z $wd_quiet_mode ]]
  218. then
  219. printf "%${max_warp_point_length}s -> %s\n" "$key" "$val"
  220. fi
  221. fi
  222. done <<< "$entries"
  223. }
  224. wd_ls()
  225. {
  226. wd_getdir "$1"
  227. ls "${dir/#\~/$HOME}"
  228. }
  229. wd_path()
  230. {
  231. wd_getdir "$1"
  232. echo "$(echo "$dir" | sed "s:~:${HOME}:g")"
  233. }
  234. wd_show()
  235. {
  236. local name_arg=$1
  237. # if there's an argument we look up the value
  238. if [[ -n $name_arg ]]
  239. then
  240. if [[ -z $points[$name_arg] ]]
  241. then
  242. wd_print_msg "$WD_BLUE" "No warp point named $name_arg"
  243. else
  244. wd_print_msg "$WD_GREEN" "Warp point: ${WD_GREEN}$name_arg${WD_NOC} -> $points[$name_arg]"
  245. fi
  246. else
  247. # hax to create a local empty array
  248. local wd_matches
  249. wd_matches=()
  250. # do a reverse lookup to check whether PWD is in $points
  251. PWD="${PWD/$HOME/~}"
  252. if [[ ${points[(r)$PWD]} == "$PWD" ]]
  253. then
  254. for name in ${(k)points}
  255. do
  256. if [[ $points[$name] == "$PWD" ]]
  257. then
  258. wd_matches[$(($#wd_matches+1))]=$name
  259. fi
  260. done
  261. wd_print_msg "$WD_BLUE" "$#wd_matches warp point(s) to current directory: ${WD_GREEN}$wd_matches${WD_NOC}"
  262. else
  263. wd_print_msg "$WD_YELLOW" "No warp point to $(echo "$PWD" | sed "s:$HOME:~:")"
  264. fi
  265. fi
  266. }
  267. wd_clean() {
  268. local force=$1
  269. local count=0
  270. local wd_tmp=""
  271. while read -r line
  272. do
  273. if [[ $line != "" ]]
  274. then
  275. arr=(${(s,:,)line})
  276. key=${arr[1]}
  277. val=${arr[2]}
  278. if [ -d "${val/#\~/$HOME}" ]
  279. then
  280. wd_tmp=$wd_tmp"\n"`echo "$line"`
  281. else
  282. wd_print_msg "$WD_YELLOW" "Nonexistent directory: ${key} -> ${val}"
  283. count=$((count+1))
  284. fi
  285. fi
  286. done < "$WD_CONFIG"
  287. if [[ $count -eq 0 ]]
  288. then
  289. wd_print_msg "$WD_BLUE" "No warp points to clean, carry on!"
  290. else
  291. if [ ! -z "$force" ] || wd_yesorno "Removing ${count} warp points. Continue? (y/n)"
  292. then
  293. echo "$wd_tmp" >! "$WD_CONFIG"
  294. wd_print_msg "$WD_GREEN" "Cleanup complete. ${count} warp point(s) removed"
  295. else
  296. wd_print_msg "$WD_BLUE" "Cleanup aborted"
  297. fi
  298. fi
  299. }
  300. wd_export_static_named_directories() {
  301. if [[ ! -z $WD_EXPORT ]]
  302. then
  303. command grep '^[0-9a-zA-Z_-]\+:' "$WD_CONFIG" | sed -e "s,~,$HOME," -e 's/:/=/' | while read -r warpdir ; do
  304. hash -d "$warpdir"
  305. done
  306. fi
  307. }
  308. local WD_CONFIG=${WD_CONFIG:-$HOME/.warprc}
  309. local WD_QUIET=0
  310. local WD_EXIT_CODE=0
  311. local WD_DEBUG=0
  312. # Parse 'meta' options first to avoid the need to have them before
  313. # other commands. The `-D` flag consumes recognized options so that
  314. # the actual command parsing won't be affected.
  315. zparseopts -D -E \
  316. c:=wd_alt_config -config:=wd_alt_config \
  317. q=wd_quiet_mode -quiet=wd_quiet_mode \
  318. v=wd_print_version -version=wd_print_version \
  319. d=wd_debug_mode -debug=wd_debug_mode \
  320. f=wd_force_mode -force=wd_force_mode
  321. if [[ ! -z $wd_print_version ]]
  322. then
  323. echo "wd version $WD_VERSION"
  324. fi
  325. if [[ ! -z $wd_alt_config ]]
  326. then
  327. WD_CONFIG=$wd_alt_config[2]
  328. fi
  329. # check if config file exists
  330. if [ ! -e "$WD_CONFIG" ]
  331. then
  332. # if not, create config file
  333. touch "$WD_CONFIG"
  334. else
  335. wd_export_static_named_directories
  336. fi
  337. # load warp points
  338. typeset -A points
  339. while read -r line
  340. do
  341. arr=(${(s,:,)line})
  342. key=${arr[1]}
  343. # join the rest, in case the path contains colons
  344. val=${(j,:,)arr[2,-1]}
  345. points[$key]=$val
  346. done < "$WD_CONFIG"
  347. # get opts
  348. args=$(getopt -o a:r:c:lhs -l add:,rm:,clean,list,ls:,path:,help,show -- $*)
  349. # check if no arguments were given, and that version is not set
  350. if [[ ($? -ne 0 || $#* -eq 0) && -z $wd_print_version ]]
  351. then
  352. wd_print_usage
  353. # check if config file is writeable
  354. elif [ ! -w "$WD_CONFIG" ]
  355. then
  356. # do nothing
  357. # can't run `exit`, as this would exit the executing shell
  358. wd_exit_fail "\'$WD_CONFIG\' is not writeable."
  359. else
  360. # parse rest of options
  361. local wd_o
  362. for wd_o
  363. do
  364. case "$wd_o"
  365. in
  366. "-a"|"--add"|"add")
  367. wd_add "$2" "$wd_force_mode"
  368. break
  369. ;;
  370. "-e"|"export")
  371. wd_export_static_named_directories
  372. break
  373. ;;
  374. "-r"|"--remove"|"rm")
  375. # Passes all the arguments as a single string separated by whitespace to wd_remove
  376. wd_remove "${@:2}"
  377. break
  378. ;;
  379. "-l"|"list")
  380. wd_list_all
  381. break
  382. ;;
  383. "-ls"|"ls")
  384. wd_ls "$2"
  385. break
  386. ;;
  387. "-p"|"--path"|"path")
  388. wd_path "$2"
  389. break
  390. ;;
  391. "-h"|"--help"|"help")
  392. wd_print_usage
  393. break
  394. ;;
  395. "-s"|"--show"|"show")
  396. wd_show "$2"
  397. break
  398. ;;
  399. "-c"|"--clean"|"clean")
  400. wd_clean "$wd_force_mode"
  401. break
  402. ;;
  403. *)
  404. wd_warp "$wd_o" "$2"
  405. break
  406. ;;
  407. --)
  408. break
  409. ;;
  410. esac
  411. done
  412. fi
  413. ## garbage collection
  414. # if not, next time warp will pick up variables from this run
  415. # remember, there's no sub shell
  416. unset wd_warp
  417. unset wd_add
  418. unset wd_remove
  419. unset wd_show
  420. unset wd_list_all
  421. unset wd_print_msg
  422. unset wd_yesorno
  423. unset wd_print_usage
  424. unset wd_alt_config
  425. unset wd_quiet_mode
  426. unset wd_print_version
  427. unset wd_export_static_named_directories
  428. unset wd_o
  429. unset args
  430. unset points
  431. unset val &> /dev/null # fixes issue #1
  432. if [[ -n $wd_debug_mode ]]
  433. then
  434. exit $WD_EXIT_CODE
  435. else
  436. unset wd_debug_mode
  437. fi