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.

124 lines
3.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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. ulimit -n 5000
  12. colors() {
  13. color=1;
  14. count=0;
  15. space=" ";
  16. while [ $color -lt 256 ]; do
  17. if [[ $color == 10 ]]
  18. then
  19. space=" "
  20. fi
  21. if [[ $color == 100 ]]
  22. then
  23. space=" "
  24. fi
  25. echo -en "$color:$space\\033[38;5;${color}myeet\\033[48;5;${color}mworld\\033[0m"
  26. echo -n " "
  27. if [[ $count == 7 ]]
  28. then
  29. echo -en "\n";
  30. count=-1
  31. fi
  32. ((color++));
  33. ((count++))
  34. done
  35. echo ""
  36. }
  37. # TMATE Functions
  38. TMATE_PAIR_NAME="$(whoami)-pair"
  39. TMATE_SOCKET_LOCATION="/tmp/tmate-pair.sock"
  40. TMATE_TMUX_SESSION="/tmp/tmate-tmux-session"
  41. # Get current tmate connection url
  42. tmate-url() {
  43. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh}')"
  44. echo "$url" | tr -d '\n' | xclip -selection clipboard
  45. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  46. echo "$url"
  47. }
  48. # Get current tmate connection url
  49. tmate-url-ro() {
  50. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}')"
  51. echo "$url" | tr -d '\n' | xclip -selection clipboard
  52. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  53. echo "$url"
  54. }
  55. # Start a new tmate pair session if one doesn't already exist
  56. # If creating a new session, the first argument can be an existing TMUX session to connect to automatically
  57. tmate-attach() {
  58. if [ -n "$1" ]; then
  59. tmux has-session -t $1 2>/dev/null
  60. if [ $? != 0 ]; then
  61. echo "Tmux session not found... Creating"
  62. tmux new-session -d -s $1
  63. sleep 1
  64. fi
  65. echo "Attaching tmate to tmux session $1";
  66. echo $1 > $TMATE_TMUX_SESSION
  67. tmate -S "$TMATE_SOCKET_LOCATION" send -t "$TMATE_PAIR_NAME" "TMUX='' tmux attach-session -t $1; tmate-unpair" ENTER
  68. fi
  69. }
  70. tmate-pair() {
  71. if [ -z "$TMATE_API_KEY" ]; then
  72. echo "You need an api key."
  73. return
  74. fi
  75. if [ ! -e "$TMATE_SOCKET_LOCATION" ]; then
  76. tmate -k $TMATE_API_KEY -r "sharedProgramming" -S "$TMATE_SOCKET_LOCATION" -f "$HOME/.tmate.conf" new-session -d -s "$TMATE_PAIR_NAME"
  77. tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}'
  78. while [ -z "$url" ]; do
  79. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}')"
  80. done
  81. echo "$url" | tr -d '\n' | xclip -selection clipboard
  82. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  83. echo "$url"
  84. tmate -S "$TMATE_SOCKET_LOCATION" send -t "$TMATE_PAIR_NAME" "q" ENTER
  85. sleep 1
  86. tmate-attach $1
  87. sleep 2
  88. fi
  89. tmate -S "$TMATE_SOCKET_LOCATION" attach-session -t "$TMATE_PAIR_NAME"
  90. }
  91. # Close the pair because security
  92. tmate-unpair() {
  93. if [ -e "$TMATE_SOCKET_LOCATION" ]; then
  94. if [ -e "$TMATE_SOCKET_LOCATION" ]; then
  95. tmux detach -s $(cat $TMATE_TMUX_SESSION)
  96. rm -f $TMATE_TMUX_SESSION
  97. fi
  98. tmate -S "$TMATE_SOCKET_LOCATION" kill-session -t "$TMATE_PAIR_NAME"
  99. echo "Killed session $TMATE_PAIR_NAME"
  100. else
  101. echo "Session already killed"
  102. fi
  103. }