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) 2018 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 <wchar.h> // wcschr
26 #include <wctype.h> // iswalnum
27 #include <string.h> // strcpy
28 #include <unistd.h> // usleep
29 #include <stdlib.h> // getenv
33 // color ramp for fading from black to color
34 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
35 237, 238, 239, 240, 241, 242,
36 244, 245, 246, 247, 248, 249,
37 250, 251, 252, 253, 254, 255 };
39 static short blue_ramp[24] = { 16, 17, 17, 18, 18, 19,
40 19, 20, 20, 21, 27, 33,
41 32, 39, 38, 45, 44, 44,
42 81, 81, 51, 51, 123, 123 };
44 static short red_ramp[24] = { 16, 52, 52, 53, 53, 89,
45 89, 90, 90, 126, 127, 127,
46 163, 163, 164, 164, 200, 200,
47 201, 201, 207, 207, 213, 213 };
49 // color ramp for fading from white to color
50 static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251,
51 250, 249, 248, 247, 246, 245,
52 243, 242, 241, 240, 239, 238,
53 237, 236, 235, 234, 233, 232};
55 static short blue_ramp_invert[24] = { 15, 231, 231, 195, 195, 159,
56 159, 123, 123, 87, 51, 44,
57 45, 38, 39, 32, 33, 33,
58 26, 26, 27, 27, 21, 21};
60 static short red_ramp_invert[24] = { 15, 231, 231, 224, 224, 225,
61 225, 218, 218, 219, 212, 213,
62 206, 207, 201, 200, 199, 199,
63 198, 198, 197, 197, 196, 196};
65 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum, int nocodebg) {
69 int l = 0; // line number
70 int lc = 0; // line count
71 int sc = 1; // slide count
72 int colors = 0; // amount of colors supported
73 int fade = 0; // disable color fading by default
74 int trans = -1; // enable transparency if term supports it
75 int max_lines = 0; // max lines per slide
76 int max_lines_slide = -1; // the slide that has the most lines
77 int max_cols = 0; // max columns per line
78 int offset; // text offset
79 int stop = 0; // passed stop bits per slide
81 // header line 1 is displayed at the top
82 int bar_top = (deck->headers > 0) ? 1 : 0;
83 // header line 2 is displayed at the bottom
84 // anyway we display the slide number at the bottom
85 int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0;
87 slide_t *slide = deck->slide;
97 while(line && line->text) {
99 if (line->text->value) {
100 lc += url_count_inline(line->text->value);
101 line->length -= url_len_inline(line->text->value);
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 fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
121 fwprintf(stderr, L"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);
146 if (lc == max_lines) {
147 max_lines_slide = sc;
150 slide->lines_consumed = lc;
156 if(max_lines + bar_top + bar_bottom > LINES) {
162 fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines for slide #%i.\n", LINES, max_lines + bar_top + bar_bottom, max_lines_slide);
163 fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
172 // disable output of keyboard typing
175 // make getch() process one char at a time
182 if(has_colors() == TRUE) {
184 use_default_colors();
191 trans = 15; // white in 256 color mode
193 trans = 16; // black in 256 color mode
198 init_pair(CP_WHITE, 232, trans);
199 init_pair(CP_BLUE, 21, trans);
200 init_pair(CP_RED, 196, trans);
201 init_pair(CP_BLACK, 15, 232);
203 init_pair(CP_WHITE, 255, trans);
204 init_pair(CP_BLUE, 123, trans);
205 init_pair(CP_RED, 213, trans);
206 init_pair(CP_BLACK, 16, 255);
208 init_pair(CP_YELLOW, 208, trans);
210 // enable color fading
219 trans = FG_COLOR; // white in 8 color mode
221 trans = BG_COLOR; // black in 8 color mode
226 init_pair(CP_WHITE, BG_COLOR, trans);
227 init_pair(CP_BLACK, FG_COLOR, BG_COLOR);
229 init_pair(CP_WHITE, FG_COLOR, trans);
230 init_pair(CP_BLACK, BG_COLOR, FG_COLOR);
232 init_pair(CP_BLUE, HEADER_COLOR, trans);
233 init_pair(CP_RED, BOLD_COLOR, trans);
234 init_pair(CP_YELLOW, TITLE_COLOR, trans);
240 // set background color for main window
242 wbkgd(stdscr, COLOR_PAIR(CP_WHITE));
244 // setup content window
245 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
247 // set background color of content window
249 wbkgd(content, COLOR_PAIR(CP_WHITE));
253 // find slide to reload
255 while(reload > 1 && reload <= deck->slides) {
261 // reset reload indicator
272 // always resize window in case terminal geometry has changed
273 wresize(content, LINES - bar_top - bar_bottom, COLS);
275 // set main window text color
277 wattron(stdscr, COLOR_PAIR(CP_YELLOW));
282 offset = next_blank(line->text, 0) + 1;
283 // add text to header
285 0, (COLS - line->length + offset) / 2,
286 &line->text->value[offset]);
290 if(deck->headers > 1) {
291 line = deck->header->next;
292 offset = next_blank(line->text, 0) + 1;
294 case 0: // add text to center footer
296 LINES - 1, (COLS - line->length + offset) / 2,
297 &line->text->value[offset]);
300 case 2: // add text to left footer
303 &line->text->value[offset]);
308 // add slide number to right footer
310 case 1: // show slide number only
312 LINES - 1, COLS - int_length(sc) - 3,
315 case 2: // show current slide & number of slides
317 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
318 "%d / %d", sc, deck->slides);
322 // copy changed lines in main window to virtual screen
323 wnoutrefresh(stdscr);
330 add_line(content, l + ((LINES - slide->lines_consumed - bar_top - bar_bottom) / 2),
331 (COLS - max_cols) / 2, line, max_cols, colors, nocodebg);
333 // raise stop counter if we pass a line having a stop bit
334 if(CHECK_BIT(line->bits, IS_STOP))
337 l += (line->length / COLS) + 1;
340 // only stop here if we didn't stop here recently
341 if(stop > slide->stop)
345 // print pandoc URL references
346 // only if we already printed all lines of the current slide (or output is stopped)
348 stop > slide->stop) {
350 getmaxyx( content, ymax, i );
351 for (i = 0; i < url_get_amount(); i++) {
352 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
354 waddwstr(content, url_get_target(i));
358 // copy changed lines in content window to virtual screen
359 wnoutrefresh(content);
361 // compare virtual screen to physical screen and does the actual updates
366 fade_in(content, trans, colors, invert);
368 // re-enable fading after any undefined key press
369 if(COLORS == 256 && !nofade)
372 // wait for user input
375 // evaluate user input
378 if (evaluate_binding(prev_slide_binding, c)) {
379 // show previous slide or stop bit
380 if(stop > 1 || (stop == 1 && !line)) {
381 // show current slide again
382 // but stop one stop bit earlier
387 // show previous slide
390 //stop on first bullet point always
398 } else if (evaluate_binding(next_slide_binding, c)) {
399 // show next slide or stop bit
401 // show current slide again
402 // but stop one stop bit later (or at end of slide)
415 } else if (isdigit(c) && c != '0') {
417 i = get_slide_number(c);
418 if(i > 0 && i <= deck->slides) {
435 } else if (evaluate_binding(first_slide_binding, c)) {
439 } else if (evaluate_binding(last_slide_binding, c)) {
441 for(i = sc; i <= deck->slides; i++) {
447 } else if (evaluate_binding(reload_binding, c)) {
454 // disable fading if reload is not possible
457 } else if (evaluate_binding(quit_binding, c)) {
459 // do not fade out on exit
465 // disable fading on undefined key press
471 fade_out(content, trans, colors, invert);
479 // free ncurses memory
484 // return reload indicator (0 means no reload)
488 void setup_list_strings(void)
492 /* utf8 can require 6 bytes */
493 if ((str = getenv("MDP_LIST_OPEN")) != NULL && strlen(str) <= 4*6) {
494 list_open1 = list_open2 = list_open3 = str;
496 if ((str = getenv("MDP_LIST_OPEN1")) != NULL && strlen(str) <= 4*6)
498 if ((str = getenv("MDP_LIST_OPEN2")) != NULL && strlen(str) <= 4*6)
500 if ((str = getenv("MDP_LIST_OPEN3")) != NULL && strlen(str) <= 4*6)
503 if ((str = getenv("MDP_LIST_HEAD")) != NULL && strlen(str) <= 4*6) {
504 list_head1 = list_head2 = list_head3 = str;
506 if ((str = getenv("MDP_LIST_HEAD1")) != NULL && strlen(str) <= 4*6)
508 if ((str = getenv("MDP_LIST_HEAD2")) != NULL && strlen(str) <= 4*6)
510 if ((str = getenv("MDP_LIST_HEAD3")) != NULL && strlen(str) <= 4*6)
515 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors, int nocodebg) {
518 int offset = 0; // text offset
520 // move the cursor in position
523 if(!line->text->value) {
525 // fill rest off line with spaces if we are in a code block
526 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
527 if(colors && !nocodebg)
528 wattron(window, COLOR_PAIR(CP_BLACK));
529 for(i = getcurx(window) - x; i < max_cols; i++)
530 wprintw(window, "%s", " ");
537 // IS_UNORDERED_LIST_3
538 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
539 offset = next_nonblank(line->text, 0);
541 int pos = 0, len, cnt;
542 len = sizeof(prompt) - pos;
543 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
544 pos += (cnt > len - 1 ? len - 1 : cnt);
545 len = sizeof(prompt) - pos;
546 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
547 pos += (cnt > len - 1 ? len - 1 : cnt);
548 len = sizeof(prompt) - pos;
550 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
551 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : " ");
553 snprintf(&prompt[pos], len, "%s", list_head3);
560 if(!CHECK_BIT(line->bits, IS_CODE))
561 inline_display(window, &line->text->value[offset], colors, nocodebg);
563 // IS_UNORDERED_LIST_2
564 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
565 offset = next_nonblank(line->text, 0);
567 int pos = 0, len, cnt;
568 len = sizeof(prompt) - pos;
569 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
570 pos += (cnt > len - 1 ? len - 1 : cnt);
571 len = sizeof(prompt) - pos;
573 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
574 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
576 snprintf(&prompt[pos], len, "%s", list_head2);
583 if(!CHECK_BIT(line->bits, IS_CODE))
584 inline_display(window, &line->text->value[offset], colors, nocodebg);
586 // IS_UNORDERED_LIST_1
587 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
588 offset = next_nonblank(line->text, 0);
591 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
592 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
594 strcpy(&prompt[0], list_head1);
601 if(!CHECK_BIT(line->bits, IS_CODE))
602 inline_display(window, &line->text->value[offset], colors, nocodebg);
606 if(CHECK_BIT(line->bits, IS_CODE)) {
608 if (!CHECK_BIT(line->bits, IS_TILDE_CODE) &&
609 !CHECK_BIT(line->bits, IS_GFM_CODE)) {
610 // set static offset for code
611 offset = CODE_INDENT;
614 // reverse color for code blocks
615 if(colors && !nocodebg)
616 wattron(window, COLOR_PAIR(CP_BLACK));
619 waddwstr(window, &line->text->value[offset]);
622 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
623 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
624 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
625 !CHECK_BIT(line->bits, IS_CODE)) {
628 if(CHECK_BIT(line->bits, IS_QUOTE)) {
629 while(line->text->value[offset] == '>') {
630 // print a reverse color block
632 wattron(window, COLOR_PAIR(CP_BLACK));
633 wprintw(window, "%s", " ");
634 wattron(window, COLOR_PAIR(CP_WHITE));
635 wprintw(window, "%s", " ");
637 wprintw(window, "%s", ">");
640 // find next quote or break
642 if(line->text->value[offset] == ' ')
643 offset = next_word(line->text, offset);
646 inline_display(window, &line->text->value[offset], colors, nocodebg);
650 if(CHECK_BIT(line->bits, IS_CENTER)) {
651 if(line->length < max_cols) {
652 wmove(window, y, x + ((max_cols - line->length) / 2));
657 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
659 // set headline color
661 wattron(window, COLOR_PAIR(CP_BLUE));
663 // enable underline for H1
664 if(CHECK_BIT(line->bits, IS_H1))
665 wattron(window, A_UNDERLINE);
668 while(line->text->value[offset] == '#')
669 offset = next_word(line->text, offset);
672 waddwstr(window, &line->text->value[offset]);
674 wattroff(window, A_UNDERLINE);
676 // no line-wide markdown
679 inline_display(window, &line->text->value[offset], colors, nocodebg);
684 // fill rest off line with spaces
685 // we only need this if the color is inverted (e.g. code-blocks),
686 // to ensure the background fades too
687 if(CHECK_BIT(line->bits, IS_CODE))
688 for(i = getcurx(window) - x; i < max_cols; i++)
689 wprintw(window, "%s", " ");
691 // reset to default color
693 wattron(window, COLOR_PAIR(CP_WHITE));
694 wattroff(window, A_UNDERLINE);
697 void inline_display(WINDOW *window, const wchar_t *c, const int colors, int nocodebg) {
698 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
699 const wchar_t *i = c; // iterator
700 const wchar_t *start_link_name, *start_url;
701 int length_link_name, url_num;
702 cstack_t *stack = cstack_init();
705 // for each char in line
708 // if char is in special char list
709 if(wcschr(special, *i)) {
711 // closing special char (or second backslash)
712 // only if not followed by :alnum:
713 if((stack->top)(stack, *i) &&
714 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
717 // print escaped backslash
719 waddnwstr(window, i, 1);
724 wattron(window, COLOR_PAIR(CP_WHITE));
728 wattroff(window, A_UNDERLINE);
730 // disable inline code
733 wattron(window, COLOR_PAIR(CP_WHITE));
737 // remove top special char from stack
740 // treat special as regular char
741 } else if((stack->top)(stack, L'\\')) {
742 waddnwstr(window, i, 1);
744 // remove backslash from stack
747 // opening special char
750 // emphasis or code span can start after new-line or space only
751 // and of cause after another emphasis markup
752 //TODO this condition looks ugly
754 iswspace(*(i - 1)) ||
755 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
756 ((i - 1) == c || iswspace(*(i - 2)))) ||
759 // url in pandoc style
760 if ((*i == L'[' && wcschr(i, L']')) ||
761 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
765 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
768 // turn higlighting and underlining on
770 wattron(window, COLOR_PAIR(CP_BLUE));
771 wattron(window, A_UNDERLINE);
775 // print the content of the label
776 // the label is printed as is
778 waddnwstr(window, i, 1);
780 } while (*i != L']');
782 length_link_name = i - 1 - start_link_name;
789 while (*i != L')') i++;
791 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
793 wprintw(window, " [%d]", url_num);
795 // turn highlighting and underlining off
796 wattroff(window, A_UNDERLINE);
797 wattron(window, COLOR_PAIR(CP_WHITE));
800 wprintw(window, "[");
807 wattron(window, COLOR_PAIR(CP_RED));
811 wattron(window, A_UNDERLINE);
813 // enable inline code
815 if(colors && !nocodebg)
816 wattron(window, COLOR_PAIR(CP_BLACK));
818 // do nothing for backslashes
821 // push special char to stack
822 (stack->push)(stack, *i);
825 waddnwstr(window, i, 1);
830 // remove backslash from stack
831 if((stack->top)(stack, L'\\'))
834 // print regular char
835 waddnwstr(window, i, 1);
839 // pop stack until empty to prevent formated trailing spaces
840 while(!(stack->empty)(stack)) {
841 switch((stack->pop)(stack)) {
845 wattron(window, COLOR_PAIR(CP_WHITE));
849 wattroff(window, A_UNDERLINE);
851 // disable inline code
854 wattron(window, COLOR_PAIR(CP_WHITE));
856 // do nothing for backslashes
860 (stack->delete)(stack);
863 void fade_out(WINDOW *window, int trans, int colors, int invert) {
865 if(colors && COLORS == 256) {
866 for(i = 22; i >= 0; i--) {
870 init_pair(CP_WHITE, white_ramp_invert[i], trans);
871 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
872 init_pair(CP_RED, red_ramp_invert[i], trans);
873 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
875 init_pair(CP_WHITE, white_ramp[i], trans);
876 init_pair(CP_BLUE, blue_ramp[i], trans);
877 init_pair(CP_RED, red_ramp[i], trans);
878 init_pair(CP_BLACK, 16, white_ramp[i]);
881 // refresh virtual screen with new color
882 wnoutrefresh(window);
884 // compare virtual screen to physical screen and does the actual updates
887 // delay for our eyes to recognize the change
893 void fade_in(WINDOW *window, int trans, int colors, int invert) {
895 if(colors && COLORS == 256) {
896 for(i = 0; i <= 23; i++) {
898 // brighten color pairs
900 init_pair(CP_WHITE, white_ramp_invert[i], trans);
901 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
902 init_pair(CP_RED, red_ramp_invert[i], trans);
903 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
905 init_pair(CP_WHITE, white_ramp[i], trans);
906 init_pair(CP_BLUE, blue_ramp[i], trans);
907 init_pair(CP_RED, red_ramp[i], trans);
908 init_pair(CP_BLACK, 16, white_ramp[i]);
911 // refresh virtual screen with new color
912 wnoutrefresh(window);
914 // compare virtual screen to physical screen and does the actual updates
917 // delay for our eyes to recognize the change
923 int int_length (int val) {
932 int get_slide_number(char init) {
933 int retval = init - '0';
935 // block for tenths of a second when using getch, ERR if no input
936 halfdelay(GOTO_SLIDE_DELAY);
937 while((c = getch()) != ERR) {
938 if (c < '0' || c > '9') {
942 retval = (retval * 10) + (c - '0');
944 nocbreak(); // cancel half delay mode
945 cbreak(); // go back to cbreak
949 bool evaluate_binding(const int bindings[], char c) {
952 while((binding = bindings[ind]) != 0) {
953 if (c == binding) return true;