From 14cb7d12c6497ebf53639cd04cc78f5f4fa8f38b Mon Sep 17 00:00:00 2001 From: Jonn Date: Sat, 14 Nov 2020 03:25:24 +0000 Subject: [PATCH] Problem: Shmux isn't readily available Solution: - Import its code in a separate repository - Clean up the code, insert some commentaries - Make sure it complies with `shellcheck` --- shmux | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 shmux diff --git a/shmux b/shmux new file mode 100755 index 0000000..dded156 --- /dev/null +++ b/shmux @@ -0,0 +1,73 @@ +#!/usr/bin/env bash +# shmux is shell tmuxinator for a generic project + +## Error codes +_err_no_target=1 +_tmux_session_not_found=1 + +_session="$(basename "$1")" +_session="${_session/./_}" + +tmux start-server || true + +# tmux has-session returns value that we'll refer to via $? +tmux has-session -t "$_session" +if [ "$?" -eq "$_tmux_session_not_found" ]; then # If there is no session +if [ -n "$1" ]; then # And we have an argument + # It's OK to fail + cd "$1" || true + _wd="$(pwd)" + _post_hook_maybe="" + + TMUX="$(tmux new-session -d -s "$_session" -n vim)" + tmux new-window -c "$_wd" -t "$_session:1" -n tasks + tmux new-window -c "$_wd" -t "$_session:2" -n git + tmux new-window -c "$_wd" -t "$_session:3" -n grep + tmux new-window -c "$_wd" -t "$_session:4" -n mon0 + tmux new-window -c "$_wd" -t "$_session:5" -n mon1 + + tmux send-keys -t "$_session:0" "vim ." C-m + tmux send-keys -t "$_session:2" "git log --graph --decorate --all -n7 | grep ." C-m + if [ -n "$_post_hook_maybe" ]; then + for x in {2..5}; do + tmux send-keys -t "$_session:$x" "$_post_hook_maybe" C-m + done + fi + + if [ -d tasks ]; then + _split="horizontal" + for x in tasks/*; do + # Run buffer hook in the current split + tmux send-keys -t "$_session:1" "$_post_hook_maybe" C-m + # Run the task the current split + tmux send-keys -t "$_session:1" "$x" C-m + # Create a new split, spiral layout + if [[ $_split == "horizontal" ]]; then + tmux split-window -h -t "$_session:1" + _split="vertical" + else + tmux split-window -t "$_session:1" + _split="horizontal" + fi + done + # Exit last split (it didn't have a task) + tmux send-keys -t "$_session:1" "exit" C-m + else + tmux send-keys -t "$_session:1" "$_post_hook_maybe" C-m + fi + + tmux select-window -t 0 +fi +fi + +# We can run tmux in detached mode with -d +if [ "$2" == "-d" ]; then + exit 0; +fi +# Otherwise we attach to the session if we're outside tmux +if [ -z "$TMUX" ]; then + tmux -u attach-session -t "$_session" +# And switch to the session if we're inside +else + tmux -u switch-client -t "$_session" +fi \ No newline at end of file