From 1ac168ccb0742d240d3ad218ea2a7ab2fe94f017 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 10 Jul 2019 12:19:02 +0200 Subject: [PATCH 1/4] 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. --- .local/bin/tm | 130 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 104 insertions(+), 26 deletions(-) diff --git a/.local/bin/tm b/.local/bin/tm index 21f0250..8e42a1a 100755 --- a/.local/bin/tm +++ b/.local/bin/tm @@ -2,42 +2,120 @@ # # Attach to a tmux session, or create it if it doesnt exist -if [ -e $XDG_CONFIG_HOME/tmux/tmux.conf ]; then - start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf" -else - start=tmux +if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then +cat <<-EOF + Usage: $0 [FILE] [NAME] + 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 -path_name="$(basename "$PWD" | tr . -)" -curr_dir=${1-$path_name} -session_name=${1:-$curr_dir} +# Make tmux obey the XDG specification on startup +if [ -e "$XDG_CONFIG_HOME/tmux/tmux.conf" ]; then + 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() { [ -z "$TMUX" ] } _session_exists() { - $start has-session -t "$session_name" -} - -_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" + if tmux has-session -t "$session_name" > /dev/null 2>&1; then + true else - if ! _session_exists; then - _create_detached_session - fi - $start switch-client -t "$session_name" + false 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 +exit 0 From 6fcf2a750eaf655f6bed6e4dee68a60662226a7f Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 10 Jul 2019 13:09:17 +0200 Subject: [PATCH 2/4] Always execute session setup Even if session already exists, if we explicitly pass along a session file, we should execute it. --- .local/bin/tm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.local/bin/tm b/.local/bin/tm index 8e42a1a..fdb6fef 100755 --- a/.local/bin/tm +++ b/.local/bin/tm @@ -93,9 +93,10 @@ _attach_or_create() { # first create a new session if one doesn't exist if ! _session_exists; then _create_detached_session - _load_session_file fi + _load_session_file + # then attach or switch to the session if _not_in_tmux; then $start attach -t "$session_name" From 27af1cc7fa02236d22af4b59820faa7f1462597e Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 10 Jul 2019 13:21:44 +0200 Subject: [PATCH 3/4] Add looking for .tmux.session files in pwd If no explicit session file is passed in and a new session is being created tm will look for a .tmux.session file in the pwd and use that to set up the session. --- .local/bin/tm | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.local/bin/tm b/.local/bin/tm index fdb6fef..a2de614 100755 --- a/.local/bin/tm +++ b/.local/bin/tm @@ -10,7 +10,8 @@ cat <<-EOF 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. + passed to tmux unmodified. If a session exists and a valid configuration FILE is + passed, it will attach itself to the session and execute the session setup inside. It will load the default tmux.conf in the following order: - XDG_CONFIG_HOME/tmux/tmux.conf (usually ~/.config/tmux/tmux.conf) @@ -23,6 +24,12 @@ cat <<-EOF If no NAME argument is passed it will automatically create a session name from the FILE argument passed. + + If creating a new session without passing in a FILE argument, and the current directory + contains a .tmux.session file, it will automatically use that to set up the session. + + By default, it also looks for valid session files in XDG_CONFIG_HOME/tmux/sessions/ + This path can be changed by setting the TM_SESSION_PATH environment variable. EOF exit 0 fi @@ -58,6 +65,10 @@ _create_detached_session() { ($start new-session -d -s "$session_name") } +_load_env_session_file() { + xargs -L1 tmux < ./.tmux.session +} + _load_session_file() { xargs -L1 tmux < "$session_file" } @@ -93,6 +104,10 @@ _attach_or_create() { # first create a new session if one doesn't exist if ! _session_exists; then _create_detached_session + # look for .tmux.session file if no file has been explicitly passed in + if ! _file_exists "$session_file"; then + _load_env_session_file + fi fi _load_session_file From 4e3cc2342a1662117e1855ec060d8041f0b47c02 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Wed, 10 Jul 2019 13:48:44 +0200 Subject: [PATCH 4/4] Add default session root dir to tm --- .local/bin/tm | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/.local/bin/tm b/.local/bin/tm index a2de614..02193f5 100755 --- a/.local/bin/tm +++ b/.local/bin/tm @@ -66,25 +66,41 @@ _create_detached_session() { } _load_env_session_file() { - xargs -L1 tmux < ./.tmux.session + if [ -f ./.tmux.session ]; then + xargs -L1 tmux < ./.tmux.session + fi } _load_session_file() { - xargs -L1 tmux < "$session_file" + if [ -f "$session_file" ]; then + xargs -L1 tmux < "$session_file" + fi +} + +_set_session_file_path() { + # Prefer local dir if file exists there, fall back to root dir + if _file_exists "${1}"; then + session_file="${1}" + elif _file_exists "${session_dir}${1}"; then + session_file="${session_dir}${1}" + fi } _set_session_vars() { + session_dir=${TM_SESSION_DIR:-"$XDG_CONFIG_HOME/tmux/sessions/"} + + # set up session name and config file - if passed in as arguments 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." + if ! _file_exists "$1" && ! _file_exists "${session_dir}${1}"; then + echo "sess dir: ${session_dir}${1}" + echo "$0: can not source configuration file: '${1}' does not exist." exit 1 fi + _set_session_file_path "$1" session_name=${2} elif [ "$#" -eq 1 ]; then - if _file_exists "${1}"; then - session_file=${1} - else + _set_session_file_path "$1" + if ! _file_exists "$session_file"; then session_name=${1} fi fi @@ -124,6 +140,7 @@ _attach_or_create() { _unset_functions() { unset _set_session_vars + unset _set_session_file_path unset _attach_or_create unset _load_session_file unset _create_detached_session