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, int reload, int noreload) {
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, "");
96 while(line && line->text) {
99 lc += url_count_inline(line->text->text);
101 if (line->text->text)
102 line->length -= url_len_inline(line->text->text);
104 if(line->length > COLS) {
109 i = prev_blank(line->text, offset + COLS) - offset;
111 // single word is > COLS
113 // calculate min_width
114 i = next_blank(line->text, offset + COLS) - offset;
120 fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
121 fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n");
128 max_cols = MAX(i, max_cols);
130 // iterate to next line
131 offset = prev_blank(line->text, offset + COLS);
132 i = line->length - offset;
135 // set max_cols one last time
136 max_cols = MAX(i, max_cols);
139 max_cols = MAX(line->length, max_cols);
145 max_lines = MAX(lc, max_lines);
151 if(max_lines + bar_top + bar_bottom > LINES) {
157 fprintf(stderr, "Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
158 fprintf(stderr, "You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
167 // disable output of keyboard typing
170 // make getch() process one char at a time
177 if(has_colors() == TRUE) {
179 use_default_colors();
186 trans = 15; // white in 256 color mode
188 trans = 16; // black in 256 color mode
193 init_pair(CP_WHITE, 232, trans);
194 init_pair(CP_BLUE, 21, trans);
195 init_pair(CP_RED, 196, trans);
196 init_pair(CP_BLACK, 15, 232);
198 init_pair(CP_WHITE, 255, trans);
199 init_pair(CP_BLUE, 123, trans);
200 init_pair(CP_RED, 213, trans);
201 init_pair(CP_BLACK, 16, 255);
203 init_pair(CP_YELLOW, 208, trans);
205 // enable color fading
214 trans = 7; // white in 8 color mode
216 trans = 0; // black in 8 color mode
221 init_pair(CP_WHITE, 0, trans);
222 init_pair(CP_BLACK, 7, 0);
224 init_pair(CP_WHITE, 7, trans);
225 init_pair(CP_BLACK, 0, 7);
227 init_pair(CP_BLUE, 4, trans);
228 init_pair(CP_RED, 1, trans);
229 init_pair(CP_YELLOW, 3, trans);
235 // set background color of main window
237 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
240 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
242 wbkgd(content, COLOR_PAIR(CP_WHITE));
246 // find slide to reload
247 while(reload > 1 && reload <= deck->slides) {
253 // reset reload indicator
264 // always resize window in case terminal geometry has changed
265 wresize(content, LINES - bar_top - bar_bottom, COLS);
270 offset = next_blank(line->text, 0) + 1;
271 // add text to header
273 0, (COLS - line->length + offset) / 2,
274 "%s", &line->text->text[offset]);
278 if(deck->headers > 1) {
279 line = deck->header->next;
280 offset = next_blank(line->text, 0) + 1;
281 // add text to left footer
284 "%s", &line->text->text[offset]);
286 // add slide number to right footer
288 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
289 "%d / %d", sc, deck->slides);
291 // make header + fooder visible
300 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
301 l += (line->length / COLS) + 1;
306 getmaxyx( content, ymax, i );
307 for (i = 0; i < url_get_amount(); i++) {
308 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
309 "[%d] %s", i, url_get_target(i));
312 // make content visible
317 fade_in(content, trans, colors, invert);
319 // re-enable fading after any undefined key press
320 if(COLORS == 256 && !nofade)
323 // wait for user input
326 // evaluate user input
330 // show previous slide
334 case 8: // BACKSPACE (ascii)
335 case 127: // BACKSPACE (xterm)
336 case 263: // BACKSPACE (getty)
373 i = get_slide_number(c);
374 if(i > 0 && i <= deck->slides) {
391 // disable fading if slide n doesn't exist
406 for(i = sc; i <= deck->slides; i++) {
421 // disable fading if reload is not possible
428 // do not fade out on exit
436 // disable fading on undefined key press
443 fade_out(content, trans, colors, invert);
451 // free ncurses memory
456 // return reload indicator (0 means no reload)
460 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
462 if(!line->text->text) {
467 int offset = 0; // text offset
469 // move the cursor in position
472 // IS_UNORDERED_LIST_3
473 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
474 offset = next_nonblank(line->text, 0);
476 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
477 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
479 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
480 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
482 strcpy(&prompt[8], " +- ");
489 if(!CHECK_BIT(line->bits, IS_CODE))
490 inline_display(window, &line->text->text[offset], colors);
492 // IS_UNORDERED_LIST_2
493 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
494 offset = next_nonblank(line->text, 0);
496 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
498 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
499 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
501 strcpy(&prompt[4], " +- ");
508 if(!CHECK_BIT(line->bits, IS_CODE))
509 inline_display(window, &line->text->text[offset], colors);
511 // IS_UNORDERED_LIST_1
512 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
513 offset = next_nonblank(line->text, 0);
516 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
517 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
519 strcpy(&prompt[0], " +- ");
526 if(!CHECK_BIT(line->bits, IS_CODE))
527 inline_display(window, &line->text->text[offset], colors);
531 if(CHECK_BIT(line->bits, IS_CODE)) {
533 // set static offset for code
534 offset = CODE_INDENT;
536 // reverse color for code blocks
538 wattron(window, COLOR_PAIR(CP_BLACK));
542 "%s", &line->text->text[offset]);
545 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
546 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
547 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
548 !CHECK_BIT(line->bits, IS_CODE)) {
551 if(CHECK_BIT(line->bits, IS_QUOTE)) {
552 while(line->text->text[offset] == '>') {
553 // print a reverse color block
555 wattron(window, COLOR_PAIR(CP_BLACK));
556 wprintw(window, "%s", " ");
557 wattron(window, COLOR_PAIR(CP_WHITE));
558 wprintw(window, "%s", " ");
560 wprintw(window, "%s", ">");
563 // find next quote or break
565 if(line->text->text[offset] == ' ')
566 offset = next_word(line->text, offset);
569 inline_display(window, &line->text->text[offset], colors);
573 if(CHECK_BIT(line->bits, IS_CENTER)) {
574 if(line->length < max_cols) {
575 wmove(window, y, x + ((max_cols - line->length) / 2));
580 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
582 // set headline color
584 wattron(window, COLOR_PAIR(CP_BLUE));
586 // enable underline for H1
587 if(CHECK_BIT(line->bits, IS_H1))
588 wattron(window, A_UNDERLINE);
591 while(line->text->text[offset] == '#')
592 offset = next_word(line->text, offset);
596 "%s", &line->text->text[offset]);
598 wattroff(window, A_UNDERLINE);
600 // no line-wide markdown
603 inline_display(window, &line->text->text[offset], colors);
608 // fill rest off line with spaces
609 for(i = getcurx(window) - x; i < max_cols; i++)
610 wprintw(window, "%s", " ");
612 // reset to default color
614 wattron(window, COLOR_PAIR(CP_WHITE));
615 wattroff(window, A_UNDERLINE);
618 void inline_display(WINDOW *window, const char *c, const int colors) {
619 const static char *special = "\\*_`!["; // list of interpreted chars
620 const char *i = c; // iterator
621 const char *start_link_name, *start_url;
622 int length_link_name, url_num;
623 cstack_t *stack = cstack_init();
626 // for each char in line
629 // if char is in special char list
630 if(strchr(special, *i)) {
632 // closing special char (or second backslash)
633 // only if not followed by :alnum:
634 if((stack->top)(stack, *i) &&
635 (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
638 // print escaped backslash
640 wprintw(window, "%c", *i);
645 wattron(window, COLOR_PAIR(CP_WHITE));
649 wattroff(window, A_UNDERLINE);
651 // disable inline code
654 wattron(window, COLOR_PAIR(CP_WHITE));
658 // remove top special char from stack
661 // treat special as regular char
662 } else if((stack->top)(stack, '\\')) {
663 wprintw(window, "%c", *i);
665 // remove backslash from stack
668 // opening special char
671 // emphasis or code span can start after new-line or space only
672 // and of cause after another emphasis markup
673 //TODO this condition looks ugly
676 ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
679 // url in pandoc style
680 if ((*i == '[' && strchr(i, ']')) ||
681 (*i == '!' && *(i + 1) == '[' && strchr(i, ']'))) {
685 if (strchr(i, ']')[1] == '(') {
688 // turn higlighting and underlining on
690 wattron(window, COLOR_PAIR(CP_BLUE));
691 wattron(window, A_UNDERLINE);
695 // print the content of the label
696 // the label is printed as is
698 wprintw(window, "%c", *i);
702 length_link_name = i - 1 - start_link_name;
709 while (*i != ')') i++;
711 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
713 wprintw(window, " [%d]", url_num);
715 // turn highlighting and underlining off
716 wattroff(window, A_UNDERLINE);
717 wattron(window, COLOR_PAIR(CP_WHITE));
720 wprintw(window, "[");
727 wattron(window, COLOR_PAIR(CP_RED));
731 wattron(window, A_UNDERLINE);
733 // enable inline code
736 wattron(window, COLOR_PAIR(CP_BLACK));
738 // do nothing for backslashes
741 // push special char to stack
742 (stack->push)(stack, *i);
745 wprintw(window, "%c", *i);
750 // remove backslash from stack
751 if((stack->top)(stack, '\\'))
754 // print regular char
755 wprintw(window, "%c", *i);
759 // pop stack until empty to prevent formated trailing spaces
760 while(!(stack->empty)(stack)) {
761 switch((stack->pop)(stack)) {
765 wattron(window, COLOR_PAIR(CP_WHITE));
769 wattroff(window, A_UNDERLINE);
771 // disable inline code
774 wattron(window, COLOR_PAIR(CP_WHITE));
776 // do nothing for backslashes
780 (stack->delete)(stack);
783 void fade_out(WINDOW *window, int trans, int colors, int invert) {
785 if(colors && COLORS == 256) {
786 for(i = 22; i >= 0; i--) {
790 init_pair(CP_WHITE, white_ramp_invert[i], trans);
791 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
792 init_pair(CP_RED, red_ramp_invert[i], trans);
793 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
795 init_pair(CP_WHITE, white_ramp[i], trans);
796 init_pair(CP_BLUE, blue_ramp[i], trans);
797 init_pair(CP_RED, red_ramp[i], trans);
798 init_pair(CP_BLACK, 16, white_ramp[i]);
801 // refresh window with new color
804 // delay for our eyes to recognize the change
810 void fade_in(WINDOW *window, int trans, int colors, int invert) {
812 if(colors && COLORS == 256) {
813 for(i = 0; i <= 23; i++) {
815 // brighten color pairs
817 init_pair(CP_WHITE, white_ramp_invert[i], trans);
818 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
819 init_pair(CP_RED, red_ramp_invert[i], trans);
820 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
822 init_pair(CP_WHITE, white_ramp[i], trans);
823 init_pair(CP_BLUE, blue_ramp[i], trans);
824 init_pair(CP_RED, red_ramp[i], trans);
825 init_pair(CP_BLACK, 16, white_ramp[i]);
828 // refresh window with new color
831 // delay for our eyes to recognize the change
837 int int_length (int val) {
846 int get_slide_number(char init) {
847 int retval = init - '0';
849 // block for tenths of a second when using getch, ERR if no input
850 halfdelay(GOTO_SLIDE_DELAY);
851 while((c = getch()) != ERR) {
852 if (c < '0' || c > '9') {
856 retval = (retval * 10) + (c - '0');
858 nocbreak(); // cancel half delay mode
859 cbreak(); // go back to cbreak