[tmux] Add remote session nesting

If calling a tmux session within a tmux session, pressing F12 will
toggle between sending commands to the outer or inner session. If the
outer session is inactive its status-bar will be slightly greyed out to
show that no commands will reach it. If the inner session is a remote
ssh session, it will set some additional stylings for the status-bar to
further differentiate the two.
This commit is contained in:
Marty Oehme 2019-07-27 12:29:47 +00:00
parent c8cd9bf11e
commit 7fab7dd6fb
4 changed files with 95 additions and 11 deletions

39
.local/bin/test/tm.bats Normal file
View file

@ -0,0 +1,39 @@
setup() {
fut="$BATS_TEST_DIRNAME/../tm"
}
teardown() {
rm -rf $BATS_TMPDIR/xdg/*
}
@test "runs correctly if invoked without arguments" {
run $fut
echo "$BATS_TEST_DIRNAME/../tm"
[ "$status" -eq 0 ]
}
@test "Errors out if passed a non-existent session file (and some session name)" {
run $fut IDONT_EXIST_HERE.session sessionname
echo "STATUS: $output"
[ "$status" -eq 1 ]
echo "OUTPUT: $output"
[ "$output" = "can not source session file: 'IDONT_EXIST_HERE.session' does not exist." ]
}
@test "Runs without errors when session file exists in PWD" {
touch $PWD/exists.session
run $fut exists.session sessionname
rm $PWD/exists.session
[ "$status" -eq 0 ]
}
@test "Runs without errors when session file exists in XDG_CONFIG_HOME/tmux/sessions/" {
export XDG_CONFIG_HOME=$BATS_TMPDIR
mkdir -p $XDG_CONFIG_HOME/tmux/sessions
sesdir=$XDG_CONFIG_HOME/tmux/sessions
touch $sesdir/exists.session
run $fut exists.session sessionname
[ "$status" -eq 0 ]
}

View file

@ -3,7 +3,7 @@
# Attach to a tmux session, or create it if it doesnt exist
if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
cat <<-EOF
cat <<-EOF
Usage: $0 [FILE] [NAME]
Attach to an existing tmux session, or bootstrap a new session.
@ -54,7 +54,7 @@ _not_in_tmux() {
}
_session_exists() {
if tmux has-session -t "$session_name" > /dev/null 2>&1; then
if tmux has-session -t "$session_name" >/dev/null 2>&1; then
true
else
false
@ -67,13 +67,13 @@ _create_detached_session() {
_load_env_session_file() {
if [ -f ./.tmux.session ]; then
xargs -L1 tmux < ./.tmux.session
xargs -L1 tmux <./.tmux.session
fi
}
_load_session_file() {
if [ -f "$session_file" ]; then
xargs -L1 tmux < "$session_file"
xargs -L1 tmux <"$session_file"
fi
}
@ -92,7 +92,7 @@ _set_session_vars() {
# set up session name and config file - if passed in as arguments
if [ "$#" -eq 2 ]; then
if ! _file_exists "$1" && ! _file_exists "${session_dir}${1}"; then
echo "$0: can not source configuration file: '${1}' does not exist."
echo >&2 "can not source session file: '${1}' does not exist."
exit 1
fi
_set_session_file_path "$1"
@ -106,12 +106,12 @@ _set_session_vars() {
# set default session name if none was passed
if [ -z "$session_name" ]; then
# only if we have a filename should we append it to the session name
if [ -n "$session_file" ]; then
fname=/$(basename "$session_file" .session)
fi
# default to parent directory name / config file name for the session
session_name=$(basename "$PWD" | sed 's/[^a-zA-Z0-9\-]//g' | tr . -)"$fname"
# only if we have a filename should we append it to the session name
if [ -n "$session_file" ]; then
fname=/$(basename "$session_file" .session)
fi
# default to parent directory name / config file name for the session
session_name=$(basename "$PWD" | sed 's/[^a-zA-Z0-9\-]//g' | tr . -)"$fname"
fi
}