scwrypts v4.4 config adjustments; added os dependencies; removed dotwryn.env.vim references from setup; generate i3 config on setup
This commit is contained in:
148
scwrypts/remote/tmux/manager
Executable file
148
scwrypts/remote/tmux/manager
Executable file
@ -0,0 +1,148 @@
|
||||
#!/bin/zsh
|
||||
#
|
||||
# works as a standalone zsh script
|
||||
#
|
||||
|
||||
#####################################################################
|
||||
|
||||
CONNECTIONS_FILE="$HOME/.config/wryn/remote-connections.toml"
|
||||
|
||||
OMNI_LOGDIR="$HOME/.local/share/scwrypts/dotwryn"
|
||||
OMNI_LOGFILE="$HOME/.local/share/scwrypts/dotwryn/omni.current.txt"
|
||||
|
||||
[ ! -d "$OMNI_LOGDIR" ] && mkdir -p "$OMNI_LOGDIR"
|
||||
|
||||
[ -f "$OMNI_LOGFILE" ] && {
|
||||
for x in {1..99}
|
||||
do
|
||||
[ ! -f "$OMNI_LOGDIR/omni.$x.txt" ] && break
|
||||
done
|
||||
|
||||
[ ! -f "$OMNI_LOGDIR/omni.$x.txt" ] && mv "$OMNI_LOGFILE" "$OMNI_LOGDIR/omni.$x.txt"
|
||||
}
|
||||
|
||||
echo "OMNI MANAGER START : $(date)" > "$OMNI_LOGFILE"
|
||||
|
||||
OMNI_SOCKET="omni.socket"
|
||||
OMNI_TMUX() { tmux -L $OMNI_SOCKET $@; }
|
||||
|
||||
CONFIG_QUERY() { yq -oy -r $@ "$CONNECTIONS_FILE" | grep -v ^null$; }
|
||||
|
||||
CONNECTED() {
|
||||
[ $WINDOW_ID ] \
|
||||
&& echo "\\033[1;32mconnected (window $WINDOW_ID)\\033[0m" >&2 \
|
||||
|| echo "\\033[1;32mconnected\\033[0m" >&2 \
|
||||
}
|
||||
DISCONNECTED() { echo "\\033[1;31mdisconnected\\033[0m" >&2; }
|
||||
|
||||
GET_UNIQUE_WINDOW_ID() {
|
||||
local MODE=use-default
|
||||
|
||||
local WINDOW_ID="$(eval "echo \$WINDOW_ID__$REMOTE_NAME")"
|
||||
: \
|
||||
&& [[ $(eval "echo \$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \
|
||||
&& [ $WINDOW_ID ] \
|
||||
&& echo $WINDOW_ID \
|
||||
&& return 0 \
|
||||
;
|
||||
|
||||
unset WINDOW_ID__$REMOTE_NAME &>>"$OMNI_LOGFILE"
|
||||
|
||||
WINDOW_ID="$1"
|
||||
WINDOW_ID=$(CONFIG_QUERY .sessions.$REMOTE_NAME.id)
|
||||
[ $WINDOW_ID ] && MODE=specify-preferred
|
||||
|
||||
WINDOW_ID_IS_TAKEN() {
|
||||
: \
|
||||
&& [[ $(OMNI_TMUX list-windows -t=omni | grep "^$WINDOW_ID:" | wc -l) -gt 0 ]] \
|
||||
&& [[ ! $(OMNI_TMUX list-windows -t=omni | grep "^$WINDOW_ID:" | awk '{print $2;}' =~ ^$REMOTE_NAME$) ]] \
|
||||
;
|
||||
}
|
||||
|
||||
local FALLBACK_STARTING_POINT
|
||||
case $MODE in
|
||||
use-default ) FALLBACK_STARTING_POINT=42 ;;
|
||||
specify-preferred ) FALLBACK_STARTING_POINT=69 ;;
|
||||
esac
|
||||
|
||||
local I=0
|
||||
while WINDOW_ID_IS_TAKEN
|
||||
do
|
||||
echo "$REMOTE_NAME tried to acquire window id $WINDOW_ID, but it is already in-use (trying $(($FALLBACK_STARTING_POINT-$I)))" >&2
|
||||
WINDOW_ID=$(($FALLBACK_STARTING_POINT-$I))
|
||||
((I+=1))
|
||||
[[ $I -gt 10 ]] && WINDOW_ID= && break
|
||||
done
|
||||
|
||||
echo $WINDOW_ID
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
sleep 1
|
||||
OMNI_TMUX new-window -t=omni-manager -dn 'harakiri' "
|
||||
while true
|
||||
do
|
||||
tmux -L $OMNI_SOCKET list-sessions | grep -v omni-manager | grep -qi omni || {
|
||||
tmux -L $OMNI_SOCKET kill-session -t omni-manager
|
||||
}
|
||||
sleep 5
|
||||
done
|
||||
"
|
||||
|
||||
while true
|
||||
do
|
||||
clear
|
||||
for REMOTE_NAME in $(CONFIG_QUERY '.sessions | keys | .[]')
|
||||
do
|
||||
WINDOW_ID=$(GET_UNIQUE_WINDOW_ID)
|
||||
export WINDOW_ID__$REMOTE_NAME=$WINDOW_ID
|
||||
|
||||
OMNI_TMUX list-windows -t=omni | awk '{print $2;}' | grep -q $REMOTE_NAME \
|
||||
&& continue
|
||||
|
||||
printf "testing connection $REMOTE_NAME..."
|
||||
echo "testing connection $REMOTE_NAME..." &>>"$OMNI_LOGFILE"
|
||||
scwrypts --name test --group remote --type zsh \
|
||||
-- \
|
||||
--name $REMOTE_NAME \
|
||||
--command 'command -v tmux' \
|
||||
&>>"$OMNI_LOGFILE" \
|
||||
&& export CAN_CONNECT__$REMOTE_NAME=true \
|
||||
|| export CAN_CONNECT__$REMOTE_NAME=false \
|
||||
;
|
||||
|
||||
[[ $(eval echo "\$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \
|
||||
&& echo " \\033[1;32m✔\\033[0m" >&2 \
|
||||
|| { echo " \\033[1;31m✖\\033[0m" >&2; continue; }
|
||||
|
||||
OMNI_TMUX new-window \
|
||||
-t=omni:$WINDOW_ID \
|
||||
-dn $REMOTE_NAME "
|
||||
source ~/.zshrc &>/dev/null
|
||||
TMUX='' scwrypts -n connect/$REMOTE_NAME -- --type tmux
|
||||
echo 'connection closed'
|
||||
sleep 2
|
||||
"
|
||||
|
||||
OMNI_TMUX list-window -t=omni | awk '{print $1;}' | grep -q '99:' && OMNI_TMUX kill-window -t omni:99
|
||||
|
||||
OMNI_TMUX list-windows -t=omni | awk '{print $2;}' | grep -q $REMOTE_NAME \
|
||||
|| export CAN_CONNECT__$REMOTE_NAME=false
|
||||
done
|
||||
|
||||
clear
|
||||
echo "connections:\n"
|
||||
local STATUS
|
||||
{
|
||||
for REMOTE_NAME in $(CONFIG_QUERY '.sessions | keys | .[]')
|
||||
do
|
||||
[[ $(eval "echo \$CAN_CONNECT__$REMOTE_NAME") =~ true ]] \
|
||||
&& echo "${REMOTE_NAME}^$(eval "echo \$WINDOW_ID__$REMOTE_NAME")^\\033[1;32mconnected\\033[0m" \
|
||||
|| echo "${REMOTE_NAME}^-^\\033[1;31mdisconnected\\033[0m" \
|
||||
;
|
||||
done
|
||||
} | column -ts '^'
|
||||
echo "\nPress ENTER to search again now (checks every 60 seconds)" >&2
|
||||
read -t 60
|
||||
done
|
53
scwrypts/remote/tmux/omni
Executable file
53
scwrypts/remote/tmux/omni
Executable file
@ -0,0 +1,53 @@
|
||||
#!/bin/zsh
|
||||
DEPENDENCIES+=(tmux scwrypts)
|
||||
#####################################################################
|
||||
|
||||
USAGE__description="
|
||||
A program which automatically connects to any available connections
|
||||
described in the configuration file.
|
||||
(configure with 'scwrypts remote configure')
|
||||
|
||||
The omni-session is running it's own wrapper TMUX server with a special
|
||||
config. In the wrapper session, 'M-s' is overwritten as the prefix.
|
||||
Switch between connections by using 'M-s' followed by the session ID
|
||||
number (or use any other default default tmux navigation command). Full
|
||||
configuration can be found here:
|
||||
- $SCWRYPTS_ROOT__remote/omni/tmux.conf
|
||||
|
||||
Shut-down the omni-session by pressing 'M-Q' (Q not q! ALT + SHIFT + Q)
|
||||
|
||||
Disconnect (but leave the session running) with 'M-s, d'
|
||||
|
||||
A background process periodically attempts to refresh lost connections.
|
||||
Immediate retry can be forced with 'M-R'.
|
||||
|
||||
Running this command in an existing tmux session will likely result
|
||||
in an infinite loop, so:
|
||||
- don't run the omni-session in a TMUX session
|
||||
- don't connect to the omni-session directly (always use this scwrypt)
|
||||
"
|
||||
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
[[ $TERM =~ tmux ]] && ERROR "\n Cannot run tmux-omni within a tmux session!\n "
|
||||
CHECK_ERRORS
|
||||
|
||||
local OMNI_SOCKET="omni.socket"
|
||||
OMNI_TMUX() { tmux -L $OMNI_SOCKET $@; }
|
||||
|
||||
OMNI_TMUX list-sessions 2>/dev/null | grep -v omni-manager | grep -qi omni || {
|
||||
STATUS "initializing omni server"
|
||||
OMNI_TMUX kill-session -t=omni-manager >/dev/null 2>&1
|
||||
|
||||
OMNI_TMUX -f "$SCWRYPTS_ROOT__remote/tmux/tmux.conf" new -d -s omni \
|
||||
"echo searching for first connection...; sleep 30" \; \
|
||||
split-window "sleep 3; TMUX= tmux -L $OMNI_SOCKET a -t=omni-manager" \; \
|
||||
move-window -t 99 \;
|
||||
|
||||
OMNI_TMUX new -d -s omni-manager "$SCWRYPTS_ROOT__remote/tmux/manager"
|
||||
}
|
||||
|
||||
STATUS 'connecting to omni server'
|
||||
OMNI_TMUX a -t=omni
|
||||
}
|
70
scwrypts/remote/tmux/tmux.conf
Normal file
70
scwrypts/remote/tmux/tmux.conf
Normal file
@ -0,0 +1,70 @@
|
||||
# config (mostly inherited from ../../../config/tmux.conf)
|
||||
set -g default-terminal "tmux-256color"
|
||||
set -g mouse off
|
||||
set -sg escape-time 0
|
||||
set-option -g default-shell /bin/zsh
|
||||
set-option -gw xterm-keys on
|
||||
set-window-option -g mode-keys vi
|
||||
set-window-option -g status-keys vi
|
||||
setw -gq utf8 on
|
||||
set -g status-style fg=brightmagenta,bg=black
|
||||
set -g window-status-current-style fg=black,bg=brightmagenta
|
||||
set -g status-right "omni host : #(hostnamectl --static) | #(cat /sys/class/power_supply/BAT0/capacity)% "
|
||||
|
||||
# navigate between hosts with ALT+window-number
|
||||
set -g base-index 1
|
||||
|
||||
bind-key 2 select-window -t 1
|
||||
bind-key 2 select-window -t 2
|
||||
bind-key 3 select-window -t 3
|
||||
bind-key 4 select-window -t 4
|
||||
bind-key 5 select-window -t 5
|
||||
bind-key 6 select-window -t 6
|
||||
bind-key 7 select-window -t 7
|
||||
bind-key 8 select-window -t 8
|
||||
bind-key 9 select-window -t 9
|
||||
bind-key 0 select-window -t 10
|
||||
|
||||
# window / tile navigation settings from $DOTWRYN/config/tmux.conf
|
||||
# but default to nested session rather than host
|
||||
|
||||
bind -n M-h send-keys C-b Left
|
||||
bind -n M-H select-pane -L
|
||||
bind -n M-j send-keys C-b Down
|
||||
bind -n M-J select-pane -D
|
||||
bind -n M-k send-keys C-b Up
|
||||
bind -n M-K select-pane -U
|
||||
bind -n M-l send-keys C-b Right
|
||||
bind -n M-L select-pane -R
|
||||
|
||||
bind -n M-Left send-keys C-b C-Left
|
||||
bind -n M-S-Left resize-pane -L 2
|
||||
bind -n M-Down send-keys C-b C-Down
|
||||
bind -n M-S-Down resize-pane -D 2
|
||||
bind -n M-Up send-keys C-b C-Up
|
||||
bind -n M-S-Up resize-pane -U 2
|
||||
bind -n M-Right send-keys C-b C-Right
|
||||
bind -n M-S-Right resize-pane -R 2
|
||||
|
||||
bind-key -n M-Tab send-keys C-b n
|
||||
bind-key -n M-BTab send-keys C-b p
|
||||
|
||||
bind-key -n M-Enter send-keys C-b c
|
||||
|
||||
bind-key -n M-v send-keys M-v
|
||||
bind-key -n M-b send-keys M-b
|
||||
bind-key -n M-q send-keys M-q
|
||||
bind-key -n M-z send-keys M-z
|
||||
bind-key -n M-w send-keys C-M-w
|
||||
|
||||
bind-key -n M-Q kill-session
|
||||
|
||||
unbind-key C-b
|
||||
bind-key C-b send-prefix
|
||||
set-option -g prefix M-s
|
||||
bind-key M-s send-keys M-s
|
||||
|
||||
bind-key -n M-Q kill-session
|
||||
|
||||
# force reload now
|
||||
bind-key -n M-R send-keys -t omni-manager ENTER
|
Reference in New Issue
Block a user