4e91b71390c6cec4b109c61b5dee5bb008beb68e
[smdp.git] / viewer.c
1 #include <ncurses.h>
2 #include <stdlib.h>
3 #include <string.h> // strchr
4 #include <unistd.h>
5
6 #include "include/viewer.h"
7
8 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
9                                237, 238, 239, 240, 241, 242,
10                                244, 245, 246, 247, 248, 249,
11                                250, 251, 252, 253, 254, 255 };
12
13 static short blue_ramp[24]  = { 16,  17,  17,  18,  18,  19,
14                                 19,  20,  20,  21,  27,  32,
15                                 33,  38,  39,  44,  45,  45,
16                                 81,  81,  51,  51, 123, 123 };
17
18 static short red_ramp[24]   = { 16,  52,  52,  53,  53,  89,
19                                 89,  90,  90, 126, 127, 127,
20                                163, 163, 164, 164, 200, 200,
21                                201, 201, 207, 207, 213, 213 };
22
23 int ncurses_display(deck_t *deck, int notrans, int nofade) {
24
25     int c = 0;          // char
26     int l = 0;          // line number
27     int colors = 0;     // amount of colors supported
28     int fade = 0;       // disable color fading by default
29     int trans = -1;     // enable transparency if term supports it
30     int max_lines = 0;  // max lines per slide
31     int max_cols = 0;   // max columns per line
32     int offset;         // text offset
33
34     // header line 1 is displayed at the top
35     int bar_top = (deck->headers > 0) ? 1 : 0;
36     // header line 2 and 3 are displayed at the bottom
37     int bar_bottom = (deck->headers > 1) ? 1 : 0;
38
39     slide_t *slide = deck->slide;
40     line_t *line;
41
42     while(slide) {
43         // set max_lines if line count exceeded
44         max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
45         line = slide->line;
46         while(line) {
47             // set max_cols if length exceeded
48             max_cols = (line->length > max_cols) ? line->length : max_cols;
49             line = line->next;
50         }
51         slide = slide->next;
52     }
53
54     // replace stdin with current tty if markdown input was piped
55     freopen("/dev/tty", "rw", stdin);
56
57     // init ncurses
58     initscr();
59
60     if((max_cols > COLS) ||
61        (max_lines + bar_top + bar_bottom + 2 > LINES)) {
62
63         fprintf(stderr, "Error: Terminal size %ix%i to small. Need at least %ix%i.\n",
64             COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
65         endwin();
66         return(1);
67     }
68
69     // replace stdin with current tty if markdown input was piped
70     freopen("/dev/tty", "rw", stdin);
71
72     // disable cursor
73     curs_set(0);
74
75     // disable output of keyboard typing
76     noecho();
77
78     // make getch() process one char at a time
79     cbreak();
80
81     // enable arrow keys
82     keypad(stdscr,TRUE);
83
84     // set colors
85     if(has_colors() == TRUE) {
86         start_color();
87         use_default_colors();
88
89         if(notrans) trans = 0; // 0 is black
90
91         if(COLORS == 256) {
92             // 256 color mode
93             init_pair(CP_WHITE, 255, trans);
94             init_pair(CP_BLUE, 123, trans);
95             init_pair(CP_RED, 213, trans);
96             init_pair(CP_YELLOW, 208, trans);
97
98             // enable color fading
99             if(!nofade) fade = 1;
100         } else {
101
102             // 8 color mode
103             init_pair(CP_WHITE, 7, trans);
104             init_pair(CP_BLUE, 4, trans);
105             init_pair(CP_RED, 1, trans);
106             init_pair(CP_YELLOW, 3, trans);
107         }
108
109         colors = 1;
110     }
111
112     // set background color of main window
113     if(colors)
114         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
115
116     // setup header
117     if(bar_top) {
118         line = deck->header;
119         offset = next_blank(line->text, 0) + 1;
120         // add text to header
121         mvwprintw(stdscr,
122                   0, (COLS - line->length + offset) / 2,
123                   "%s", &line->text->text[offset]);
124     }
125
126     // setup footer
127     if(bar_bottom) {
128         line = deck->header->next;
129         offset = next_blank(line->text, 0) + 1;
130         // add text to left footer
131         mvwprintw(stdscr,
132                   LINES - 1, 3,
133                   "%s", &line->text->text[offset]);
134
135         if(deck->headers > 2) {
136             line = deck->header->next->next;
137             offset = next_blank(line->text, 0) + 1;
138             // add text to right footer
139             mvwprintw(stdscr,
140                       LINES - 1, COLS - line->length + offset - 3,
141                       "%s", &line->text->text[offset]);
142         }
143     }
144
145     // make header + fooder visible
146     wrefresh(stdscr);
147
148     // setup main window
149     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
150     if(colors)
151         wbkgd(content, COLOR_PAIR(CP_WHITE));
152
153     slide = deck->slide;
154     while(slide) {
155         // clear main window
156         werase(content);
157
158         line = slide->line;
159         l = 0;
160
161         // print lines
162         while(line) {
163             add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
164             line = line->next;
165             l++;
166         }
167
168         // make content visible
169         wrefresh(content);
170
171         // fade in
172         if(fade)
173             fade_in(content, trans, colors);
174
175         // re-enable fading after any undefined key press
176         if(COLORS == 256 && !nofade) fade = 1;
177
178         // wait for user input
179         c = getch();
180
181         // evaluate user input
182         switch(c) {
183
184             // show previous slide
185             case KEY_UP:
186             case KEY_LEFT:
187             case 8:   // BACKSPACE (ascii)
188             case 127: // BACKSPACE (xterm)
189             case 263: // BACKSPACE (getty)
190             case 'h':
191             case 'k':
192                 if(slide->prev)
193                     slide = slide->prev;
194                 break;
195
196             // show next slide
197             case KEY_DOWN:
198             case KEY_RIGHT:
199             case '\n': // ENTER
200             case ' ':  // SPACE
201             case 'j':
202             case 'l':
203                 if(slide->next)
204                     slide = slide->next;
205                 break;
206
207             // quit
208             case 'q':
209                 // do not fade out on exit
210                 fade = 0;
211                 slide = (void*)0;
212                 break;
213
214             default:
215                 // disable fading on undefined key press
216                 fade = 0;
217                 break;
218         }
219
220         // fade out
221         if(fade)
222             fade_out(content, trans, colors);
223     }
224
225     endwin();
226
227     return(0);
228 }
229
230 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
231     int i = 0; // increment
232     char *c; // char pointer for iteration
233     char *special = "\\*_"; // list of interpreted chars
234     cstack_t *stack = cstack_init();
235
236     if(line->text->text) {
237         int offset = 0; // text offset
238         offset = next_nonblank(line->text, 0);
239
240         // IS_CODE
241         if(CHECK_BIT(line->bits, IS_CODE)) {
242
243             // set static offset for code
244             offset = CODE_INDENT;
245
246             // reverse color for code blocks
247             wattron(window, A_REVERSE);
248
249             // print whole lines
250             mvwprintw(window,
251                       y, x,
252                       "%s", &line->text->text[offset]);
253
254         // IS_QUOTE
255         } else if(CHECK_BIT(line->bits, IS_QUOTE)) {
256             //TODO replace greater sign with color block
257
258             //FIXME remove dummy print code
259             mvwprintw(window,
260                       y, x,
261                       "%s", &line->text->text[offset]);
262
263         } else {
264
265             // IF_H1 || IF_H2
266             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
267
268                 // set headline color
269                 wattron(window, COLOR_PAIR(CP_BLUE));
270
271                 // enable underline for H1
272                 if(CHECK_BIT(line->bits, IS_H1))
273                     wattron(window, A_UNDERLINE);
274
275                 // skip hashes
276                 while(line->text->text[offset] == '#')
277                     offset = next_word(line->text, offset);
278
279                 // print whole lines
280                 mvwprintw(window,
281                       y, x,
282                       "%s", &line->text->text[offset]);
283
284                 wattroff(window, A_UNDERLINE);
285
286             } else {
287                 // move the cursor in position
288                 wmove(window, y, x);
289
290                 // for each char in line
291                 c = line->text->text;
292                 while(*c) {
293
294                     // if char is in special char list
295                     if(strchr(special, *c)) {
296
297                         // closing special char (or second backslash)
298                         if((stack->top)(stack, *c)) {
299
300                             switch(*c) {
301                                 // print escaped backslash
302                                 case '\\':
303                                     wprintw(window, "%c", *c);
304                                     break;
305                                 // disable highlight
306                                 case '*':
307                                     wattron(window, COLOR_PAIR(CP_WHITE));
308                                     break;
309                                 // disable underline
310                                 case '_':
311                                     wattroff(window, A_UNDERLINE);
312                                     break;
313                             }
314
315                             // remove top special char from stack
316                             (stack->pop)(stack);
317
318                         // treat special as regular char
319                         } else if((stack->top)(stack, '\\')) {
320                             wprintw(window, "%c", *c);
321
322                             // remove backslash from stack
323                             (stack->pop)(stack);
324
325                         // opening special char
326                         } else {
327                             switch(*c) {
328                                 // enable highlight
329                                 case '*':
330                                     wattron(window, COLOR_PAIR(CP_RED));
331                                     break;
332                                 // enable underline
333                                 case '_':
334                                     wattron(window, A_UNDERLINE);
335                                     break;
336                                 // do nothing for backslashes
337                             }
338
339                             // push special char to stack
340                             (stack->push)(stack, *c);
341                         }
342
343                     } else {
344                         // print regular char
345                         wprintw(window, "%c", *c);
346                     }
347
348                     c++;
349                 }
350
351                 //TODO pop stack until empty
352             }
353         }
354
355         // fill rest off line with spaces
356         for(i = getcurx(window) - x; i < max_cols; i++)
357             wprintw(window, "%s", " ");
358
359         // reset to default color
360         wattron(window, COLOR_PAIR(CP_WHITE));
361         wattroff(window, A_UNDERLINE);
362         wattroff(window, A_REVERSE);
363     }
364
365     (stack->delete)(stack);
366 }
367
368 void fade_out(WINDOW *window, int trans, int colors) {
369     int i; // increment
370     if(colors) {
371         for(i = 22; i >= 0; i--) {
372             // darken color pairs
373             init_pair(CP_WHITE, white_ramp[i], trans);
374             init_pair(CP_BLUE, blue_ramp[i], trans);
375             init_pair(CP_RED, red_ramp[i], trans);
376             // refresh window with new color
377             wrefresh(window);
378             // delay for our eyes to recognize the change
379             usleep(FADE_DELAY);
380         }
381     }
382 }
383
384 void fade_in(WINDOW *window, int trans, int colors) {
385     int i; // increment
386     if(colors) {
387         for(i = 0; i <= 23; i++) {
388             // lighten color pairs
389             init_pair(CP_WHITE, white_ramp[i], trans);
390             init_pair(CP_BLUE, blue_ramp[i], trans);
391             init_pair(CP_RED, red_ramp[i], trans);
392             // refresh window with new color
393             wrefresh(window);
394             // delay for our eyes to recognize the change
395             usleep(FADE_DELAY);
396         }
397     }
398 }
399