~jonn/shmux

shmux/shmux -rwxr-xr-x 2.1 KiB
1694097eJonn Problem: more than one dot in the project name crashes shmux 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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