~skiqqy/bin

bin/ws -rwxr-xr-x 1.4 KiB
e187890dStephen Cochrane Changed echo to no op a day 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
# Simple tmux work session builder/launcher
# Written by skiqqy

argv=( "$@" )

usage ()
{
	cat << EOF
ws ~ skiqqy's tmux workspace

Usage: ws [options] WINDOW-NAME

options
-------

-h         Shows this message.
-s NAME    Specify the session name.
-a         Attach to the session.
EOF
	exit "$1"
}

# Build my tmux workspace
# Usage: build "session:pain"
build ()
{
	tmux split-window -t "$1" -h -p 50
	tmux send-keys -t "$1.0" 'ranger' Enter
	tmux send-keys -t "$1.1" '' Enter
	tmux select-pane -t "$1.0"
}

main ()
{
	opts='hs:a'
	sesh=PW
	attach=false

	# Get the session name, whilst checking it was actually passed
	[ "$#" -eq 0 ] && usage 1
	wn=$(sed -E 's|(-s .+ )\|(-.* )||g' <<< "$@")
	if [[ $wn =~ ^- ]]
	then
		# Last argument is a parameter, hence WINDOW-NAME was not passed
		usage 1
	fi

	while getopts "$opts" opt
	do
		case "$opt" in
			h)
				usage 0
				;;
			s)
				sesh="$OPTARG"
				;;
			a)
				attach=true
				;;
			*)
				usage 1
				;;
		esac
	done

	if tmux has-session -t "$sesh:$wn" > /dev/null 2>&1
	then
		# Session exists, attach
		echo "$sesh:$wn already exists, skipping."
	elif tmux has-session -t "$sesh" > /dev/null 2>&1
	then
		echo "Creating window."
		tmux new-window -d -n "$wn" -t "$sesh"
		build "$sesh:$wn"
	else
		echo "Session DNE, creating $sesh:$wn"
		tmux new-session -d -s "$sesh"
		tmux rename-window -t "$sesh:1" "$wn"
		build "$sesh:$wn"
	fi

	if "$attach"
	then
		tmux a -t "$sesh:$wn"
	fi
}

main "$@"