-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
421 lines (310 loc) · 10.7 KB
/
main.cpp
File metadata and controls
421 lines (310 loc) · 10.7 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <algorithm>
#include <array>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <limits>
#include <print>
#include <sqlite3.h>
#include <sstream>
#include <stdexcept>
#include <string>
#define requirenargs(n) \
if (argc != n) { \
usage(); \
return 1; \
}
#define sizetint(s) \
s > static_cast<size_t>(std::numeric_limits<int>::max()) \
? throw std::overflow_error("size_t value too big for int") \
: static_cast<int>(s)
using namespace std::literals;
const auto DAY = 24h;
const std::array<std::chrono::duration<long long>, 8> INTERVALS = {
0 * DAY, 1 * DAY, 2 * DAY, 4 * DAY, 8 * DAY, 16 * DAY, 32 * DAY, 64 * DAY};
const auto NEW_CARDS_PER_DAY = 2;
const auto PROGRAM = "honoka"s;
const auto DATABASE = std::filesystem::path(getenv("HOME")) / ".local"s /
"share"s / PROGRAM / "data.db"s;
const auto IFS = getenv("IFS");
class Application {
public:
Application() : db_(nullptr) {
std::filesystem::create_directories(DATABASE.parent_path());
try {
if (int rc = sqlite3_open(DATABASE.c_str(), &db_); rc) {
throw std::runtime_error("Can't open database");
}
create();
} catch (...) {
sqlite3_close(db_);
throw;
}
}
~Application() { sqlite3_close(db_); }
auto add() -> void {
std::string line;
while (std::getline(std::cin, line)) {
const auto pos = line.find(IFS ? *IFS : ' ');
if (pos == std::string::npos) {
std::println(stderr, "Malformed line, separator not found: {}", line);
continue;
}
const auto front = line.substr(0, pos);
const auto back = line.substr(pos + 1);
add_one(front, back);
}
}
auto add(const std::string &front, const std::string &back) -> void {
add_one(front, back);
}
auto list() -> void {
const auto query =
"SELECT front, interval, unixepoch(updated_at) FROM cards"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
int rc = 0;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const std::string front = (const char *)sqlite3_column_text(stmt, 0);
int interval = sqlite3_column_int(stmt, 1);
long long updated_at = sqlite3_column_int64(stmt, 2);
if (needs_review(interval, updated_at)) {
std::println("{}", front);
}
}
if (rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't select from table");
}
sqlite3_finalize(stmt);
}
auto next() -> void {
const auto query =
"SELECT front, back, interval, unixepoch(updated_at) FROM cards"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
int rc = 0;
std::string front;
std::string back;
int interval = 0;
long long updated_at = 0;
bool success = false;
const auto new_cards = new_cards_reviewed_today();
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
if (!success) {
front = (const char *)sqlite3_column_text(stmt, 0);
back = (const char *)sqlite3_column_text(stmt, 1);
interval = sqlite3_column_int(stmt, 2);
updated_at = sqlite3_column_int64(stmt, 3);
if (needs_review(interval, updated_at)) {
if (interval == 0) {
if (new_cards >= NEW_CARDS_PER_DAY) {
continue;
}
new_cards_review_one();
}
success = true;
}
}
}
if (rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't select from table");
}
sqlite3_finalize(stmt);
if (!success) {
return;
}
std::string reply;
std::print("{}", front);
std::getline(std::cin, reply);
std::println("{}", back);
std::print("Ok? (Y/n) ");
std::getline(std::cin, reply);
if (reply == ""s || reply == "Y"s || reply == "y"s) {
interval = sizetint(std::min(size_t(interval) + 1, INTERVALS.size() - 1));
} else {
interval = interval == 0 ? 0 : 1;
}
update(interval, front);
}
auto remove(const std::string &front) -> void {
const auto query = "DELETE FROM cards WHERE front = ?"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
sqlite3_bind_text(stmt, 1, front.c_str(), sizetint(front.size()),
SQLITE_STATIC);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't delete from table");
}
sqlite3_finalize(stmt);
}
private:
auto add_one(const std::string &front, const std::string &back) -> void {
const auto query = "INSERT INTO cards (front, back) VALUES (?, ?)"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
sqlite3_bind_text(stmt, 1, front.c_str(), sizetint(front.size()),
SQLITE_STATIC);
sqlite3_bind_text(stmt, 2, back.c_str(), sizetint(back.size()),
SQLITE_STATIC);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't insert into table");
}
sqlite3_finalize(stmt);
}
auto create() -> void {
create_cards();
create_stats();
}
auto create_cards() -> void {
const auto query =
"CREATE TABLE IF NOT EXISTS cards (front TEXT PRIMARY KEY, back TEXT NOT NULL, interval INTEGER NOT NULL DEFAULT 0, created_at TEXT DEFAULT CURRENT_TIMESTAMP, updated_at TEXT DEFAULT CURRENT_TIMESTAMP)"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't create table");
}
sqlite3_finalize(stmt);
}
auto create_stats() -> void {
const auto query =
"CREATE TABLE IF NOT EXISTS stats (date TEXT PRIMARY KEY DEFAULT CURRENT_DATE, new_cards INTEGER NOT NULL)"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't create table");
}
sqlite3_finalize(stmt);
}
auto needs_review(int interval, long long updated_at) -> bool {
auto now = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
auto review_at =
updated_at +
std::chrono::duration_cast<std::chrono::seconds>(INTERVALS[interval])
.count();
return now >= review_at;
}
auto new_cards_review_one() -> void {
const auto query =
"UPDATE stats SET new_cards = new_cards + 1 WHERE date = CURRENT_DATE"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't update table");
}
sqlite3_finalize(stmt);
}
auto new_cards_reviewed_today() -> int {
const auto query = "SELECT date, new_cards FROM stats"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
int rc = 0;
std::string date;
int new_cards = 0;
std::time_t now = std::time(nullptr);
std::ostringstream oss;
oss << std::put_time(std::gmtime(&now), "%F");
const std::string current_date = oss.str();
bool success = false;
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
if (!success) {
date = (const char *)sqlite3_column_text(stmt, 0);
new_cards = sqlite3_column_int(stmt, 1);
if (date == current_date) {
success = true;
}
}
}
if (rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't select from table");
}
sqlite3_finalize(stmt);
if (!success) {
const auto query = "INSERT INTO stats (new_cards) VALUES (0)"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't insert into table");
}
sqlite3_finalize(stmt);
return 0;
}
return new_cards;
}
auto update(int interval, const std::string &front) -> void {
const auto query =
"UPDATE cards SET interval = ?, updated_at = CURRENT_TIMESTAMP WHERE front = ?"s;
sqlite3_stmt *stmt = nullptr;
sqlite3_prepare_v2(db_, query.c_str(), sizetint(query.size()), &stmt,
nullptr);
sqlite3_bind_int(stmt, 1, interval);
sqlite3_bind_text(stmt, 2, front.c_str(), sizetint(front.size()),
SQLITE_STATIC);
if (int rc = sqlite3_step(stmt); rc != SQLITE_DONE) {
sqlite3_finalize(stmt);
throw std::runtime_error("Can't update table");
}
sqlite3_finalize(stmt);
}
sqlite3 *db_;
};
static auto usage() -> void {
std::println(stderr,
"Usage: {} [add [<front> <back>] | list | remove <front>]",
PROGRAM);
}
auto main(int argc, char *argv[]) -> int {
try {
Application application;
if (argc == 1) {
application.next();
return 0;
}
const std::string command = argv[1];
if (command == "add"s) {
if (argc == 2) {
application.add();
} else {
requirenargs(4);
const std::string front = argv[2];
const std::string back = argv[3];
application.add(front, back);
}
} else if (command == "list"s) {
requirenargs(2);
application.list();
} else if (command == "remove"s) {
requirenargs(3);
const std::string front = argv[2];
application.remove(front);
} else {
usage();
return 1;
}
} catch (const std::exception &e) {
std::println(stderr, "{}", e.what());
return 1;
}
}