wd.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "Unkown warp point '$1'"
  51. fi
  52. }
  53. wd_add()
  54. {
  55. if [[ $1 =~ "^\.+$" ]]
  56. then
  57. wd_print_msg $RED "Illeagal warp point (see README)."
  58. elif [[ ${points[$1]} == "" ]] || $2
  59. then
  60. wd_remove $1 > /dev/null
  61. print "$1:$PWD" >> $CONFIG
  62. wd_print_msg $GREEN "Warp point added"
  63. else
  64. wd_print_msg $YELLOW "Warp point '$1' alredy 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. echo $wd_tmp > $CONFIG
  74. wd_print_msg $GREEN "Warp point removed"
  75. else
  76. wd_print_msg $RED "Warp point unsuccessfully removed. Sorry!"
  77. fi
  78. else
  79. wd_print_msg $RED "Warp point was not found"
  80. fi
  81. }
  82. wd_show()
  83. {
  84. wd_print_msg $BLUE "Warp points to current directory:"
  85. wd_list_all | grep $PWD$
  86. }
  87. wd_list_all()
  88. {
  89. wd_print_msg $BLUE "All warp points:"
  90. while read line
  91. do
  92. if [[ $line != "" ]]
  93. then
  94. arr=(${(s,:,)line})
  95. key=${arr[1]}
  96. val=${arr[2]}
  97. print "\t" $key "\t -> \t" $val
  98. fi
  99. done < $CONFIG
  100. }
  101. wd_print_msg()
  102. {
  103. if [[ $1 == "" || $2 == "" ]]
  104. then
  105. print " $RED*$NOC Could not print message. Sorry!"
  106. else
  107. print " $1*$NOC $2"
  108. fi
  109. }
  110. wd_print_usage()
  111. {
  112. print "Usage: wd [add|-a|--add] [rm|-r|--remove] [ls|-l|--list] <point>"
  113. print "\nCommands:"
  114. print "\t add \t Adds the current working directory to your warp points"
  115. print "\t add! \t Overwrites existing warp point"
  116. print "\t remove Removes the given warp point"
  117. print "\t show \t Outputs warp points to current directory"
  118. print "\t list \t Outputs all stored warp points"
  119. print "\t help \t Show this extremely helpful text"
  120. }
  121. ## run
  122. # get opts
  123. args=`getopt -o a:r:lhs -l add:,remove:,list,help,show -- $*`
  124. # check if no arguments were given
  125. if [[ $? -ne 0 || $#* -eq 0 ]]
  126. then
  127. wd_print_usage
  128. # check if config file is writeable
  129. elif [[ ! -w $CONFIG ]]
  130. then
  131. wd_print_msg $RED "\'$CONFIG\' is not writeable."
  132. # do nothing => exit
  133. # can't run `exit`, as this would exit the executing shell
  134. # i.e. your terminal
  135. else
  136. #set -- $args # WTF
  137. for i
  138. do
  139. case "$i"
  140. in
  141. -a|--add|add)
  142. wd_add $2 false
  143. shift
  144. shift
  145. break
  146. ;;
  147. -a!|--add!|add!)
  148. wd_add $2 true
  149. shift
  150. shift
  151. break
  152. ;;
  153. -r|--remove|rm)
  154. wd_remove $2
  155. shift
  156. shift
  157. break
  158. ;;
  159. -l|--list|ls)
  160. wd_list_all
  161. shift
  162. break
  163. ;;
  164. -h|--help|help)
  165. wd_print_usage
  166. shift
  167. break
  168. ;;
  169. -s|--show|show)
  170. wd_show
  171. shift
  172. break
  173. ;;
  174. *)
  175. wd_warp $i
  176. shift
  177. break
  178. ;;
  179. --)
  180. shift; break;;
  181. esac
  182. done
  183. fi
  184. ## garbage collection
  185. # if not, next time warp will pick up variables from this run
  186. # remember, there's no sub shell
  187. points=""
  188. args=""
  189. unhash -d val &> /dev/null # fixes issue #1