summaryrefslogtreecommitdiff
path: root/src/spreed.c
blob: cd9d24ae9126a2e63d73b6c66dac5d3d7c17c881 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#if 0
${CC:-gcc} -s -O2 -std=c99 -Wall -o ${SPREED_INSTALL_DIR:-.}/spreed src/spreed.c
exit
#endif

#define _POSIX_C_SOURCE 200809L

#include <locale.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <wchar.h>
#include <wctype.h>

#define SPRD_VER   L"0.1"
#define CLR_FG     L"\033[31m"
#define CLR_RESET  L"\033[0m"
#define SPRD_CLOCK CLOCK_MONOTONIC
#define MS_PER_MIN (60ll * 1000000000ll)

#define SPRD_CLOCK_DIFF(t1, t2) ((((int64_t)t1.tv_sec * 1000000000ll) + (int64_t)t1.tv_nsec) - (((int64_t)t2.tv_sec * 1000000000ll) + (int64_t)t2.tv_nsec))

const uint8_t orps[9] = {0,0,0,1,1,2,2,2,2};

uint8_t get_optimal_recognition_point(size_t len) {
	if (len >= 10) return 3;
	return orps[len-1];
}

int main(int argc, char *argv[])
{
	struct timespec ts_last, ts_current, ts_sleep;
	wchar_t word[37];
	wchar_t wc;
	char *str;
	int64_t cooldown, wpm_ns;
	int c;
	char chararr[4];
	uint8_t u, uu, ichar, iword, b_word_break;

	setlocale(LC_ALL, "");
	fwide(stdout, 1);

	clock_getres(SPRD_CLOCK, &ts_last);
	memset(word, 0, 37 * sizeof(wchar_t));
	cooldown = 0;
	wpm_ns = MS_PER_MIN / 250;
	memset(chararr, 0, sizeof(chararr));
	ichar = 0;
	iword = 0;
	b_word_break = 0;

	if (argc > 1) {
		if (strstr(argv[1], "-v") || strstr(argv[1], "--v") || strstr(argv[1], "-V") || strstr(argv[1], "--V")) {
			fwprintf(stdout, "spreed version "SPRD_VER"\n");
			return 0;
		}
	}

	str = getenv("SPREED_WPM");
	if (argc > 1) {
		str = argv[1];
	}
	if (str != NULL) {
		wpm_ns = strtoll(str, NULL, 10);
		if (wpm_ns == 0) {
			fwprintf(stdout, L"Error: parsed '%s' as %ll, setting to default of 250\n", str, wpm_ns);
			wpm_ns = 250;
		} else if (wpm_ns > (60 * 30)) {
			wpm_ns = 60 * 30;
			fwprintf(stdout, L"Limiting to %i wpm (30 fps)\n", 60 * 30);
		}
		wpm_ns = MS_PER_MIN / wpm_ns;
	}

	fwprintf(stdout, L"Clock (%i) accuracy: %ll.%09lls\n", SPRD_CLOCK, ts_last.tv_sec, ts_last.tv_nsec);

	fwprintf(stdout, L"          V\n");
	clock_gettime(SPRD_CLOCK, &ts_current);
	ts_last = ts_current;
	ts_last.tv_sec -= 60;
	do {
		while (cooldown > 0) {
			clock_gettime(SPRD_CLOCK, &ts_current);
			cooldown -= SPRD_CLOCK_DIFF(ts_current, ts_last);
			if (cooldown <= 0) {
				break;
			}
			ts_sleep.tv_sec = 0;
			ts_sleep.tv_nsec = cooldown > 1000000000ll ? 999999999ll : cooldown;
			nanosleep(&ts_sleep, NULL);
		}
		b_word_break = 0;
		c = getchar();
		if (c == EOF) {
			b_word_break = iword > 0;
			goto END_OF_WORD;
		}
		chararr[ichar++] = (char)c;

		switch(mbrtowc(&wc, chararr, 4, NULL)) {
			case 0:
				// null char
				if (iword == 0 && ichar == 1) {
					ichar = 0;
					memset(chararr, 0, 4 * sizeof(char));
					continue;
				} else {
					b_word_break = 1;
					goto END_OF_WORD;
				}
			case (size_t)-2:
				// incomplete wchar_t, keep reading bytes
				continue;
			case (size_t)-1:
				if (ichar == 4) {
					// emojis return (size_t)-1 until we have all the bytes
					fwprintf(stderr, L"\nmbrtowc encoding error\n");
					return 1;
				}
				continue;
		}

		// not a printable character, swallow
		if (!iswprint(wc) && iword == 0) {
			ichar = 0;
			memset(chararr, 0, 4 * sizeof(char));
			continue;
		}

		if (iswspace(wc)) {
			if (iword == 0) {
				ichar = 0;
				memset(chararr, 0, 4 * sizeof(char));
				continue;
			}
			b_word_break = 1;
		}

END_OF_WORD:
		ichar = 0;
		memset(chararr, 0, 4 * sizeof(char));

		if (b_word_break != 0 || iword >= 37) {
			if (iword == 0) {
				fwprintf(stderr, L"\n0 length word\n");
				return 2;
			}
			ichar = get_optimal_recognition_point(iword);
			uu = 0;
			putwchar('\r');
			for (u = 0; u < 48; ++u) {
				if (u < 10-ichar) {
					putwchar(L' ');
					continue;
				}
				if (uu < iword) {
					if (u == 10) {
						fwprintf(stdout, L"%ls", CLR_FG);
					}
					putwchar(word[uu++]);
					if (u == 10) {
						fwprintf(stdout, L"%ls", CLR_RESET);
					}
					continue;
				}
				putwchar(L' ');
			}

			fflush(stdout);

			while (iword > 0) {
				if (iswalnum(word[iword-1])) {
					break;
				}
				if (word[iword-1] == L'.' || word[iword-1] == L'!' || word[iword-1] == L'?') {
					cooldown += wpm_ns;
					break;
				}
				iword--;
			}
			cooldown += wpm_ns;

			iword = 0;
			memset(word, 0, 37 * sizeof(wchar_t));
			ts_last = ts_current;
		}

		ichar = 0;
		if (!iswspace(wc)) {
			word[iword++] = wc;
		}

	} while (c != EOF);

	putwchar('\n');

	return 0;
}