Show tmux sock instead of date in status-right
[khome.git] / home / .xlaunch
... / ...
CommitLineData
1#! /bin/bash
2
3set -e
4
5SOCK="$HOME/.xlaunch.sock"
6SESSION='xlaunch'
7TMUX="tmux -S $SOCK"
8LOG_FILE=~/var/log/xlaunch.log
9
10log() {
11 local -r fmt="$1"
12 shift
13
14 printf "${fmt}\n" $@ | twrap.sh >> "$LOG_FILE"
15}
16
17counter_next() {
18 local -r file="$_counter_file"
19
20 awk '{n = $1} END {print n + 1}' "$file" | sponge "$file"
21 cat "$file"
22}
23
24tmux_new_win() {
25 local -r command="$1"
26 local -r window_id=$(counter_next)
27 local -r window_name=$(basename "$command")
28 local -r pane=0
29
30 log \
31 '[debug] tmux window_id:"%s", window_name:"%s", command:"%s"' \
32 "$window_id" "$window_name" "$command"
33
34 $TMUX new-window -t "$SESSION" -n "$window_name"
35 $TMUX send-keys -t "$SESSION":"$window_id"."$pane" "$command" ENTER
36}
37
38launch_from_directory() {
39 local -r scripts_dir="$1"
40
41 log '[info] Looking for scripts in directory: "%s"' "$scripts_dir"
42 if test -d "$scripts_dir"
43 then
44 for script in "$scripts_dir"/*; do
45 log '[debug] Launching script: "%s"' "$script"
46 tmux_new_win "$script"
47 sleep 0.1 # TODO Find a way to block between starts instead.
48 done
49 else
50 log '[warning] Directory not found: %s' "$scripts_dir"
51 fi
52}
53
54_start() {
55 $TMUX new-session -d -s "$SESSION"
56 $TMUX set-option -gt "$SESSION" allow-rename off
57
58 _counter_file=$(mktemp)
59 launch_from_directory ~/.xlaunch.d
60 launch_from_directory ~/.xlaunch.d."$(hostname)"
61}
62
63_startx() {
64 log '[info] Starting X11'
65 _restart
66 log '[info] Launching dwm'
67 exec dwm
68}
69
70_stop() {
71 $TMUX kill-session -t "$SESSION"
72}
73
74_restart() {
75 _stop || true
76 _start
77}
78
79_attach() {
80 $TMUX attach -t "$SESSION"
81}
82
83main() {
84 local -r command="$1"
85
86 case "$command" in
87 #'' ) _startx;;
88 'startx' ) _startx;;
89 'start' ) _start;;
90 'stop' ) _stop;;
91 'restart' ) _restart;;
92 'attach' ) _attach;;
93 *)
94 echo "[error] Unknown command: \"$command\". Known: startx, start, stop, restart, attach."
95 exit 1;;
96 esac
97}
98
99main "$*"
This page took 0.026797 seconds and 4 git commands to generate.