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
26 #include <string.h> // strchr
29 #include "include/viewer.h"
31 // color ramp for fading from black to color
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, 33,
39 32, 39, 38, 45, 44, 44,
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 // color ramp for fading from white to color
48 static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251,
49 250, 249, 248, 247, 246, 245,
50 243, 242, 241, 240, 239, 238,
51 237, 236, 235, 234, 233, 232};
53 static short blue_ramp_invert[24] = { 15, 231, 231, 195, 195, 159,
54 159, 123, 123, 87, 51, 44,
55 45, 38, 39, 32, 33, 33,
56 26, 26, 27, 27, 21, 21};
58 static short red_ramp_invert[24] = { 15, 231, 231, 224, 224, 225,
59 225, 218, 218, 219, 212, 213,
60 206, 207, 201, 200, 199, 199,
61 198, 198, 197, 197, 196, 196};
63 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
67 int l = 0; // line number
68 int sc = 1; // slide count
69 int colors = 0; // amount of colors supported
70 int fade = 0; // disable color fading by default
71 int trans = -1; // enable transparency if term supports it
72 int max_lines = 0; // max lines per slide
73 int max_cols = 0; // max columns per line
74 int offset; // text offset
76 // header line 1 is displayed at the top
77 int bar_top = (deck->headers > 0) ? 1 : 0;
78 // header line 2 is displayed at the bottom
79 // anyway we display the slide number at the bottom
82 slide_t *slide = deck->slide;
86 // set max_lines if line count exceeded
87 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
90 // set max_cols if length exceeded
91 max_cols = (line->length > max_cols) ? line->length : max_cols;
97 // set locale to display UTF-8 correctly in ncurses
98 setlocale(LC_CTYPE, "");
103 if((max_cols > COLS) ||
104 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
110 fprintf(stderr, "Error: Terminal size %ix%i too small. Need at least %ix%i.\n",
111 COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
113 // print hint to solve it
114 if(max_lines + bar_top + bar_bottom + 2 > LINES)
115 fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
117 fprintf(stderr, "Automatic line wrapping is not supported jet. You may need to shorten some lines by inserting line breaks.\n");
125 // disable output of keyboard typing
128 // make getch() process one char at a time
135 if(has_colors() == TRUE) {
137 use_default_colors();
144 trans = 15; // white in 256 color mode
146 trans = 16; // black in 256 color mode
151 init_pair(CP_WHITE, 232, trans);
152 init_pair(CP_BLUE, 21, trans);
153 init_pair(CP_RED, 196, trans);
154 init_pair(CP_BLACK, 15, 232);
156 init_pair(CP_WHITE, 255, trans);
157 init_pair(CP_BLUE, 123, trans);
158 init_pair(CP_RED, 213, trans);
159 init_pair(CP_BLACK, 16, 255);
161 init_pair(CP_YELLOW, 208, trans);
163 // enable color fading
164 if(!nofade) fade = 1;
171 trans = 7; // white in 8 color mode
173 trans = 0; // black in 8 color mode
178 init_pair(CP_WHITE, 0, trans);
179 init_pair(CP_BLACK, 7, 0);
181 init_pair(CP_WHITE, 7, trans);
182 init_pair(CP_BLACK, 0, 7);
184 init_pair(CP_BLUE, 4, trans);
185 init_pair(CP_RED, 1, trans);
186 init_pair(CP_YELLOW, 3, trans);
192 // set background color of main window
194 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
197 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
199 wbkgd(content, COLOR_PAIR(CP_WHITE));
207 // always resize window in case terminal geometry has changed
208 wresize(content, LINES - bar_top - bar_bottom, COLS);
213 offset = next_blank(line->text, 0) + 1;
214 // add text to header
216 0, (COLS - line->length + offset) / 2,
217 "%s", &line->text->text[offset]);
221 if(deck->headers > 1) {
222 line = deck->header->next;
223 offset = next_blank(line->text, 0) + 1;
224 // add text to left footer
227 "%s", &line->text->text[offset]);
229 // add slide number to right footer
231 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
232 "%d / %d", sc, deck->slides);
234 // make header + fooder visible
242 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
247 // make content visible
252 fade_in(content, trans, colors, invert);
254 // re-enable fading after any undefined key press
255 if(COLORS == 256 && !nofade) fade = 1;
257 // wait for user input
260 // evaluate user input
264 // show previous slide
267 case 8: // BACKSPACE (ascii)
268 case 127: // BACKSPACE (xterm)
269 case 263: // BACKSPACE (getty)
305 if(i <= deck->slides) {
322 // disable fading if slide n doesn't exist
335 for(i = sc; i <= deck->slides; i++) {
345 // do not fade out on exit
351 // disable fading on undefined key press
358 fade_out(content, trans, colors, invert);
366 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
367 int i = 0; // increment
368 char *c; // char pointer for iteration
369 char *special = "\\*_`"; // list of interpreted chars
370 cstack_t *stack = cstack_init();
372 if(line->text->text) {
373 int offset = 0; // text offset
376 if(CHECK_BIT(line->bits, IS_CODE)) {
378 // set static offset for code
379 offset = CODE_INDENT;
381 // reverse color for code blocks
383 wattron(window, COLOR_PAIR(CP_BLACK));
388 "%s", &line->text->text[offset]);
393 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
395 // set headline color
397 wattron(window, COLOR_PAIR(CP_BLUE));
399 // enable underline for H1
400 if(CHECK_BIT(line->bits, IS_H1))
401 wattron(window, A_UNDERLINE);
404 while(line->text->text[offset] == '#')
405 offset = next_word(line->text, offset);
410 "%s", &line->text->text[offset]);
412 wattroff(window, A_UNDERLINE);
415 // move the cursor in position
419 if(CHECK_BIT(line->bits, IS_QUOTE)) {
420 while(line->text->text[offset] == '>') {
421 // print a reverse color block
423 wattron(window, COLOR_PAIR(CP_BLACK));
424 wprintw(window, "%s", " ");
425 wattron(window, COLOR_PAIR(CP_WHITE));
426 wprintw(window, "%s", " ");
428 wprintw(window, "%s", ">");
431 // find next quote or break
433 if(line->text->text[offset] == ' ')
434 offset = next_word(line->text, offset);
438 // for each char in line
439 c = &line->text->text[offset];
442 // if char is in special char list
443 if(strchr(special, *c)) {
445 // closing special char (or second backslash)
446 if((stack->top)(stack, *c)) {
449 // print escaped backslash
451 wprintw(window, "%c", *c);
456 wattron(window, COLOR_PAIR(CP_WHITE));
460 wattroff(window, A_UNDERLINE);
462 // disable inline code
465 wattron(window, COLOR_PAIR(CP_WHITE));
469 // remove top special char from stack
472 // treat special as regular char
473 } else if((stack->top)(stack, '\\')) {
474 wprintw(window, "%c", *c);
476 // remove backslash from stack
479 // opening special char
485 wattron(window, COLOR_PAIR(CP_RED));
489 wattron(window, A_UNDERLINE);
491 // enable inline code
494 wattron(window, COLOR_PAIR(CP_BLACK));
496 // do nothing for backslashes
499 // push special char to stack
500 (stack->push)(stack, *c);
504 // remove backslash from stack
505 if((stack->top)(stack, '\\'))
508 // print regular char
509 wprintw(window, "%c", *c);
515 // pop stack until empty to prevent formated trailing spaces
516 while(!(stack->empty)(stack)) {
517 switch((stack->pop)(stack)) {
521 wattron(window, COLOR_PAIR(CP_WHITE));
525 wattroff(window, A_UNDERLINE);
527 // disable inline code
530 wattron(window, COLOR_PAIR(CP_WHITE));
532 // do nothing for backslashes
538 // fill rest off line with spaces
539 for(i = getcurx(window) - x; i < max_cols; i++)
540 wprintw(window, "%s", " ");
542 // reset to default color
544 wattron(window, COLOR_PAIR(CP_WHITE));
545 wattroff(window, A_UNDERLINE);
548 (stack->delete)(stack);
551 void fade_out(WINDOW *window, int trans, int colors, int invert) {
553 if(colors && COLORS == 256) {
554 for(i = 22; i >= 0; i--) {
558 init_pair(CP_WHITE, white_ramp_invert[i], trans);
559 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
560 init_pair(CP_RED, red_ramp_invert[i], trans);
561 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
563 init_pair(CP_WHITE, white_ramp[i], trans);
564 init_pair(CP_BLUE, blue_ramp[i], trans);
565 init_pair(CP_RED, red_ramp[i], trans);
566 init_pair(CP_BLACK, 16, white_ramp[i]);
569 // refresh window with new color
572 // delay for our eyes to recognize the change
578 void fade_in(WINDOW *window, int trans, int colors, int invert) {
580 if(colors && COLORS == 256) {
581 for(i = 0; i <= 23; i++) {
583 // brighten color pairs
585 init_pair(CP_WHITE, white_ramp_invert[i], trans);
586 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
587 init_pair(CP_RED, red_ramp_invert[i], trans);
588 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
590 init_pair(CP_WHITE, white_ramp[i], trans);
591 init_pair(CP_BLUE, blue_ramp[i], trans);
592 init_pair(CP_RED, red_ramp[i], trans);
593 init_pair(CP_BLACK, 16, white_ramp[i]);
596 // refresh window with new color
599 // delay for our eyes to recognize the change
605 int int_length (int val) {