Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

179 lines
4.3 KiB

  1. #!/usr/bin/zsh
  2. count() {
  3. echo -n $1 | wc -c
  4. }
  5. dec() {
  6. echo "obase=10; ibase=16; $(echo "$1" | tr a-z A-Z)" | bc
  7. }
  8. hex() {
  9. echo "obase=16; ibase=10; $1" | bc
  10. }
  11. colors() {
  12. color=1;
  13. count=0;
  14. space=" ";
  15. while [ $color -lt 256 ]; do
  16. if [[ $color == 10 ]]
  17. then
  18. space=" "
  19. fi
  20. if [[ $color == 100 ]]
  21. then
  22. space=" "
  23. fi
  24. echo -en "$color:$space\\033[38;5;${color}myeet\\033[48;5;${color}mworld\\033[0m"
  25. echo -n " "
  26. if [[ $count == 7 ]]
  27. then
  28. echo -en "\n";
  29. count=-1
  30. fi
  31. ((color++));
  32. ((count++))
  33. done
  34. echo ""
  35. }
  36. # TMATE Functions
  37. TMATE_PAIR_NAME="$(whoami)-pair"
  38. TMATE_SOCKET_LOCATION="$XDG_RUNTIME_DIR/tmate-pair.sock"
  39. TMATE_TMUX_SESSION="$XDG_RUNTIME_DIR/tmate-tmux-session"
  40. # Get current tmate connection url
  41. tmate-url() {
  42. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh}')"
  43. echo "$url" | tr -d '\n' | xclip -selection clipboard
  44. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  45. echo "$url"
  46. }
  47. # Get current tmate connection url
  48. tmate-url-ro() {
  49. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}')"
  50. echo "$url" | tr -d '\n' | xclip -selection clipboard
  51. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  52. echo "$url"
  53. }
  54. # Start a new tmate pair session if one doesn't already exist
  55. # If creating a new session, the first argument can be an existing TMUX session to connect to automatically
  56. tmate-attach() {
  57. if [ -n "$1" ]; then
  58. tmux has-session -t $1 2>/dev/null
  59. if [ $? != 0 ]; then
  60. echo "Tmux session not found... Creating"
  61. tmux new-session -d -s $1
  62. sleep 1
  63. fi
  64. echo "Attaching tmate to tmux session $1";
  65. echo $1 > $TMATE_TMUX_SESSION
  66. tmate -S "$TMATE_SOCKET_LOCATION" send -t "$TMATE_PAIR_NAME" "TMUX='' tmux attach-session -t $1; tmate-unpair" ENTER
  67. fi
  68. }
  69. tmate-pair() {
  70. if [ -z "$(pass show "AppPass/tmate.com/api-key")" ]; then
  71. echo "You need an api key."
  72. return
  73. fi
  74. if [ ! -e "$TMATE_SOCKET_LOCATION" ]; then
  75. tmate -k $(pass show "AppPass/tmate.com/api-key") -r "sharedProgramming" -S "$TMATE_SOCKET_LOCATION" -f "$HOME/.tmate.conf" new-session -d -s "$TMATE_PAIR_NAME"
  76. tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}'
  77. while [ -z "$url" ]; do
  78. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}')"
  79. done
  80. echo "$url" | tr -d '\n' | xclip -selection clipboard
  81. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  82. echo "$url"
  83. tmate -S "$TMATE_SOCKET_LOCATION" send -t "$TMATE_PAIR_NAME" "q" ENTER
  84. sleep 1
  85. tmate-attach $1
  86. sleep 2
  87. fi
  88. tmate -S "$TMATE_SOCKET_LOCATION" attach-session -t "$TMATE_PAIR_NAME"
  89. }
  90. # Colored man
  91. man() {
  92. LESS_TERMCAP_md=$'\e[01;31m' \
  93. LESS_TERMCAP_me=$'\e[0m' \
  94. LESS_TERMCAP_so=$'\e[01;44;33m' \
  95. LESS_TERMCAP_se=$'\e[0m' \
  96. LESS_TERMCAP_us=$'\e[01;32m' \
  97. LESS_TERMCAP_ue=$'\e[0m' \
  98. command man "$@"
  99. }
  100. genccls() {
  101. cat > .ccls << EOF
  102. -I
  103. ../include
  104. -I
  105. ../vendor/include
  106. -std=c++14
  107. -stdlib=libc++
  108. -fPIC
  109. EOF
  110. }
  111. # Close the pair because security
  112. tmate-unpair() {
  113. if [ -e "$TMATE_SOCKET_LOCATION" ]; then
  114. if [ -e "$TMATE_SOCKET_LOCATION" ]; then
  115. tmux detach -s $(cat $TMATE_TMUX_SESSION)
  116. rm -f $TMATE_TMUX_SESSION
  117. fi
  118. tmate -S "$TMATE_SOCKET_LOCATION" kill-session -t "$TMATE_PAIR_NAME"
  119. echo "Killed session $TMATE_PAIR_NAME"
  120. else
  121. echo "Session already killed"
  122. fi
  123. }
  124. transfer() {
  125. curl --upload-file "$1" "https://transfer.sh/$1"
  126. }
  127. rawurlencode() {
  128. local string="${1}:$(cat -)"
  129. local strlen=${#string}
  130. local encoded=""
  131. local pos c o
  132. for (( pos=0 ; pos<strlen ; pos++ )); do
  133. c=${string:$pos:1}
  134. case "$c" in
  135. [-_.~a-zA-Z0-9] ) o="${c}" ;;
  136. * ) printf -v o '%%%02x' "'$c"
  137. esac
  138. encoded+="${o}"
  139. done
  140. echo "${encoded}" # You can either set a return variable (FASTER)
  141. REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
  142. }
  143. rawurldecode() {
  144. printf -v REPLY '%b' "${1//%/\\x}" # You can either set a return variable (FASTER)
  145. echo "${REPLY}" #+or echo the result (EASIER)... or both... :p
  146. }
  147. pdfcomp(){
  148. gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile="${2}" "${1}"
  149. }