wd.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/bin/zsh
  2. # WARP
  3. # ====
  4. # Jump to custom directories in terminal
  5. # because `cd` takes too long...
  6. #
  7. # @github.com/mfaerevaag/wd
  8. ## variables
  9. CONFIG=$HOME/.warprc
  10. ## colors
  11. BLUE="\033[96m"
  12. GREEN="\033[92m"
  13. YELLOW="\033[93m"
  14. RED="\033[91m"
  15. NOC="\033[m"
  16. # check if config file exists
  17. if [[ ! -a $CONFIG ]]
  18. then
  19. # if not: create config file
  20. touch $CONFIG
  21. fi
  22. ## load warp points
  23. typeset -A points
  24. while read line
  25. do
  26. arr=(${(s,:,)line})
  27. key=${arr[1]}
  28. val=${arr[2]}
  29. points[$key]=$val
  30. done < $CONFIG
  31. ## functions
  32. # prepended wd_ to not conflict with your environment (no sub shell)
  33. wd_warp()
  34. {
  35. if [[ $1 =~ "^\.+$" ]]
  36. then
  37. if [[ $#1 < 2 ]]
  38. then
  39. wd_print_msg $YELLOW "Warping to current directory?"
  40. else
  41. (( n = $#1 - 1 ))
  42. #wd_print_msg $BLUE "Warping..."
  43. cd -$n > /dev/null
  44. fi
  45. elif [[ ${points[$1]} != "" ]]
  46. then
  47. #wd_print_msg $BLUE "Warping..."
  48. cd ${points[$1]}
  49. else
  50. wd_print_msg $RED "Unknown warp point '$1'"
  51. fi
  52. }
  53. wd_add()
  54. {
  55. if [[ $2 =~ "^\.+$" || $2 =~ "^\s*$" ]]
  56. then
  57. wd_print_msg $RED "Illegal warp point (see README)."
  58. elif [[ ${points[$2]} == "" ]] || $1
  59. then
  60. wd_remove $2 > /dev/null
  61. print "$2:$PWD" >> $CONFIG
  62. wd_print_msg $GREEN "Warp point added"
  63. else
  64. wd_print_msg $YELLOW "Warp point '$2' already exists. Use 'add!' to overwrite."
  65. fi
  66. }
  67. wd_remove()
  68. {
  69. if [[ ${points[$1]} != "" ]]
  70. then
  71. if wd_tmp=`sed "/^$1:/d" $CONFIG`
  72. then
  73. # `>!` forces overwrite
  74. # we need this if people use `setopt NO_CLOBBER`
  75. echo $wd_tmp >! $CONFIG
  76. wd_print_msg $GREEN "Warp point removed"
  77. else
  78. wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
  79. fi
  80. else
  81. wd_print_msg $RED "Warp point was not found"
  82. fi
  83. }
  84. wd_show()
  85. {
  86. wd_print_msg $BLUE "Warp points to current directory:"
  87. wd_list_all | grep $PWD$
  88. }
  89. wd_list_all()
  90. {
  91. wd_print_msg $BLUE "All warp points:"
  92. while read line
  93. do
  94. if [[ $line != "" ]]
  95. then
  96. arr=(${(s,:,)line})
  97. key=${arr[1]}
  98. val=${arr[2]}
  99. print "\t" $key "\t -> \t" $val
  100. fi
  101. done < $CONFIG
  102. }
  103. wd_print_msg()
  104. {
  105. if [[ $1 == "" || $2 == "" ]]
  106. then
  107. print " $RED*$NOC Could not print message. Sorry!"
  108. else
  109. print " $1*$NOC $2"
  110. fi
  111. }
  112. wd_print_usage()
  113. {
  114. print "Usage: wd [add|-a|--add] [rm|-r|--remove] [ls|-l|--list] <point>"
  115. print "\nCommands:"
  116. print "\t add \t Adds the current working directory to your warp points"
  117. print "\t add! \t Overwrites existing warp point"
  118. print "\t rm \t Removes the given warp point"
  119. print "\t show \t Outputs warp points to current directory"
  120. print "\t ls \t Outputs all stored warp points"
  121. print "\t help \t Show this extremely helpful text"
  122. }
  123. ## run
  124. # get opts
  125. args=`getopt -o a:r:lhs -l add:,rm:,ls,help,show -- $*`
  126. # check if no arguments were given
  127. if [[ $? -ne 0 || $#* -eq 0 ]]
  128. then
  129. wd_print_usage
  130. # check if config file is writeable
  131. elif [[ ! -w $CONFIG ]]
  132. then
  133. wd_print_msg $RED "\'$CONFIG\' is not writeable."
  134. # do nothing => exit
  135. # can't run `exit`, as this would exit the executing shell
  136. # i.e. your terminal
  137. else
  138. #set -- $args # WTF
  139. for i
  140. do
  141. case "$i"
  142. in
  143. -a|--add|add)
  144. wd_add false $2
  145. break
  146. ;;
  147. -a!|--add!|add!)
  148. wd_add true $2
  149. break
  150. ;;
  151. -r|--remove|rm)
  152. wd_remove $2
  153. break
  154. ;;
  155. -l|--list|ls)
  156. wd_list_all
  157. break
  158. ;;
  159. -h|--help|help)
  160. wd_print_usage
  161. break
  162. ;;
  163. -s|--show|show)
  164. wd_show
  165. break
  166. ;;
  167. *)
  168. wd_warp $i
  169. break
  170. ;;
  171. --)
  172. break
  173. ;;
  174. esac
  175. done
  176. fi
  177. ## garbage collection
  178. # if not, next time warp will pick up variables from this run
  179. # remember, there's no sub shell
  180. unset points
  181. unset args
  182. unset val &> /dev/null # fixes issue #1