144 lines
3.1 KiB
Plaintext
144 lines
3.1 KiB
Plaintext
|
#!/usr/bin/env bash
|
||
|
#
|
||
|
# Auto attach to tmux
|
||
|
#
|
||
|
|
||
|
CONF_FILE=$HOME/.autoattach.conf
|
||
|
|
||
|
###########################
|
||
|
|
||
|
function write_setting
|
||
|
{
|
||
|
if ! sed -i -E "s/^$1=.*/$1=$2/" $CONF_FILE 2> /dev/null; then
|
||
|
echo "$1=$2" >> $CONF_FILE
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
|
||
|
function attach_session
|
||
|
{
|
||
|
if [ -n "${TMUX}" ]; then
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
tmux attach -t $1 2> /dev/null
|
||
|
|
||
|
# Check for exit status
|
||
|
if [ $? -gt 0 ]; then
|
||
|
echo "There is no tmux session '$1'"
|
||
|
if [ -z "${AUTO_CREATE}" ]; then
|
||
|
read -p "Do you want me to auto create sessions by default? (y/n): "
|
||
|
if [ "${REPLY}" = "y" ]; 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
|
||
|
echo 'Nothing to do, exiting...'
|
||
|
exit 1
|
||
|
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
function usage
|
||
|
{
|
||
|
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
|
||
|
case "$OSTYPE" in
|
||
|
freebsd* )
|
||
|
echo "Freebsb detected!!"
|
||
|
echo "Long options is disabled!!"
|
||
|
OPTS=`getopt chgs: $*`
|
||
|
set_opts_exit_status=$?
|
||
|
PLATFORM='Freebsd' ;;
|
||
|
#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
|
||
|
|
||
|
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
|