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