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 // color ramp for fading from black to color
33 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
34 237, 238, 239, 240, 241, 242,
35 244, 245, 246, 247, 248, 249,
36 250, 251, 252, 253, 254, 255 };
38 static short blue_ramp[24] = { 16, 17, 17, 18, 18, 19,
39 19, 20, 20, 21, 27, 33,
40 32, 39, 38, 45, 44, 44,
41 81, 81, 51, 51, 123, 123 };
43 static short red_ramp[24] = { 16, 52, 52, 53, 53, 89,
44 89, 90, 90, 126, 127, 127,
45 163, 163, 164, 164, 200, 200,
46 201, 201, 207, 207, 213, 213 };
48 // color ramp for fading from white to color
49 static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251,
50 250, 249, 248, 247, 246, 245,
51 243, 242, 241, 240, 239, 238,
52 237, 236, 235, 234, 233, 232};
54 static short blue_ramp_invert[24] = { 15, 231, 231, 195, 195, 159,
55 159, 123, 123, 87, 51, 44,
56 45, 38, 39, 32, 33, 33,
57 26, 26, 27, 27, 21, 21};
59 static short red_ramp_invert[24] = { 15, 231, 231, 224, 224, 225,
60 225, 218, 218, 219, 212, 213,
61 206, 207, 201, 200, 199, 199,
62 198, 198, 197, 197, 196, 196};
64 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) {
68 int l = 0; // line number
69 int sc = 1; // slide count
70 int colors = 0; // amount of colors supported
71 int fade = 0; // disable color fading by default
72 int trans = -1; // enable transparency if term supports it
73 int max_lines = 0; // max lines per slide
74 int max_cols = 0; // max columns per line
75 int offset; // text offset
77 // header line 1 is displayed at the top
78 int bar_top = (deck->headers > 0) ? 1 : 0;
79 // header line 2 is displayed at the bottom
80 // anyway we display the slide number at the bottom
83 slide_t *slide = deck->slide;
87 // set max_lines if line count exceeded
88 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
91 // set max_cols if length exceeded
92 max_cols = (line->length > max_cols) ? line->length : max_cols;
98 // set locale to display UTF-8 correctly in ncurses
99 setlocale(LC_CTYPE, "");
104 if((max_cols > COLS) ||
105 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
111 fprintf(stderr, "Error: Terminal size %ix%i too small. Need at least %ix%i.\n",
112 COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
114 // print hint to solve it
115 if(max_lines + bar_top + bar_bottom + 2 > LINES)
116 fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
118 fprintf(stderr, "Automatic line wrapping is not supported jet. You may need to shorten some lines by inserting line breaks.\n");
126 // disable output of keyboard typing
129 // make getch() process one char at a time
136 if(has_colors() == TRUE) {
138 use_default_colors();
145 trans = 15; // white in 256 color mode
147 trans = 16; // black in 256 color mode
152 init_pair(CP_WHITE, 232, trans);
153 init_pair(CP_BLUE, 21, trans);
154 init_pair(CP_RED, 196, trans);
156 init_pair(CP_WHITE, 255, trans);
157 init_pair(CP_BLUE, 123, trans);
158 init_pair(CP_RED, 213, trans);
160 init_pair(CP_YELLOW, 208, trans);
162 // enable color fading
163 if(!nofade) fade = 1;
170 trans = 7; // white in 8 color mode
172 trans = 0; // black in 8 color mode
177 init_pair(CP_WHITE, 0, trans);
179 init_pair(CP_WHITE, 7, trans);
181 init_pair(CP_BLUE, 4, trans);
182 init_pair(CP_RED, 1, trans);
183 init_pair(CP_YELLOW, 3, trans);
189 // set background color of main window
191 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
194 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
196 wbkgd(content, COLOR_PAIR(CP_WHITE));
204 // always resize window in case terminal geometry has changed
205 wresize(content, LINES - bar_top - bar_bottom, COLS);
210 offset = next_blank(line->text, 0) + 1;
211 // add text to header
213 0, (COLS - line->length + offset) / 2,
214 "%s", &line->text->text[offset]);
218 if(deck->headers > 1) {
219 line = deck->header->next;
220 offset = next_blank(line->text, 0) + 1;
221 // add text to left footer
224 "%s", &line->text->text[offset]);
226 // add slide number to right footer
228 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
229 "%d / %d", sc, deck->slides);
231 // make header + fooder visible
239 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
244 // make content visible
249 fade_in(content, trans, colors, invert);
251 // re-enable fading after any undefined key press
252 if(COLORS == 256 && !nofade) fade = 1;
254 // wait for user input
257 // evaluate user input
261 // show previous slide
264 case 8: // BACKSPACE (ascii)
265 case 127: // BACKSPACE (xterm)
266 case 263: // BACKSPACE (getty)
302 if(i <= deck->slides) {
319 // disable fading if slide n doesn't exist
332 for(i = sc; i <= deck->slides; i++) {
342 // do not fade out on exit
348 // disable fading on undefined key press
355 fade_out(content, trans, colors, invert);
363 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
364 int i = 0; // increment
365 char *c; // char pointer for iteration
366 char *special = "\\*_`"; // list of interpreted chars
367 cstack_t *stack = cstack_init();
369 if(line->text->text) {
370 int offset = 0; // text offset
373 if(CHECK_BIT(line->bits, IS_CODE)) {
375 // set static offset for code
376 offset = CODE_INDENT;
378 // reverse color for code blocks
379 wattron(window, A_REVERSE);
384 "%s", &line->text->text[offset]);
389 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
391 // set headline color
393 wattron(window, COLOR_PAIR(CP_BLUE));
395 // enable underline for H1
396 if(CHECK_BIT(line->bits, IS_H1))
397 wattron(window, A_UNDERLINE);
400 while(line->text->text[offset] == '#')
401 offset = next_word(line->text, offset);
406 "%s", &line->text->text[offset]);
408 wattroff(window, A_UNDERLINE);
411 // move the cursor in position
415 if(CHECK_BIT(line->bits, IS_QUOTE)) {
416 while(line->text->text[offset] == '>') {
417 // print a reverse color block
418 wattron(window, A_REVERSE);
419 wprintw(window, "%s", " ");
420 wattroff(window, A_REVERSE);
421 wprintw(window, "%s", " ");
422 // find next quote or break
424 if(line->text->text[offset] == ' ')
425 offset = next_word(line->text, offset);
429 // for each char in line
430 c = &line->text->text[offset];
433 // if char is in special char list
434 if(strchr(special, *c)) {
436 // closing special char (or second backslash)
437 if((stack->top)(stack, *c)) {
440 // print escaped backslash
442 wprintw(window, "%c", *c);
447 wattron(window, COLOR_PAIR(CP_WHITE));
451 wattroff(window, A_UNDERLINE);
453 // disable inline code
455 wattroff(window, A_REVERSE);
459 // remove top special char from stack
462 // treat special as regular char
463 } else if((stack->top)(stack, '\\')) {
464 wprintw(window, "%c", *c);
466 // remove backslash from stack
469 // opening special char
475 wattron(window, COLOR_PAIR(CP_RED));
479 wattron(window, A_UNDERLINE);
481 // enable inline code
483 wattron(window, A_REVERSE);
485 // do nothing for backslashes
488 // push special char to stack
489 (stack->push)(stack, *c);
493 // remove backslash from stack
494 if((stack->top)(stack, '\\'))
497 // print regular char
498 wprintw(window, "%c", *c);
504 // pop stack until empty to prevent formated trailing spaces
505 while(!(stack->empty)(stack)) {
506 switch((stack->pop)(stack)) {
510 wattron(window, COLOR_PAIR(CP_WHITE));
514 wattroff(window, A_UNDERLINE);
516 // disable inline code
518 wattroff(window, A_REVERSE);
520 // do nothing for backslashes
526 // fill rest off line with spaces
527 for(i = getcurx(window) - x; i < max_cols; i++)
528 wprintw(window, "%s", " ");
530 // reset to default color
532 wattron(window, COLOR_PAIR(CP_WHITE));
533 wattroff(window, A_UNDERLINE);
534 wattroff(window, A_REVERSE);
537 (stack->delete)(stack);
540 void fade_out(WINDOW *window, int trans, int colors, int invert) {
543 for(i = 22; i >= 0; i--) {
547 init_pair(CP_WHITE, white_ramp_invert[i], trans);
548 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
549 init_pair(CP_RED, red_ramp_invert[i], trans);
551 init_pair(CP_WHITE, white_ramp[i], trans);
552 init_pair(CP_BLUE, blue_ramp[i], trans);
553 init_pair(CP_RED, red_ramp[i], trans);
556 // refresh window with new color
559 // delay for our eyes to recognize the change
565 void fade_in(WINDOW *window, int trans, int colors, int invert) {
568 for(i = 0; i <= 23; i++) {
570 // brighten color pairs
572 init_pair(CP_WHITE, white_ramp_invert[i], trans);
573 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
574 init_pair(CP_RED, red_ramp_invert[i], trans);
576 init_pair(CP_WHITE, white_ramp[i], trans);
577 init_pair(CP_BLUE, blue_ramp[i], trans);
578 init_pair(CP_RED, red_ramp[i], trans);
581 // refresh window with new color
584 // delay for our eyes to recognize the change
590 int int_length (int val) {