5 #include "include/parser.h"
6 #include "include/viewer.h"
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 };
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 };
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 };
23 int ncurses_display(deck_t *deck, int notrans, int nofade) {
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
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;
39 slide_t *slide = deck->slide;
43 // set max_lines if line count exceeded
44 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
47 // set max_cols if length exceeded
48 max_cols = (line->length > max_cols) ? line->length : max_cols;
54 // replace stdin with current tty if markdown input was piped
55 freopen("/dev/tty", "rw", stdin);
60 if((max_cols > COLS) ||
61 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
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);
69 // replace stdin with current tty if markdown input was piped
70 freopen("/dev/tty", "rw", stdin);
75 // disable output of keyboard typing
78 // make getch() process one char at a time
85 if(has_colors() == TRUE) {
89 if(notrans) trans = 0; // 0 is black
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);
98 // enable color fading
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);
112 // set background color of main window
114 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
119 offset = next_blank(line->text, 0) + 1;
120 // add text to header
122 0, (COLS - line->length + offset) / 2,
123 "%s", &line->text->text[offset]);
128 line = deck->header->next;
129 offset = next_blank(line->text, 0) + 1;
130 // add text to left footer
133 "%s", &line->text->text[offset]);
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
140 LINES - 1, COLS - line->length + offset - 3,
141 "%s", &line->text->text[offset]);
145 // make header + fooder visible
149 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
151 wbkgd(content, COLOR_PAIR(CP_WHITE));
163 add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
168 // make content visible
173 fade_in(content, trans, colors);
175 // re-enable fading after any undefined key press
176 if(COLORS == 256 && !nofade) fade = 1;
178 // wait for user input
181 // evaluate user input
184 // show previous slide
187 case 8: // BACKSPACE (ascii)
188 case 127: // BACKSPACE (xterm)
189 case 263: // BACKSPACE (getty)
209 // do not fade out on exit
215 // disable fading on undefined key press
222 fade_out(content, trans, colors);
230 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
231 int i = 0; // increment
233 if(line->text->text) {
234 int offset = 0; // text offset
235 offset = next_nonblank(line->text, 0);
238 if(CHECK_BIT(line->bits, IS_CODE)) {
240 // set static offset for code
241 offset = CODE_INDENT;
243 // reverse color for code blocks
244 wattron(window, A_REVERSE);
249 "%s", &line->text->text[offset]);
252 } else if(CHECK_BIT(line->bits, IS_QUOTE)) {
253 //TODO replace greater sign with color block
255 //FIXME remove dummy print code
258 "%s", &line->text->text[offset]);
263 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
265 // set headline color
266 wattron(window, COLOR_PAIR(CP_BLUE));
268 // enable underline for H1
269 if(CHECK_BIT(line->bits, IS_H1))
270 wattron(window, A_UNDERLINE);
273 while(line->text->text[offset] == '#')
274 offset = next_word(line->text, offset);
279 "%s", &line->text->text[offset]);
281 wattroff(window, A_UNDERLINE);
284 //TODO for each char in line
285 //TODO if *|_ highlight (maybe use a stack here?)
288 "%s", &line->text->text[offset]);
292 // fill rest off line with spaces
293 for(i = getcurx(window) - x; i < max_cols; i++)
294 wprintw(window, "%s", " ");
296 // reset to default color
297 wattron(window, COLOR_PAIR(CP_WHITE));
298 wattroff(window, A_UNDERLINE);
299 wattroff(window, A_REVERSE);
303 void fade_out(WINDOW *window, int trans, int 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
313 // delay for our eyes to recognize the change
319 void fade_in(WINDOW *window, int trans, int 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
329 // delay for our eyes to recognize the change