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 <ctype.h> // isalnum
25 #include <locale.h> // setlocale
26 #include <string.h> // strchr
27 #include <unistd.h> // usleep
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 lc = 0; // line count
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;
86 // set locale to display UTF-8 correctly in ncurses
87 setlocale(LC_CTYPE, "");
98 if (line && line->text && line->text->text)
99 lc += url_count_inline(line->text->text);
101 if(line->length > COLS) {
106 i = prev_blank(line->text, offset + COLS) - offset;
108 // single word is > COLS
110 // calculate min_width
111 i = next_blank(line->text, offset + COLS) - offset;
117 fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
118 fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n");
124 max_cols = MAX(i, max_cols);
126 // iterate to next line
127 offset = prev_blank(line->text, offset + COLS);
128 i = line->length - offset;
131 // set max_cols one last time
132 max_cols = MAX(i, max_cols);
135 max_cols = MAX(line->length, max_cols);
141 max_lines = MAX(lc, max_lines);
147 if(max_lines + bar_top + bar_bottom > LINES) {
153 fprintf(stderr, "Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
154 fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
162 // disable output of keyboard typing
165 // make getch() process one char at a time
172 if(has_colors() == TRUE) {
174 use_default_colors();
181 trans = 15; // white in 256 color mode
183 trans = 16; // black in 256 color mode
188 init_pair(CP_WHITE, 232, trans);
189 init_pair(CP_BLUE, 21, trans);
190 init_pair(CP_RED, 196, trans);
191 init_pair(CP_BLACK, 15, 232);
193 init_pair(CP_WHITE, 255, trans);
194 init_pair(CP_BLUE, 123, trans);
195 init_pair(CP_RED, 213, trans);
196 init_pair(CP_BLACK, 16, 255);
198 init_pair(CP_YELLOW, 208, trans);
200 // enable color fading
209 trans = 7; // white in 8 color mode
211 trans = 0; // black in 8 color mode
216 init_pair(CP_WHITE, 0, trans);
217 init_pair(CP_BLACK, 7, 0);
219 init_pair(CP_WHITE, 7, trans);
220 init_pair(CP_BLACK, 0, 7);
222 init_pair(CP_BLUE, 4, trans);
223 init_pair(CP_RED, 1, trans);
224 init_pair(CP_YELLOW, 3, trans);
230 // set background color of main window
232 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
235 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
237 wbkgd(content, COLOR_PAIR(CP_WHITE));
248 // always resize window in case terminal geometry has changed
249 wresize(content, LINES - bar_top - bar_bottom, COLS);
254 offset = next_blank(line->text, 0) + 1;
255 // add text to header
257 0, (COLS - line->length + offset) / 2,
258 "%s", &line->text->text[offset]);
262 if(deck->headers > 1) {
263 line = deck->header->next;
264 offset = next_blank(line->text, 0) + 1;
265 // add text to left footer
268 "%s", &line->text->text[offset]);
270 // add slide number to right footer
272 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
273 "%d / %d", sc, deck->slides);
275 // make header + fooder visible
284 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
285 l += (line->length / COLS) + 1;
290 getmaxyx( content, ymax, i );
291 for (i = 0; i < url_get_amount(); i++) {
292 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
293 "[%d] %s", i, url_get_target(i));
296 // make content visible
301 fade_in(content, trans, colors, invert);
303 // re-enable fading after any undefined key press
304 if(COLORS == 256 && !nofade)
307 // wait for user input
310 // evaluate user input
314 // show previous slide
318 case 8: // BACKSPACE (ascii)
319 case 127: // BACKSPACE (xterm)
320 case 263: // BACKSPACE (getty)
357 if(i <= deck->slides) {
374 // disable fading if slide n doesn't exist
387 for(i = sc; i <= deck->slides; i++) {
397 // do not fade out on exit
403 // disable fading on undefined key press
410 fade_out(content, trans, colors, invert);
420 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
422 if(!line->text->text) {
427 int offset = 0; // text offset
429 // move the cursor in position
432 // IS_UNORDERED_LIST_3
433 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
434 offset = next_nonblank(line->text, 0);
436 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
437 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
439 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
440 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
442 strcpy(&prompt[8], " +- ");
449 if(!CHECK_BIT(line->bits, IS_CODE))
450 inline_display(window, &line->text->text[offset], colors);
452 // IS_UNORDERED_LIST_2
453 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
454 offset = next_nonblank(line->text, 0);
456 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
458 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
459 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
461 strcpy(&prompt[4], " +- ");
468 if(!CHECK_BIT(line->bits, IS_CODE))
469 inline_display(window, &line->text->text[offset], colors);
471 // IS_UNORDERED_LIST_1
472 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
473 offset = next_nonblank(line->text, 0);
476 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
477 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
479 strcpy(&prompt[0], " +- ");
486 if(!CHECK_BIT(line->bits, IS_CODE))
487 inline_display(window, &line->text->text[offset], colors);
491 if(CHECK_BIT(line->bits, IS_CODE)) {
493 // set static offset for code
494 offset = CODE_INDENT;
496 // reverse color for code blocks
498 wattron(window, COLOR_PAIR(CP_BLACK));
502 "%s", &line->text->text[offset]);
505 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
506 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
507 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
508 !CHECK_BIT(line->bits, IS_CODE)) {
511 if(CHECK_BIT(line->bits, IS_QUOTE)) {
512 while(line->text->text[offset] == '>') {
513 // print a reverse color block
515 wattron(window, COLOR_PAIR(CP_BLACK));
516 wprintw(window, "%s", " ");
517 wattron(window, COLOR_PAIR(CP_WHITE));
518 wprintw(window, "%s", " ");
520 wprintw(window, "%s", ">");
523 // find next quote or break
525 if(line->text->text[offset] == ' ')
526 offset = next_word(line->text, offset);
529 inline_display(window, &line->text->text[offset], colors);
533 if(CHECK_BIT(line->bits, IS_CENTER)) {
534 if(line->length < max_cols) {
535 wmove(window, y, x + ((max_cols - line->length) / 2));
540 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
542 // set headline color
544 wattron(window, COLOR_PAIR(CP_BLUE));
546 // enable underline for H1
547 if(CHECK_BIT(line->bits, IS_H1))
548 wattron(window, A_UNDERLINE);
551 while(line->text->text[offset] == '#')
552 offset = next_word(line->text, offset);
556 "%s", &line->text->text[offset]);
558 wattroff(window, A_UNDERLINE);
560 // no line-wide markdown
563 inline_display(window, &line->text->text[offset], colors);
568 // fill rest off line with spaces
569 for(i = getcurx(window) - x; i < max_cols; i++)
570 wprintw(window, "%s", " ");
572 // reset to default color
574 wattron(window, COLOR_PAIR(CP_WHITE));
575 wattroff(window, A_UNDERLINE);
578 void inline_display(WINDOW *window, const char *c, const int colors) {
579 const static char *special = "\\*_`!["; // list of interpreted chars
580 const char *i = c; // iterator
581 const char *start_link_name, *start_url;
582 int length_link_name, url_num;
583 cstack_t *stack = cstack_init();
586 // for each char in line
589 // if char is in special char list
590 if(strchr(special, *i)) {
592 // closing special char (or second backslash)
593 // only if not followed by :alnum:
594 if((stack->top)(stack, *i) &&
595 (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
598 // print escaped backslash
600 wprintw(window, "%c", *i);
605 wattron(window, COLOR_PAIR(CP_WHITE));
609 wattroff(window, A_UNDERLINE);
611 // disable inline code
614 wattron(window, COLOR_PAIR(CP_WHITE));
618 // remove top special char from stack
621 // treat special as regular char
622 } else if((stack->top)(stack, '\\')) {
623 wprintw(window, "%c", *i);
625 // remove backslash from stack
628 // opening special char
631 // emphasis or code span can start after new-line or space only
632 // and of cause after another emphasis markup
633 //TODO this condition looks ugly
636 ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
639 // url in pandoc style
640 if ((*i == '[' && strchr(i, ']')) ||
641 (*i == '!' && *(i + 1) == '[' && strchr(i, ']'))) {
645 if (strchr(i, ']')[1] == '(') {
648 // turn higlighting and underlining on
650 wattron(window, COLOR_PAIR(CP_BLUE));
651 wattron(window, A_UNDERLINE);
655 // print the content of the label
656 // the label is printed as is
658 wprintw(window, "%c", *i);
662 length_link_name = i - 1 - start_link_name;
669 while (*i != ')') i++;
671 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
673 wprintw(window, "[%d]", url_num);
675 // turn highlighting and underlining off
676 wattroff(window, A_UNDERLINE);
677 wattron(window, COLOR_PAIR(CP_WHITE));
680 wprintw(window, "[");
687 wattron(window, COLOR_PAIR(CP_RED));
691 wattron(window, A_UNDERLINE);
693 // enable inline code
696 wattron(window, COLOR_PAIR(CP_BLACK));
698 // do nothing for backslashes
701 // push special char to stack
702 (stack->push)(stack, *i);
705 wprintw(window, "%c", *i);
710 // remove backslash from stack
711 if((stack->top)(stack, '\\'))
714 // print regular char
715 wprintw(window, "%c", *i);
719 // pop stack until empty to prevent formated trailing spaces
720 while(!(stack->empty)(stack)) {
721 switch((stack->pop)(stack)) {
725 wattron(window, COLOR_PAIR(CP_WHITE));
729 wattroff(window, A_UNDERLINE);
731 // disable inline code
734 wattron(window, COLOR_PAIR(CP_WHITE));
736 // do nothing for backslashes
740 (stack->delete)(stack);
743 void fade_out(WINDOW *window, int trans, int colors, int invert) {
745 if(colors && COLORS == 256) {
746 for(i = 22; i >= 0; i--) {
750 init_pair(CP_WHITE, white_ramp_invert[i], trans);
751 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
752 init_pair(CP_RED, red_ramp_invert[i], trans);
753 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
755 init_pair(CP_WHITE, white_ramp[i], trans);
756 init_pair(CP_BLUE, blue_ramp[i], trans);
757 init_pair(CP_RED, red_ramp[i], trans);
758 init_pair(CP_BLACK, 16, white_ramp[i]);
761 // refresh window with new color
764 // delay for our eyes to recognize the change
770 void fade_in(WINDOW *window, int trans, int colors, int invert) {
772 if(colors && COLORS == 256) {
773 for(i = 0; i <= 23; i++) {
775 // brighten color pairs
777 init_pair(CP_WHITE, white_ramp_invert[i], trans);
778 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
779 init_pair(CP_RED, red_ramp_invert[i], trans);
780 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
782 init_pair(CP_WHITE, white_ramp[i], trans);
783 init_pair(CP_BLUE, blue_ramp[i], trans);
784 init_pair(CP_RED, red_ramp[i], trans);
785 init_pair(CP_BLACK, 16, white_ramp[i]);
788 // refresh window with new color
791 // delay for our eyes to recognize the change
797 int int_length (int val) {