mirror of
https://github.com/pnx/dotfiles
synced 2026-06-18 12:00:02 +02:00
scripts/tmuxs: more improvements
This commit is contained in:
parent
5b67d85598
commit
5b322780fc
3 changed files with 32 additions and 5 deletions
24
scripts/tmuxs/helpers.sh
Normal file
24
scripts/tmuxs/helpers.sh
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
function current_session {
|
||||||
|
tty=$(tty)
|
||||||
|
for s in $(tmux list-sessions -F '#{session_name}' 2>/dev/null); do
|
||||||
|
tmux list-panes -F '#{pane_tty} #{session_name}' -t "$s"
|
||||||
|
done | grep "$tty" | awk '{print $2}'
|
||||||
|
}
|
||||||
|
|
||||||
|
function list_sessions {
|
||||||
|
tmux list-sessions -F "#{session_activity},#{session_id},#{session_name}" \
|
||||||
|
| sort -nr | cut -d, -f2,3 | format_session_string
|
||||||
|
}
|
||||||
|
|
||||||
|
# Given the input "#{session_id},#{session_name}" from tmux ($7,my_session for example)
|
||||||
|
# will output <id>: <name (7: my_session)
|
||||||
|
function format_session_string {
|
||||||
|
sed -r 's/^\$([[:digit:]]+),/\1: /'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Extract the name part from the input (format "<id>: <name>")
|
||||||
|
function get_session_name {
|
||||||
|
sed 's/[[:digit:]]\+\:\s//'
|
||||||
|
}
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
BASE_PATH=$(dirname $(readlink -f $BASH_SOURCE))
|
||||||
|
source ${BASE_PATH}/helpers.sh
|
||||||
|
|
||||||
SESSION=$(echo $@ | sed 's/: .*//g')
|
SESSION=$(echo $@ | get_session_name)
|
||||||
CURRENT=$(tmux list-sessions -F "#{session_name}" -f "#{session_attached}" | head -n 1)
|
|
||||||
|
|
||||||
# If we are killing the current session. Move to another
|
# If we are killing the current session. Move to another
|
||||||
if [ "$CURRENT" == "$SESSION" ]; then
|
if [ "$(current_session)" == "$SESSION" ]; then
|
||||||
NEXT=$(tmux list-sessions -F "#{session_name}" -f "#{==:#{session_attached},0}" | head -n 1)
|
NEXT=$(list_sessions | grep -v "${SESSION}" | head -n 1 | get_session_name)
|
||||||
if [ ! -z "$NEXT" ]; then
|
if [ ! -z "$NEXT" ]; then
|
||||||
tmux switch -t "${NEXT}"
|
tmux switch -t "${NEXT}"
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
BASE_PATH=$(dirname $(readlink -f $BASH_SOURCE))
|
BASE_PATH=$(dirname $(readlink -f $BASH_SOURCE))
|
||||||
|
source ${BASE_PATH}/helpers.sh
|
||||||
source ${BASE_PATH}/.env
|
source ${BASE_PATH}/.env
|
||||||
|
|
||||||
# Scripts
|
# Scripts
|
||||||
|
|
@ -9,7 +10,8 @@ KILL_SCRIPT=${BASE_PATH}/kill.sh
|
||||||
|
|
||||||
if [[ $# -eq 1 ]]; then
|
if [[ $# -eq 1 ]]; then
|
||||||
if [[ "$1" == "-a" ]]; then
|
if [[ "$1" == "-a" ]]; then
|
||||||
selected=$($LIST_SESSIONS_SCRIPT | $FZF --bind "ctrl-d:execute(${KILL_SCRIPT} {})+reload(${LIST_SESSIONS_SCRIPT})" --border-label=Session | sed 's/: .*//g')
|
selected=$($LIST_SESSIONS_SCRIPT | $FZF --bind "ctrl-d:execute(${KILL_SCRIPT} {})+reload(${LIST_SESSIONS_SCRIPT})" \
|
||||||
|
--border-label=Session | get_session_name)
|
||||||
if [ ! -z "$selected" ]; then
|
if [ ! -z "$selected" ]; then
|
||||||
${BASE_PATH}/new.sh $selected
|
${BASE_PATH}/new.sh $selected
|
||||||
fi
|
fi
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue