dotfiles/scripts/prefix/bin/x

107 lines
3.4 KiB
Text
Raw Normal View History

2016-03-26 14:40:29 +01:00
#!/bin/bash
# This bash script is designed to be called from a command prompt in your #
# favorite tiling window-manager or desktop environment or from any graphical #
# program running in X. #
# It handles wrapping the command you give it as an argument in X, that is #
# to say in your favorite terminal emulator. By default it also wraps your #
# command in a tmux terminal, either by creating a new session, or by adding a #
# window to a pre-existing session: #
# #
# Terminal [ Tmux ( Command ) ] #
# #
# Called without argument, it will start the command $DEFAULTSHELL. If a #
# terminal window has already been opened, it will NOT create a new one, but #
# add instead a new tmux window in the current tmux session, or if no session #
# is started, this script will start a new session. #
# An example of generated command after a simple call to this script with no #
# arguments: #
# termite -t Ephemere --geometry=1920x800 -e \ #
# "tmux new-window -t Ephemere '/bin/zsh' \; attach" #
# Here there was no terminal window already open, but a tmux session was #
# already created. #
# Default tmux session name
SESSIONNAME="Ephemere"
# Default shell
DEFAULTSHELL="/bin/zsh"
# Default window name
WINDOWNAME="Unnamed"
# Don't wrap these apps
NOWRAPL="irc"
# Don't start new window but attach these apps (if already started)
PREFERATTACH="mutt agenda playlist alsamixer rtorrent journalctl"
# You shouldn't need to tweak after this comment ###############################
TERMW=`xwininfo -tree -root | grep $SESSIONNAME`
TMUXS=`tmux ls | grep $SESSIONNAME`
# For reference, this works:
# tmux new-session -AD -s "Ephemere"\; neww -t "Ephemere" -n "somename" cmd
NEWSC="new-session -A -s $SESSIONNAME"
NEWWC="new-window -t $SESSIONNAME"
X="termite -t $SESSIONNAME --geometry=1920x800 -e"
XNW="termite -t LonesomeCowboy --geometry=1920x800 -e"
# Any args?
if [[ -n "$@" ]]
then
ARGS=$@
# Start the following CMD
CMD="$ARGS"
# Set the current window name
WINDOWNAME=$1
# Don't wrap some commands
if [[ $NOWRAPL =~ $1 ]]
then
exec $XNW \'$CMD\'
fi
# Check for a preexisting session
if [[ $PREFERATTACH =~ $1 ]]
then
PREEXSESSIONNAME=`tmux lsw -a | grep $1 | sed 's/^\(.*\):[0-9]:.*/\1/' | head -n 1`
echo "$WINDOWNAME is in session $PREEXSESSIONNAME"
fi
else
CMD="$DEFAULTSHELL"
fi
# Enrich the commands with a window name
NEWWC="$NEWWC -n $WINDOWNAME"
NEWSC="$NEWSC -n $WINDOWNAME"
# Is there already a window in a session with this $COMMANDNAME?
if [[ ! -z $PREEXSESSIONNAME ]]
then
CMD="tmux attach -t \"$PREEXSESSIONNAME:$WINDOWNAME\""
echo "I'd rather attach $PREEXSESSIONNAME"
# Is there already a session with this $SESSIONNAME?
elif [[ -z $TMUXS ]]
then
CMD="tmux $NEWSC '$CMD'"
else
CMD="tmux $NEWWC '$CMD' \\; attach -t $SESSIONNAME:$WINDOWNAME"
fi
# Should I start a new term?
if [[ -z $TERMW ]]
then
CMD="$X \"$CMD\""
fi
# Finally exec CMD
echo $CMD
eval $CMD