-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
209 lines (176 loc) · 4.44 KB
/
main.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
#include "parser.h"
#include "render.h"
#include "utils.h"
#include "vector.h"
#include <SDL2/SDL.h>
#include <getopt.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static ns_Renderer renderer;
static ns_Parser parser;
static vec_Vector *token_vec;
void ns_cleanup(int signum) {
(void)signum;
ns_renderer_destroy(&renderer);
ns_parser_free(&parser, token_vec);
exit(0);
}
void ns_show_help_message(void) {
puts("Simple plaintext presentation tool"
"\n\n"
"Usage: nsent [OPTION...] <FILE>"
"\n\n"
" -t\tChange the text color\n"
" -b\tChange the background color\n"
" -f\tChange the font family\n"
" -s\tChange font size\n"
" -p\tHide the progress bar\n"
" -h\tShow this message\n"
"-V\tPrints version information");
}
int main(int argc, char *argv[]) {
int opt;
// --- Default values ---
uint32_t text_color = 0xFFFFFFFF;
uint32_t bg_color = 0;
bool show_progressbar = true;
char font[PATH_MAX];
size_t fontsiz = 52;
ns_get_font_path(font, "");
// --- Overriding defaults with environment variables ---
char *env;
if ((env = getenv("NS_FG_COLOR")) != NULL) {
text_color = ns_get_color(env);
}
if ((env = getenv("NS_BG_COLOR")) != NULL) {
bg_color = ns_get_color(env);
}
if ((env = getenv("NS_PREFERRED_FONT")) != NULL) {
ns_get_font_path(font, env);
}
if ((env = getenv("NS_FONT_SIZE")) != NULL) {
fontsiz = SDL_strtol(env, NULL, 10);
}
// --- Parsing command line arguments ---
while ((opt = getopt(argc, argv, ":hVt:b:f:p")) != -1) {
switch (opt) {
case 't':
text_color = ns_get_color(optarg);
break;
case 'b':
bg_color = ns_get_color(optarg);
break;
case 'f':
ns_get_font_path(font, optarg);
break;
case 's':
fontsiz = SDL_strtol(env, NULL, 10);
break;
case 'p':
show_progressbar = false;
break;
case ':':
fputs("Error: Argument was not provided to option.\n", stderr);
return 1;
case '?':
fputs("Error: No such option.\n", stderr);
return 1;
case 'h':
ns_show_help_message();
return 0;
case 'V':
puts("Neosent v" NS_VERSION);
return 0;
}
}
char *stylesheet = argv[optind];
// --- Parsing presentation file ---
parser = ns_parser_new(stylesheet);
token_vec = vec_new();
ns_parser_parse(&parser, token_vec);
signal(SIGINT, ns_cleanup);
signal(SIGABRT, ns_cleanup);
signal(SIGTERM, ns_cleanup);
signal(SIGTSTP, ns_cleanup);
// --- Presenting contents of file ---
renderer = ns_renderer_create(stylesheet, font, fontsiz, text_color, bg_color,
token_vec, show_progressbar);
SDL_Event e;
size_t page = 0;
for (;;) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
goto cleanup;
}
if (e.type == SDL_MOUSEBUTTONDOWN) {
switch (e.button.button) {
case SDL_BUTTON_LEFT:
if (page + 1 < vec_len(token_vec)) {
++page;
}
break;
case SDL_BUTTON_RIGHT:
if (page != 0) {
--page;
}
break;
}
}
// --- Neosent keybindings ---
if (e.key.type == SDL_KEYDOWN && e.key.repeat == 0) {
switch (e.key.keysym.sym) {
// next page
case SDLK_RIGHT:
case SDLK_DOWN:
case SDLK_PAGEDOWN:
case 'l':
case 'j':
case 'n':
if (page + 1 < vec_len(token_vec)) {
++page;
}
break;
// previous page
case SDLK_LEFT:
case SDLK_UP:
case SDLK_PAGEUP:
case 'h':
case 'k':
case 'p':
if (page != 0) {
--page;
}
break;
case 'i':
ns_renderer_invert_colors(&renderer);
break;
// toggle fullscreen
case SDLK_F11:
case 'f':
ns_renderer_toggle_fullscreen(&renderer);
break;
// go to first page
case SDLK_HOME:
page = 0;
break;
// go to last page
case SDLK_END:
page = vec_len(token_vec) - 1;
break;
// close program
case 'q':
goto cleanup;
break;
}
}
}
ns_renderer_draw(&renderer, token_vec, page);
}
// --- Final cleanup ---
cleanup:
ns_cleanup(0);
return 0;
}