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
7 * This file is part of mdp.
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.
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.
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/>.
24 #include <locale.h> // setlocale
27 #include <string.h> // strchr
30 #include "include/viewer.h"
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 };
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 };
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 };
47 int ncurses_display(deck_t *deck, int notrans, int nofade) {
51 int l = 0; // line number
52 int sc = 1; // slide count
53 int colors = 0; // amount of colors supported
54 int fade = 0; // disable color fading by default
55 int trans = -1; // enable transparency if term supports it
56 int max_lines = 0; // max lines per slide
57 int max_cols = 0; // max columns per line
58 int offset; // text offset
60 // header line 1 is displayed at the top
61 int bar_top = (deck->headers > 0) ? 1 : 0;
62 // header line 2 is displayed at the bottom
63 // anyway we display the slide number at the bottom
66 slide_t *slide = deck->slide;
70 // set max_lines if line count exceeded
71 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
74 // set max_cols if length exceeded
75 max_cols = (line->length > max_cols) ? line->length : max_cols;
81 // set locale to display UTF-8 correctly in ncurses
82 setlocale(LC_CTYPE, "");
84 // replace stdin with current tty if markdown input was piped
85 freopen("/dev/tty", "rw", stdin);
90 if((max_cols > COLS) ||
91 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
93 fprintf(stderr, "Error: Terminal size %ix%i to small. Need at least %ix%i.\n",
94 COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
99 // replace stdin with current tty if markdown input was piped
100 freopen("/dev/tty", "rw", stdin);
105 // disable output of keyboard typing
108 // make getch() process one char at a time
115 if(has_colors() == TRUE) {
117 use_default_colors();
119 if(notrans) trans = 0; // black in 8 color mode
122 if(notrans) trans = 16; // black in 256 color mode
125 init_pair(CP_WHITE, 255, trans);
126 init_pair(CP_BLUE, 123, trans);
127 init_pair(CP_RED, 213, trans);
128 init_pair(CP_YELLOW, 208, trans);
130 // enable color fading
131 if(!nofade) fade = 1;
135 init_pair(CP_WHITE, 7, trans);
136 init_pair(CP_BLUE, 4, trans);
137 init_pair(CP_RED, 1, trans);
138 init_pair(CP_YELLOW, 3, trans);
144 // set background color of main window
146 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
149 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
151 wbkgd(content, COLOR_PAIR(CP_WHITE));
159 // always resize window in case terminal geometry has changed
160 wresize(content, LINES - bar_top - bar_bottom, COLS);
165 offset = next_blank(line->text, 0) + 1;
166 // add text to header
168 0, (COLS - line->length + offset) / 2,
169 "%s", &line->text->text[offset]);
173 if(deck->headers > 1) {
174 line = deck->header->next;
175 offset = next_blank(line->text, 0) + 1;
176 // add text to left footer
179 "%s", &line->text->text[offset]);
181 // add slide number to right footer
183 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
184 "%d / %d", sc, deck->slides);
186 // make header + fooder visible
194 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
199 // make content visible
204 fade_in(content, trans, colors);
206 // re-enable fading after any undefined key press
207 if(COLORS == 256 && !nofade) fade = 1;
209 // wait for user input
212 // evaluate user input
216 // show previous slide
219 case 8: // BACKSPACE (ascii)
220 case 127: // BACKSPACE (xterm)
221 case 263: // BACKSPACE (getty)
253 if(i <= deck->slides) {
270 // disable fading if slide n doesn't exist
283 for(i = sc; i <= deck->slides; i++) {
293 // do not fade out on exit
299 // disable fading on undefined key press
306 fade_out(content, trans, colors);
314 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
315 int i = 0; // increment
316 char *c; // char pointer for iteration
317 char *special = "\\*_"; // list of interpreted chars
318 cstack_t *stack = cstack_init();
320 if(line->text->text) {
321 int offset = 0; // text offset
324 if(CHECK_BIT(line->bits, IS_CODE)) {
326 // set static offset for code
327 offset = CODE_INDENT;
329 // reverse color for code blocks
330 wattron(window, A_REVERSE);
335 "%s", &line->text->text[offset]);
340 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
342 // set headline color
344 wattron(window, COLOR_PAIR(CP_BLUE));
346 // enable underline for H1
347 if(CHECK_BIT(line->bits, IS_H1))
348 wattron(window, A_UNDERLINE);
351 while(line->text->text[offset] == '#')
352 offset = next_word(line->text, offset);
357 "%s", &line->text->text[offset]);
359 wattroff(window, A_UNDERLINE);
362 // move the cursor in position
366 if(CHECK_BIT(line->bits, IS_QUOTE)) {
367 while(line->text->text[offset] == '>') {
368 // print a reverse color block
369 wattron(window, A_REVERSE);
370 wprintw(window, "%s", " ");
371 wattroff(window, A_REVERSE);
372 wprintw(window, "%s", " ");
373 // find next quote or break
375 if(line->text->text[offset] == ' ')
376 offset = next_word(line->text, offset);
380 // for each char in line
381 c = &line->text->text[offset];
384 // if char is in special char list
385 if(strchr(special, *c)) {
387 // closing special char (or second backslash)
388 if((stack->top)(stack, *c)) {
391 // print escaped backslash
393 wprintw(window, "%c", *c);
398 wattron(window, COLOR_PAIR(CP_WHITE));
402 wattroff(window, A_UNDERLINE);
406 // remove top special char from stack
409 // treat special as regular char
410 } else if((stack->top)(stack, '\\')) {
411 wprintw(window, "%c", *c);
413 // remove backslash from stack
416 // opening special char
422 wattron(window, COLOR_PAIR(CP_RED));
426 wattron(window, A_UNDERLINE);
428 // do nothing for backslashes
431 // push special char to stack
432 (stack->push)(stack, *c);
436 // print regular char
437 wprintw(window, "%c", *c);
443 // pop stack until empty to prevent formated trailing spaces
444 while(!(stack->empty)(stack)) {
445 switch((stack->pop)(stack)) {
449 wattron(window, COLOR_PAIR(CP_WHITE));
453 wattroff(window, A_UNDERLINE);
455 // do nothing for backslashes
461 // fill rest off line with spaces
462 for(i = getcurx(window) - x; i < max_cols; i++)
463 wprintw(window, "%s", " ");
465 // reset to default color
467 wattron(window, COLOR_PAIR(CP_WHITE));
468 wattroff(window, A_UNDERLINE);
469 wattroff(window, A_REVERSE);
472 (stack->delete)(stack);
475 void fade_out(WINDOW *window, int trans, int colors) {
478 for(i = 22; i >= 0; i--) {
479 // darken color pairs
480 init_pair(CP_WHITE, white_ramp[i], trans);
481 init_pair(CP_BLUE, blue_ramp[i], trans);
482 init_pair(CP_RED, red_ramp[i], trans);
483 // refresh window with new color
485 // delay for our eyes to recognize the change
491 void fade_in(WINDOW *window, int trans, int colors) {
494 for(i = 0; i <= 23; i++) {
495 // lighten color pairs
496 init_pair(CP_WHITE, white_ramp[i], trans);
497 init_pair(CP_BLUE, blue_ramp[i], trans);
498 init_pair(CP_RED, red_ramp[i], trans);
499 // refresh window with new color
501 // delay for our eyes to recognize the change
507 int int_length (int val) {