scripts/autoattach

161 lines
3.2 KiB
Plaintext
Raw Permalink Normal View History

2021-01-21 16:58:32 -05:00
#!/usr/bin/env bash
#
# Auto attach to tmux
#
CONF_FILE=$HOME/.autoattach.conf
###########################
function write_setting
2021-01-21 16:58:32 -05:00
{
if ! sed -i -E "s/^$1=.*/$1=$2/" $CONF_FILE 2> /dev/null; then
echo "$1=$2" >> $CONF_FILE
fi
}
2021-01-26 21:24:36 -05:00
function is_tmux_running
{
if [ -n "${TMUX}" ]; then
return 0
fi
return 1
}
function debug
{
if ! is_tmux_running; then
echo $1
fi
}
2021-01-21 16:58:32 -05:00
function attach_session
{
2021-01-26 21:24:36 -05:00
if is_tmux_running; then
exit 0
fi
2021-01-21 16:58:32 -05:00
tmux attach -t $1 2> /dev/null
2021-01-21 16:58:32 -05:00
# Check for exit status
if [ $? -gt 0 ]; then
echo "There is no tmux session '$1'"
if [ -z "${AUTO_CREATE}" ]; then
2021-02-10 15:18:24 -05:00
read -p "Do you want me to auto create sessions by default? [Y/n]: "
# convert to lowercase, then match
if [ "${REPLY,,}" = "y" -o -z "${REPLY}" ]; then
write_setting 'AUTO_CREATE' '1'
AUTO_CREATE=1
else
write_setting 'AUTO_CREATE' '0'
fi
fi
if [ "${AUTO_CREATE}" = "1" ]; then
tmux new-session -s $1 2> /dev/null
exit 0
fi
2021-02-10 15:18:24 -05:00
echo 'Nothing to do, exiting...'
exit 1
2021-01-21 16:58:32 -05:00
fi
}
function usage
{
2021-01-21 16:58:32 -05:00
echo "Usage: ${0}"
echo " -c | --clear"
echo " Delete configuration file"
echo " -s | --set auto_create on"
echo " Set configuration variable"
echo " auto_create on/off"
echo " Auto create session on start if session is missing"
echo " -h | --help"
echo " Show this usage"
exit 0
}
# Load configuration file
test -f $CONF_FILE && . $CONF_FILE
# Detect platform
PLATFORM='unknown'
unamestr=$(uname)
case "$unamestr" in
OpenBSD* )
2021-01-26 21:24:36 -05:00
debug "*BSD OS detected!!"
debug "Long options is disabled!!"
2021-01-21 16:58:32 -05:00
OPTS=`getopt chgs: $*`
set_opts_exit_status=$?
PLATFORM=$unamestr ;;
2021-01-21 16:58:32 -05:00
#if [ $? -ne 0 ]; then
# echo 'Usage: ...'
# exit 2
#fi
* )
OPTS=$(getopt -o chgs: -l clear,help,set:,get: -n 'autoattach' -- "$@")
set_opts_exit_status=$?
PLATFORM='Linux' ;;
esac
if [ "$set_opts_exit_status" -gt '0' ]; then
echo 'Failed to set command line arguments'
exit 1;
fi
eval set -- "$OPTS"
CLEAR=false
SET=false
while true; do
case "$1" in
-c | --clear ) CLEAR=true; shift ;;
-s | --set )
case "$2" in
'auto_create' )
case "$4" in
'on' )
echo 'Setting auto_create to 1'
write_setting 'AUTO_CREATE' '1'
shift;;
'off' )
echo 'Setting auto_create to 0'
write_setting 'AUTO_CREATE' '0'
shift;;
esac
2021-01-21 16:58:32 -05:00
shift;;
*)
echo "Failed to set configuration $2 $4"
exit 1
esac
exit 0
shift ;;
-h | --help ) usage; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
# Clear default window
if [ $CLEAR = true ]; then
echo "Deleting configuration..."
rm -f $CONF_FILE
exit 0
fi
if [ -n "${SET}" -a $SET != false ]; then
echo "Updating configuration..."
write_setting $SET
exit 0
fi
# Check if this a drop down terminal from env
if [ -n "${DROP_DOWN_TERMINAL}" ]; then
attach_session drop_down
else
attach_session regular
fi