-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaskthing.c
319 lines (263 loc) · 8.44 KB
/
askthing.c
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
/*
* AskThing: a very cursed question-and-answer engine.
*
* Copyright (c) Eason Qin, 2025.
*
* This source code form is licensed under the Mozilla Public License version
* 2.0. You may find the full text of the license in the root of the project, or
* visit the OSI website for a digital version.
*
*/
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
#include "a_string.h"
#include "a_vector.h"
#include "question.h"
#include "tui/tui.h"
#include "util.h"
#define LICENSE \
"This Source Code Form is subject to the terms of the Mozilla Public\n" \
"License, v. 2.0. If a copy of the MPL was not distributed with this\n" \
"file, You can obtain one at http://mozilla.org/MPL/2.0/."
#define LINES S_DIM "--------------------" S_END "\n"
#define PROMPT S_BOLD "\nchoose an action.\n" S_END
#define WELCOME \
S_BOLD \
"Welcome to askthing.\n" S_END S_DIM "--------------------\n" \
"Copyright (c) Eason Qin, 2025.\n" LICENSE S_END "\n" \
"\nversion " VERSION "\n" S_DIM "--------------------\n" S_END PROMPT
#define VERSION "0.1.0"
struct state {
a_vector favorites; // a_vector of a_string
FILE* fav_fp;
};
struct state s;
struct termios default_termios;
bool running;
static a_string get_question_filename_from_stdin(void);
static void ask_question(a_string* filename);
static void about(void);
static void restore_terminal(void);
static void setup(void);
static a_string ensure_config_dir(void);
static void load_favorites(void);
static void leave(void);
static void view_favorites(void);
static void del_favorites(void);
static void save_favorite(void);
static a_string get_question_filename_from_stdin() {
a_string filename;
printf(S_DIM "enter questions filename: " S_END);
fflush(stdout);
a_string rawfn = a_string_with_capacity(100);
fgets(rawfn.data, 100, stdin);
rawfn.len = strlen(rawfn.data);
if (rawfn.len == 1) {
warn("no file provided");
a_string_free(&rawfn);
}
filename = a_string_trim(&rawfn);
FILE* tmp = fopen(filename.data, "r");
if (tmp == NULL) {
warn("file does not exist");
} else {
fclose(tmp);
}
a_string_free(&rawfn);
return filename;
}
static void ask_question(a_string* filename) {
QuestionGroup g = questiongroup_new(filename->data);
questiongroup_ask(&g);
questiongroup_destroy(&g);
}
static void about(void) { printf("version " VERSION "\n"); }
static void handle_ctrlc(int a) {
a = a; // bypass compiler warning
running = 0;
exit(1);
}
static void restore_terminal(void) {
tcsetattr(STDIN_FILENO, TCSAFLUSH, &default_termios);
printf(S_SHOWCURSOR);
}
static a_string ensure_config_dir(void) {
char* cfg_dir_raw = getenv("XDG_CONFIG_HOME");
a_string cfg_dir_path;
if (cfg_dir_raw == NULL) {
char* home = getenv("HOME");
if (home == NULL) {
fatal("could not find $HOME environment variable");
}
cfg_dir_path = a_string_sprintf("%s/.config/askthing", home);
} else {
cfg_dir_path = a_string_sprintf("%s/askthing", cfg_dir_raw);
}
// sketchy but it works
DIR* cfg_dir = opendir(cfg_dir_path.data);
if (cfg_dir != NULL) {
closedir(cfg_dir);
} else {
if (mkdir(cfg_dir_path.data, 0777))
panic("could not make config dir at `%s`", cfg_dir_path.data);
}
info("opened config dir");
return cfg_dir_path;
}
static void load_favorites(void) {
a_string cfg_dir = ensure_config_dir();
a_string favs_file = a_string_sprintf("%s/favorites", cfg_dir.data);
info("config at: %s", favs_file.data);
s.fav_fp = fopen(favs_file.data, "r+");
if (s.fav_fp == NULL) {
s.fav_fp = fopen(favs_file.data, "w+");
if (s.fav_fp == NULL) {
panic("could not open favorites file for w+");
}
} else {
a_string line = a_string_with_capacity(300);
while (fgets(line.data, 300, s.fav_fp) != NULL) {
line.len = strlen(line.data);
a_string new = a_string_trim(&line);
if (new.len == 0)
continue;
a_string resolved = a_string_realpath(new.data);
a_string_free(&new);
a_string* heapstr = calloc(1, sizeof(a_string));
check_alloc(heapstr);
*heapstr = resolved;
a_vector_append(&s.favorites, (void*)heapstr);
}
info("loaded favorites");
a_string_free(&line);
}
a_string_free(&favs_file);
a_string_free(&cfg_dir);
}
static void view_favorites(void) {
size_t index = tui_favorites(&s.favorites, astr("play?"));
a_string* fn = s.favorites.data[index];
restore_terminal();
if (index != s.favorites.len) {
ask_question(fn);
}
}
static void del_favorites(void) {
size_t index = tui_favorites(&s.favorites, astr("delete?"));
restore_terminal();
if (index == s.favorites.len)
return;
// extreme jank.
a_string* deleted = a_vector_pop_at(&s.favorites, index); // the easy part
a_string_free(deleted); // free the string buffer
free(deleted); // free the container too
// recalculate the path
a_string cfg_dir = ensure_config_dir();
a_string favs_file = a_string_sprintf("%s/favorites", cfg_dir.data);
// rewrite the whole file (at least there wont be fragmentation :P)
s.fav_fp = freopen(favs_file.data, "w+", s.fav_fp);
for (size_t i = 0; i < s.favorites.len; i++) {
a_string* line = s.favorites.data[i];
fputs(line->data, s.fav_fp);
fputs("\n", s.fav_fp);
}
a_string_free(&cfg_dir);
a_string_free(&favs_file);
}
static void save_favorite(void) {
// get the filename
printf(S_DIM "add a favorite: " S_END);
fflush(stdout);
a_string rawfn = a_string_with_capacity(100);
fgets(rawfn.data, 100, stdin);
rawfn.len = strlen(rawfn.data);
if (rawfn.len == 1) {
warn("no file provided");
a_string_free(&rawfn);
return;
}
a_string filename = a_string_trim(&rawfn);
a_string_free(&rawfn);
// check for its existence
FILE* tmp = fopen(filename.data, "r");
if (tmp != NULL) {
fclose(tmp); // immediately close
a_string resolved = a_string_realpath(filename.data);
a_string_free(&filename);
// push to file
fputs(resolved.data, s.fav_fp);
fputs("\n", s.fav_fp);
// push to buffer
a_string* fnheap = calloc(1, sizeof(a_string));
check_alloc(fnheap);
*fnheap = resolved;
a_vector_append(&s.favorites, (void*)fnheap);
} else {
warn("file does not exist!");
a_string_free(&filename);
}
}
static void setup(void) {
running = 1;
s.favorites = a_vector_new();
load_favorites();
tcgetattr(STDIN_FILENO, &default_termios);
signal(SIGINT, handle_ctrlc);
atexit(restore_terminal);
printf(WELCOME);
}
static void leave(void) {
for (size_t i = 0; i < s.favorites.len; i++) {
a_string_free((a_string*)s.favorites.data[i]);
free(s.favorites.data[i]);
}
a_vector_free(&s.favorites);
if (s.fav_fp != NULL) {
fclose(s.fav_fp);
}
restore_terminal();
printf(S_BOLD "Bye\n" S_END);
exit(EXIT_SUCCESS);
}
int main(int argc, char** argv) {
argc--;
argv++;
setup();
TuiHomescreenSelected a;
do {
a = tui_homescreen();
printf("\n\n");
restore_terminal();
switch (a) {
case TUI_HOME_LOAD_SET: {
a_string fn = get_question_filename_from_stdin();
ask_question(&fn);
a_string_free(&fn);
} break;
case TUI_HOME_LOAD_FAVORITE: {
view_favorites();
} break;
case TUI_HOME_SAVE_FAVORITE: {
save_favorite();
} break;
case TUI_HOME_DELETE_FAVORITE: {
del_favorites();
} break;
case TUI_HOME_ABOUT: {
about();
} break;
case TUI_HOME_EXIT: {
running = 0;
leave();
}
}
printf(PROMPT);
} while (running);
leave();
}