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.

118 lines
2.9 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 [ ! -e "$TMATE_SOCKET_LOCATION" ]; then
  72. tmate -k $TMATE_API_KEY -r "sharedProgramming" -S "$TMATE_SOCKET_LOCATION" -f "$HOME/.tmate.conf" new-session -d -s "$TMATE_PAIR_NAME"
  73. while [ -z "$url" ]; do
  74. url="$(tmate -S $TMATE_SOCKET_LOCATION display -p '#{tmate_ssh_ro}')"
  75. done
  76. echo "$url" | tr -d '\n' | xclip -selection clipboard
  77. echo "Copied tmate url for $TMATE_PAIR_NAME:"
  78. echo "$url"
  79. tmate -S "$TMATE_SOCKET_LOCATION" send -t "$TMATE_PAIR_NAME" "q" ENTER
  80. sleep 1
  81. tmate-attach $1
  82. sleep 2
  83. fi
  84. tmate -S "$TMATE_SOCKET_LOCATION" attach-session -t "$TMATE_PAIR_NAME"
  85. }
  86. # Close the pair because security
  87. tmate-unpair() {
  88. if [ -e "$TMATE_SOCKET_LOCATION" ]; then
  89. if [ -e "$TMATE_SOCKET_LOCATION" ]; then
  90. tmux detach -s $(cat $TMATE_TMUX_SESSION)
  91. rm -f $TMATE_TMUX_SESSION
  92. fi
  93. tmate -S "$TMATE_SOCKET_LOCATION" kill-session -t "$TMATE_PAIR_NAME"
  94. echo "Killed session $TMATE_PAIR_NAME"
  95. else
  96. echo "Session already killed"
  97. fi
  98. }