From 1b3743469547cb3d91ae0d789df8714af7e96301 Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Fri, 27 Mar 2020 21:17:45 +1300 Subject: [PATCH] helper to initialise session with multiple windows --- .bin/mtmux | 91 +++++++++++++++++++++++++++++++++++++++++++++++ .gitignore.d/tmux | 3 +- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100755 .bin/mtmux diff --git a/.bin/mtmux b/.bin/mtmux new file mode 100755 index 0000000..bee0bc8 --- /dev/null +++ b/.bin/mtmux @@ -0,0 +1,91 @@ +#!/bin/sh +# +# mtmux — initialise tmux session with multiple windows +# +# Usage: +# 1. mtmux [-n name] [-c cwd] -- cmd1 . windowname: cmd2 . shellname: +# +# Spawns a tmux session, optionally named "name", and optionally with the +# working directory set to "cwd", with three windows, one running cmd1, +# one running cmd2 and named "windowname", and one named "shellname" +# running the default command. +# +# 2. mtmux -n name -w 2 +# +# Attach to session "name" and switch to window 2 +# +# Copyright © 2020 martin f. krafft +# Released under the WTFPL. +# +set -eu + +warn() { + echo >&2 "$@" +} + +err() { + local exitcode; exitcode="$1"; shift + warn "$@" + exit $exitcode +} + +for i in "$@"; do + case "${state:-}:$i" in + (:-c) state=cwd;; + (cwd:*) + [ -z "${cwd:-}" ] && cwd="$i" || err 1 "Option -c already specified" + state= + ;; + + (:-n) state=name;; + (name:*) + [ -z "${name:-}" ] && name="$i" || err 1 "Option -n already specified" + state= + ;; + + (:-w) state=win;; + win:*) + [ -z "${window:-}" ] && window="$i" || err 1 "Option -w already specified" + state= + ;; + + (:--) shift; set -- "$@"; break + esac + shift 2>/dev/null || break +done + +args= +if [ -n "${window:-}" ]; then + + if [ -n "${cwd:-}" ]; then + warn "Both -w and -c don't make sense, ignoring -c" + cwd= + fi + if [ -n "$@" ]; then + warn "Ignoring commands specified with -w" + set -- + fi + if [ -z "$name" ]; then + err 1 "Missing option -n, required with -w" + fi + + args="new -t $name \; select-window -t $window" + +else + + subcmd="new${name:+ -s $name}" + for i in "$@" +; do + case "$i" in + (*:) wname="${i%:}";; + (+|.|\;) + args="${args}${subcmd}${cwd:+ -c $cwd}${wname:+ -n $wname}${cmd:-} \\; " + subcmd="neww -d" + cmd= + ;; + (*) cmd="${cmd:-} $i";; + esac + done + +fi + +eval exec tmux $args diff --git a/.gitignore.d/tmux b/.gitignore.d/tmux index 60810fd..c398bca 100644 --- a/.gitignore.d/tmux +++ b/.gitignore.d/tmux @@ -1,4 +1,5 @@ * -!/.gitignore.d +!/.bin/mtmux +!/.bin/tmux !/.gitignore.d/tmux !/.tmux.conf -- 2.39.5