-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
290 lines (256 loc) · 6.22 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <ncurses.h>
#include <locale.h>
#include "utils.h"
#define MIN(a,b) (a < b) ? a : b
#define MAX(a,b) (a > b) ? a : b
void statusline(int row, int now, int total, char* title);
int readfile(char arr[MAXSLD][MAXLEN], char *title, int slidegrp[], const char fname[]) {
FILE *fp;
if ((fp = fopen(fname, "r")) == NULL) {
fputs("Error: file does not exist or is not readable\n", stderr);
usage();
}
char c, line[MAXLEN];
int i, j, k;
i = j = k = 0;
slidegrp[0] = 0;
while ((c = getc(fp)) != EOF && i < MAXLEN) {
if (c == '\n') {
rtrim(line);
if (!strcmp(line, "--")) {
strcpy(arr[i+1], arr[i]);
slidegrp[++i] = k;
} else if (!strcmp(line, "--newpage"))
k = ++i, slidegrp[i] = k;
else {
line[j++] = c;
line[j] = '\0';
gettitle(line, title);
strcat(arr[i], line);
}
clean(line);
j = 0;
} else
line[j++] = c;
}
if (!title)
fnametotitle(title, fname);
return i;
}
void format(WINDOW *win, int height, int width, char* text, int lc);
void draw(char *slide, int now, int total, char* title) {
int row, col, margin = 4;
getmaxyx(stdscr, row, col);
/* line width, temp width & line count */
int lw, tw, lc = 1;
lw = tw = 0;
char c, temp[MAXLEN] = "", msg[MAXLEN] = "";
/* fix formating so that it breaks words when having lines too long for screen */
while (*slide != '\0' && lc < (row - 2*margin)) {
temp[tw++] = c = *slide++;
if (isspace(c)) {
if (tw + lw <= (col - 2*margin)) {
strcat(msg, temp);
if (c == '\n') {
lc++;
lw = 0;
} else
lw += tw;
} else {
lw = tw;
lc++;
strcat(strcat(msg, "\n"), temp);
}
clean(temp);
tw = 0;
}
}
WINDOW *win;
win = newwin(row - 2*margin, col - 2*margin, margin, margin);
format(win, row - 2*margin, col - 2*margin, msg, lc);
wrefresh(win);
delwin(win);
statusline(row, now+1, total+1, title);
refresh();
}
int incSlide(int slide, const int slidegrp[], const int total) {
for (int i = slide; i < total+1; ++i) {
if (slidegrp[i] != slidegrp[slide])
return i;
}
return slide;
}
int decSlide(int slide, const int slidegrp[], const int total) {
int value = 0;
for (int i = slide; i >= 0; --i) {
if (slidegrp[i] != slidegrp[slide] && !value)
value = slidegrp[i];
else if (slidegrp[i] < value)
return i+1;
}
return 0;
}
int main(int argc, char *argv[]) {
char pages[MAXSLD][MAXLEN] = {""};
int slide = 0, total = 0, slidegrp[MAXSLD] = {};
char title[200] = "";
/* get options */
if (argc == 1)
usage();
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-v")) {
printf("scpp 1.2\n");
return 0;
} else if (!strcmp(argv[i], "-h"))
usage();
else if (!strcmp(argv[i], "-f"))
slide = -1;
else if (i + 1 == argc)
total = readfile( pages, title, slidegrp, (!strcmp(argv[i], "-")) ? "/dev/stdin" : argv[i] );
else if (!strcmp(argv[i], "-s"))
++i, slide = (atoi(argv[i]) > 0) ? atoi(argv[i]) - 1 : atoi(argv[i]);
else
usage();
}
/* check for slide overflow or negatives */
slide = (slide < 0) ? MAX(total + slide + 1, 0) : slide;
slide = MIN(slide, total);
setlocale(LC_ALL, ""); // allow utf8 characters
initscr();
cbreak();
curs_set(0); // hide cursor
refresh();
draw(pages[slide], slide, total, title);
noecho(); // don't display pressed characters
keypad(stdscr, TRUE); // include extra keys (in particular, left, right and backspace)
int i, key, num = 0;
while ( (key = getch()) != 'q' && key != 'Q' ) {
/* number based commands */
if (isdigit(key))
do
num = 10*num + (key - '0');
while ( isdigit(key = getch()) );
switch (key) {
case 'h':
case 'p':
case KEY_LEFT:
case KEY_BACKSPACE:
slide -= MAX(num, 1); // is num not null?
slide = MAX(0, slide); // slide overflow
draw(pages[slide], slide, total, title);
break;
case 'l':
case 'n':
case KEY_RIGHT:
case ' ':
case '\n':
slide += MAX(num, 1);
slide = MIN(slide, total); // slide overflow
draw(pages[slide], slide, total, title);
break;
case 'H':
i = 0;
do {
if (slidegrp[slide])
slide = decSlide(slide, slidegrp, total);
else
slide = 0;
} while ((++i) < num);
draw(pages[slide], slide, total, title);
break;
case 'L':
i = 0;
do
slide = incSlide(slide, slidegrp, total);
while ((++i) < num);
draw(pages[slide], slide, total, title);
break;
case 'g':
num--; // user will press slide 0 as 1
slide = MAX(num, 0);
slide = MIN(slide, total);
draw(pages[slide], slide, total, title);
break;
case 'G':
if (num) {
num--;
slide = num;
slide = MIN(slide, total);
} else
slide = total;
draw(pages[slide], slide, total, title);
break;
case KEY_RESIZE:
endwin();
refresh();
clear();
draw(pages[slide], slide, total, title);
break;
}
num = 0; // reset number
}
endwin();
return 0;
}
void statusline(int row, int now, int total, char* title) {
attron(A_BOLD);
mvprintw(row-1, 0, "[%d/%d] %s", now, total, title);
attroff(A_BOLD);
}
void format(WINDOW *win, int height, int width, char* text, int lc) {
char c;
int i = 0;
/* title format */
int attr, inc;
bool it = false, bf = false;
/* check for slide title/subtitle */
for (int j = 0; j < 2; ++j) {
if (text[i] == '#') {
if (text[++i] == '#')
attr = A_ITALIC, i++;
else
attr = A_BOLD;
wattron(win, attr);
do
wprintw(win, "%c", text[i]);
while (text[++i] != '\n');
wattroff(win, attr);
waddch(win, text[i++]);
lc--;
} else
break;
}
wmove(win, (height - lc)/2, 0);
for (; i < strlen(text); ++i) {
if ((c = text[i]) == '*' || c == '_') {
if (text[++i] == c) {
if (bf)
wattroff(win, A_BOLD);
else
wattron(win, A_BOLD);
bf = !bf;
} else {
if (it)
wattroff(win, A_ITALIC);
else
wattron(win, A_ITALIC);
it = !it, i--;
}
continue;
}
if (i == 0 || text[i-1] == '\n') {
inc = formatcenter(win, i, text, width);
i += inc;
if (inc)
continue;
}
/* We use wprintw instead of waddch because of utf8 displaying errors */
/* waddch(win, (c == '\\') ? text[++i] : c); */
wprintw(win, "%c", (c == '\\') ? text[++i] : c);
}
wattrset(win, A_NORMAL);
}