summaryrefslogtreecommitdiff
path: root/.local/bin/launch-gtypist
blob: c5f452f7e86e66710a8d22d02500a93b5b440a2f (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh


help() {
	echo "launch-gtypist - launch gtypist in fullscreen

USAGE:
	launch-gtypist [OPTION]...

OPTIONS:
	-l LESSON start gtypist lesson
	-f FILE   load lesson from a file
	-e %      max error percentage
	-v        verbose output
	-h        show this help message

LESSON can optionally be followed by the exercise number seprated by a colon.
Example - Q2:3, which means 3rd execercise of lesson Q2.
If LESSON is provided through the command line along with a file with -f
option then the file will ignored"
}


while getopts 'l:f:e:vh' o; do case "$o" in
	l) use_file=1; current="$OPTARG" ;;
	f) use_file=0; file="$OPTARG" ;;
	e) export GTYPIST_OPTS="$GTYPIST_OPTS --max-error=$OPTARG" ;;
	v) vflag=0 ;;
	h) help; exit ;;
	*) echo "launch-gtypist: invalid option -- '${OPTARG}'"
		echo 'Try `launch-gtypist -h` for more information.' >&2; exit 1 ;;
esac done
shift $((OPTIND - 1))

msg() { [ "$vflag" = 0 ] && printf ':: %s\n' "$@" >&2; true; }
notify() {
	[ -n "$DISPLAY" ] && notify-send 'launch-gtypist' "$@"
	printf '%s\n' "$@"
}

if [ "$use_file" = 0 ]; then
	msg "Getting lesson from file: $file"
	current="$(sed -En 's/^gtypist lesson\s*-\s*(.*)$/\1/p' "$file")" ||
		exit 1
	[ -z "$current" ] &&
		printf 'launch-gtypist: current gtypist lesson not found in:\n\t%s\n' \
		"$file" && exit 1
fi

[ -n "$1" ] && current="$1" && shift
[ "${current#*:}" != "$current" ] && lesson="${current%:*}" &&
	exercise="${current##*:}" || lesson="$current"
[ -n "$lesson" ] && {
	msg "Gtypist lesson name: $lesson"
	export GTYPIST_OPTS="$GTYPIST_OPTS --start-label=$lesson"
}

tmuxx() { [ -z "$TMUX" ] && return 1; tmux "$@"; }


shift_exercise() {
	msg "Shifting to exercise $exercise"
	sleep 1

	keys() {
		if [ -n "$TMUX" ]; then
			for key in "$@"; do
				tmux send-keys -t gtypist "$key";
			done
		else
			xdotool key --clearmodifiers --delay 100 "$@"
		fi
	}

	for i in $(seq "$((exercise-1))"); do
		if [ "$i" -gt 1 ]; then
			keys Escape n
		else
			keys space space space space space Escape Escape n
		fi
	done
}


msg "Switcing to default keymap"
# kmonad-manage stop
remap -x

msg "Waiting for terminal columns to get more than 100..."
while [ "$(tput cols)" -lt 100 ]; do sleep 0.5; done
tmuxx rename-window gtypist
[ -n "$exercise" ] && shift_exercise &
msg "	>>> Gtypist started."
msg "     gtypist $GTYPIST_OPTS $*"
gtypist $GTYPIST_OPTS $@
msg "	<<< Gtypist exited."
tmuxx set-window-option automatic-rename on

msg "Switcing to user keymap"
# kmonad-manage start
remap