0f5d92896ec14c047fc8701b3cf70f01fe4b4726
[smdp.git] / viewer.c
1 #include <ncurses.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4
5 #include "include/parser.h"
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
233     if(line->text->text) {
234         int offset = 0; // text offset
235         offset = next_nonblank(line->text, 0);
236
237         // IS_CODE
238         if(CHECK_BIT(line->bits, IS_CODE)) {
239
240             // set static offset for code
241             offset = CODE_INDENT;
242
243             // reverse color for code blocks
244             wattron(window, A_REVERSE);
245
246             // print whole lines
247             mvwprintw(window,
248                       y, x,
249                       "%s", &line->text->text[offset]);
250
251         // IS_QUOTE
252         } else if(CHECK_BIT(line->bits, IS_QUOTE)) {
253             //TODO replace greater sign with color block
254
255             //FIXME remove dummy print code
256             mvwprintw(window,
257                       y, x,
258                       "%s", &line->text->text[offset]);
259
260         } else {
261
262             // IF_H1 || IF_H2
263             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
264
265                 // set headline color
266                 wattron(window, COLOR_PAIR(CP_BLUE));
267
268                 // enable underline for H1
269                 if(CHECK_BIT(line->bits, IS_H1))
270                     wattron(window, A_UNDERLINE);
271
272                 // skip hashes
273                 while(line->text->text[offset] == '#')
274                     offset = next_word(line->text, offset);
275
276                 // print whole lines
277                 mvwprintw(window,
278                       y, x,
279                       "%s", &line->text->text[offset]);
280
281                 wattroff(window, A_UNDERLINE);
282
283             } else {
284                 //TODO for each char in line
285                     //TODO if *|_ highlight (maybe use a stack here?)
286                 mvwprintw(window,
287                       y, x,
288                       "%s", &line->text->text[offset]);
289             }
290         }
291
292         // fill rest off line with spaces
293         for(i = getcurx(window) - x; i < max_cols; i++)
294             wprintw(window, "%s", " ");
295
296         // reset to default color
297         wattron(window, COLOR_PAIR(CP_WHITE));
298         wattroff(window, A_UNDERLINE);
299         wattroff(window, A_REVERSE);
300     }
301 }
302
303 void fade_out(WINDOW *window, int trans, int colors) {
304     int i; // increment
305     if(colors) {
306         for(i = 22; i >= 0; i--) {
307             // darken color pairs
308             init_pair(CP_WHITE, white_ramp[i], trans);
309             init_pair(CP_BLUE, blue_ramp[i], trans);
310             init_pair(CP_RED, red_ramp[i], trans);
311             // refresh window with new color
312             wrefresh(window);
313             // delay for our eyes to recognize the change
314             usleep(FADE_DELAY);
315         }
316     }
317 }
318
319 void fade_in(WINDOW *window, int trans, int colors) {
320     int i; // increment
321     if(colors) {
322         for(i = 0; i <= 23; i++) {
323             // lighten color pairs
324             init_pair(CP_WHITE, white_ramp[i], trans);
325             init_pair(CP_BLUE, blue_ramp[i], trans);
326             init_pair(CP_RED, red_ramp[i], trans);
327             // refresh window with new color
328             wrefresh(window);
329             // delay for our eyes to recognize the change
330             usleep(FADE_DELAY);
331         }
332     }
333 }
334