Extend tm script to allow session file loading
With the `tm script.conf session-name` format, or `tm script.conf` format, it now allows setting up pre-defined session setups to be loaded by tmux.
This commit is contained in:
parent
ea7e3635fe
commit
1ac168ccb0
1 changed files with 104 additions and 26 deletions
130
.local/bin/tm
130
.local/bin/tm
|
@ -2,42 +2,120 @@
|
||||||
#
|
#
|
||||||
# Attach to a tmux session, or create it if it doesnt exist
|
# Attach to a tmux session, or create it if it doesnt exist
|
||||||
|
|
||||||
if [ -e $XDG_CONFIG_HOME/tmux/tmux.conf ]; then
|
if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
|
||||||
start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf"
|
cat <<-EOF
|
||||||
else
|
Usage: $0 [FILE] [NAME]
|
||||||
start=tmux
|
Attach to an existing tmux session, or bootstrap a new session.
|
||||||
|
|
||||||
|
If no session exists which fits the criteria, this will create a new one and
|
||||||
|
load it up obeying the commands contained in the FILE argument passed in. It
|
||||||
|
must contain commands which are tmux-command compatible since they will be
|
||||||
|
passed to tmux unmodified.
|
||||||
|
|
||||||
|
It will load the default tmux.conf in the following order:
|
||||||
|
- XDG_CONFIG_HOME/tmux/tmux.conf (usually ~/.config/tmux/tmux.conf)
|
||||||
|
- ~/.tmux.conf
|
||||||
|
- /etc/tmux.conf
|
||||||
|
Afterwards, it will apply the contents of the FILE argument passed into the command.
|
||||||
|
|
||||||
|
If no FILE argument is passed it will create an empty session, or attach itself to
|
||||||
|
the session specified.
|
||||||
|
|
||||||
|
If no NAME argument is passed it will automatically create a session name from the
|
||||||
|
FILE argument passed.
|
||||||
|
EOF
|
||||||
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
path_name="$(basename "$PWD" | tr . -)"
|
# Make tmux obey the XDG specification on startup
|
||||||
curr_dir=${1-$path_name}
|
if [ -e "$XDG_CONFIG_HOME/tmux/tmux.conf" ]; then
|
||||||
session_name=${1:-$curr_dir}
|
start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf"
|
||||||
|
else
|
||||||
|
start=tmux
|
||||||
|
fi
|
||||||
|
|
||||||
|
_file_exists() {
|
||||||
|
if [ -f "$1" ]; then
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
_not_in_tmux() {
|
_not_in_tmux() {
|
||||||
[ -z "$TMUX" ]
|
[ -z "$TMUX" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
_session_exists() {
|
_session_exists() {
|
||||||
$start has-session -t "$session_name"
|
if tmux has-session -t "$session_name" > /dev/null 2>&1; then
|
||||||
}
|
true
|
||||||
|
|
||||||
_create_detached_session() {
|
|
||||||
(TMUX='' $start new-session -Ad -s "$session_name")
|
|
||||||
}
|
|
||||||
|
|
||||||
_get_session_name() {
|
|
||||||
#${1:-$session_name}
|
|
||||||
return "blubb"
|
|
||||||
}
|
|
||||||
|
|
||||||
_attach_or_create() {
|
|
||||||
if _not_in_tmux; then
|
|
||||||
$start new-session -As "$session_name"
|
|
||||||
else
|
else
|
||||||
if ! _session_exists; then
|
false
|
||||||
_create_detached_session
|
|
||||||
fi
|
|
||||||
$start switch-client -t "$session_name"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_create_detached_session() {
|
||||||
|
($start new-session -d -s "$session_name")
|
||||||
|
}
|
||||||
|
|
||||||
|
_load_session_file() {
|
||||||
|
xargs -L1 tmux < "$session_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
_set_session_vars() {
|
||||||
|
if [ "$#" -eq 2 ]; then
|
||||||
|
session_file=${1}
|
||||||
|
if ! _file_exists "$session_file"; then
|
||||||
|
echo "$0: can not source configuration file: '${session_file}' does not exist."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
session_name=${2}
|
||||||
|
elif [ "$#" -eq 1 ]; then
|
||||||
|
if _file_exists "${1}"; then
|
||||||
|
session_file=${1}
|
||||||
|
else
|
||||||
|
session_name=${1}
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 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" .conf)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
_attach_or_create() {
|
||||||
|
# first create a new session if one doesn't exist
|
||||||
|
if ! _session_exists; then
|
||||||
|
_create_detached_session
|
||||||
|
_load_session_file
|
||||||
|
fi
|
||||||
|
|
||||||
|
# then attach or switch to the session
|
||||||
|
if _not_in_tmux; then
|
||||||
|
$start attach -t "$session_name"
|
||||||
|
else
|
||||||
|
$start switch-client -t "$session_name"
|
||||||
|
fi
|
||||||
|
|
||||||
|
_unset_functions
|
||||||
|
}
|
||||||
|
|
||||||
|
_unset_functions() {
|
||||||
|
unset _set_session_vars
|
||||||
|
unset _attach_or_create
|
||||||
|
unset _load_session_file
|
||||||
|
unset _create_detached_session
|
||||||
|
unset _session_exists
|
||||||
|
unset _not_in_tmux
|
||||||
|
unset _file_exists
|
||||||
|
}
|
||||||
|
|
||||||
|
_set_session_vars "$@"
|
||||||
_attach_or_create
|
_attach_or_create
|
||||||
|
exit 0
|
||||||
|
|
Loading…
Reference in a new issue