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) 2016 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
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 // unordered list characters
66 // override via env vars:
67 // export MDP_LIST_OPEN1=" " MDP_LIST_OPEN2=" " MDP_LIST_OPEN3=" "
68 // export MDP_LIST_HEAD1=" ■ " MDP_LIST_HEAD2=" ● " MDP_LIST_HEAD3=" ▫ "
69 static const char *list_open1 = " | ";
70 static const char *list_open2 = " | ";
71 static const char *list_open3 = " | ";
72 static const char *list_head1 = " +- ";
73 static const char *list_head2 = " +- ";
74 static const char *list_head3 = " +- ";
76 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum) {
80 int l = 0; // line number
81 int lc = 0; // line count
82 int sc = 1; // slide count
83 int colors = 0; // amount of colors supported
84 int fade = 0; // disable color fading by default
85 int trans = -1; // enable transparency if term supports it
86 int max_lines = 0; // max lines per slide
87 int max_cols = 0; // max columns per line
88 int offset; // text offset
89 int stop = 0; // passed stop bits per slide
91 // header line 1 is displayed at the top
92 int bar_top = (deck->headers > 0) ? 1 : 0;
93 // header line 2 is displayed at the bottom
94 // anyway we display the slide number at the bottom
95 int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0;
97 slide_t *slide = deck->slide;
107 while(line && line->text) {
109 if (line->text->value) {
110 lc += url_count_inline(line->text->value);
111 line->length -= url_len_inline(line->text->value);
114 if(line->length > COLS) {
119 i = prev_blank(line->text, offset + COLS) - offset;
121 // single word is > COLS
123 // calculate min_width
124 i = next_blank(line->text, offset + COLS) - offset;
130 fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
131 fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n");
138 max_cols = MAX(i, max_cols);
140 // iterate to next line
141 offset = prev_blank(line->text, offset + COLS);
142 i = line->length - offset;
145 // set max_cols one last time
146 max_cols = MAX(i, max_cols);
149 max_cols = MAX(line->length, max_cols);
155 max_lines = MAX(lc, max_lines);
161 if(max_lines + bar_top + bar_bottom > LINES) {
167 fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
168 fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
177 // disable output of keyboard typing
180 // make getch() process one char at a time
187 if(has_colors() == TRUE) {
189 use_default_colors();
196 trans = 15; // white in 256 color mode
198 trans = 16; // black in 256 color mode
203 init_pair(CP_WHITE, 232, trans);
204 init_pair(CP_BLUE, 21, trans);
205 init_pair(CP_RED, 196, trans);
206 init_pair(CP_BLACK, 15, 232);
208 init_pair(CP_WHITE, 255, trans);
209 init_pair(CP_BLUE, 123, trans);
210 init_pair(CP_RED, 213, trans);
211 init_pair(CP_BLACK, 16, 255);
213 init_pair(CP_YELLOW, 208, trans);
215 // enable color fading
224 trans = 7; // white in 8 color mode
226 trans = 0; // black in 8 color mode
231 init_pair(CP_WHITE, 0, trans);
232 init_pair(CP_BLACK, 7, 0);
234 init_pair(CP_WHITE, 7, trans);
235 init_pair(CP_BLACK, 0, 7);
237 init_pair(CP_BLUE, 4, trans);
238 init_pair(CP_RED, 1, trans);
239 init_pair(CP_YELLOW, 3, trans);
245 // set background color for main window
247 wbkgd(stdscr, COLOR_PAIR(CP_WHITE));
249 // setup content window
250 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
252 // set background color of content window
254 wbkgd(content, COLOR_PAIR(CP_WHITE));
258 // find slide to reload
259 while(reload > 1 && reload <= deck->slides) {
265 // reset reload indicator
276 // always resize window in case terminal geometry has changed
277 wresize(content, LINES - bar_top - bar_bottom, COLS);
279 // set main window text color
281 wattron(stdscr, COLOR_PAIR(CP_YELLOW));
286 offset = next_blank(line->text, 0) + 1;
287 // add text to header
289 0, (COLS - line->length + offset) / 2,
290 &line->text->value[offset]);
294 if(deck->headers > 1) {
295 line = deck->header->next;
296 offset = next_blank(line->text, 0) + 1;
298 case 0: // add text to center footer
300 LINES - 1, (COLS - line->length + offset) / 2,
301 &line->text->value[offset]);
304 case 2: // add text to left footer
307 &line->text->value[offset]);
312 // add slide number to right footer
314 case 1: // show slide number only
316 LINES - 1, COLS - int_length(sc) - 3,
319 case 2: // show current slide & number of slides
321 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
322 "%d / %d", sc, deck->slides);
326 // copy changed lines in main window to virtual screen
327 wnoutrefresh(stdscr);
334 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
336 // raise stop counter if we pass a line having a stop bit
337 if(CHECK_BIT(line->bits, IS_STOP))
340 l += (line->length / COLS) + 1;
343 // only stop here if we didn't stop here recently
344 if(stop > slide->stop)
348 // print pandoc URL references
349 // only if we already printed all lines of the current slide (or output is stopped)
351 stop > slide->stop) {
353 getmaxyx( content, ymax, i );
354 for (i = 0; i < url_get_amount(); i++) {
355 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
357 waddwstr(content, url_get_target(i));
361 // copy changed lines in content window to virtual screen
362 wnoutrefresh(content);
364 // compare virtual screen to physical screen and does the actual updates
369 fade_in(content, trans, colors, invert);
371 // re-enable fading after any undefined key press
372 if(COLORS == 256 && !nofade)
375 // wait for user input
378 // evaluate user input
382 // show previous slide or stop bit
386 case 8: // BACKSPACE (ascii)
387 case 127: // BACKSPACE (xterm)
388 case 263: // BACKSPACE (getty)
391 if(stop > 1 || (stop == 1 && !line)) {
392 // show current slide again
393 // but stop one stop bit earlier
398 // show previous slide
401 //stop on first bullet point always
411 // show next slide or stop bit
420 // show current slide again
421 // but stop one stop bit later (or at end of slide)
446 i = get_slide_number(c);
447 if(i > 0 && i <= deck->slides) {
464 // disable fading if slide n doesn't exist
479 for(i = sc; i <= deck->slides; i++) {
494 // disable fading if reload is not possible
501 // do not fade out on exit
509 // disable fading on undefined key press
516 fade_out(content, trans, colors, invert);
524 // free ncurses memory
529 // return reload indicator (0 means no reload)
533 void setup_list_strings(void)
537 /* utf8 can require 6 bytes */
538 if ((str = getenv("MDP_LIST_OPEN")) != NULL && strlen(str) <= 4*6) {
539 list_open1 = list_open2 = list_open3 = str;
541 if ((str = getenv("MDP_LIST_OPEN1")) != NULL && strlen(str) <= 4*6)
543 if ((str = getenv("MDP_LIST_OPEN2")) != NULL && strlen(str) <= 4*6)
545 if ((str = getenv("MDP_LIST_OPEN3")) != NULL && strlen(str) <= 4*6)
548 if ((str = getenv("MDP_LIST_HEAD")) != NULL && strlen(str) <= 4*6) {
549 list_head1 = list_head2 = list_head3 = str;
551 if ((str = getenv("MDP_LIST_HEAD1")) != NULL && strlen(str) <= 4*6)
553 if ((str = getenv("MDP_LIST_HEAD2")) != NULL && strlen(str) <= 4*6)
555 if ((str = getenv("MDP_LIST_HEAD3")) != NULL && strlen(str) <= 4*6)
560 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
563 int offset = 0; // text offset
565 // move the cursor in position
568 if(!line->text->value) {
570 // fill rest off line with spaces if we are in a code block
571 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
573 wattron(window, COLOR_PAIR(CP_BLACK));
574 for(i = getcurx(window) - x; i < max_cols; i++)
575 wprintw(window, "%s", " ");
582 // IS_UNORDERED_LIST_3
583 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
584 offset = next_nonblank(line->text, 0);
586 int pos = 0, len, cnt;
587 len = sizeof(prompt) - pos;
588 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
589 pos += (cnt > len - 1 ? len - 1 : cnt);
590 len = sizeof(prompt) - pos;
591 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
592 pos += (cnt > len - 1 ? len - 1 : cnt);
593 len = sizeof(prompt) - pos;
595 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
596 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : " ");
598 snprintf(&prompt[pos], len, "%s", list_head3);
605 if(!CHECK_BIT(line->bits, IS_CODE))
606 inline_display(window, &line->text->value[offset], colors);
608 // IS_UNORDERED_LIST_2
609 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
610 offset = next_nonblank(line->text, 0);
612 int pos = 0, len, cnt;
613 len = sizeof(prompt) - pos;
614 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
615 pos += (cnt > len - 1 ? len - 1 : cnt);
616 len = sizeof(prompt) - pos;
618 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
619 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
621 snprintf(&prompt[pos], len, "%s", list_head2);
628 if(!CHECK_BIT(line->bits, IS_CODE))
629 inline_display(window, &line->text->value[offset], colors);
631 // IS_UNORDERED_LIST_1
632 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
633 offset = next_nonblank(line->text, 0);
636 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
637 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
639 strcpy(&prompt[0], list_head1);
646 if(!CHECK_BIT(line->bits, IS_CODE))
647 inline_display(window, &line->text->value[offset], colors);
651 if(CHECK_BIT(line->bits, IS_CODE)) {
653 if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) {
654 // set static offset for code
655 offset = CODE_INDENT;
658 // reverse color for code blocks
660 wattron(window, COLOR_PAIR(CP_BLACK));
663 waddwstr(window, &line->text->value[offset]);
666 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
667 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
668 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
669 !CHECK_BIT(line->bits, IS_CODE)) {
672 if(CHECK_BIT(line->bits, IS_QUOTE)) {
673 while(line->text->value[offset] == '>') {
674 // print a reverse color block
676 wattron(window, COLOR_PAIR(CP_BLACK));
677 wprintw(window, "%s", " ");
678 wattron(window, COLOR_PAIR(CP_WHITE));
679 wprintw(window, "%s", " ");
681 wprintw(window, "%s", ">");
684 // find next quote or break
686 if(line->text->value[offset] == ' ')
687 offset = next_word(line->text, offset);
690 inline_display(window, &line->text->value[offset], colors);
694 if(CHECK_BIT(line->bits, IS_CENTER)) {
695 if(line->length < max_cols) {
696 wmove(window, y, x + ((max_cols - line->length) / 2));
701 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
703 // set headline color
705 wattron(window, COLOR_PAIR(CP_BLUE));
707 // enable underline for H1
708 if(CHECK_BIT(line->bits, IS_H1))
709 wattron(window, A_UNDERLINE);
712 while(line->text->value[offset] == '#')
713 offset = next_word(line->text, offset);
716 waddwstr(window, &line->text->value[offset]);
718 wattroff(window, A_UNDERLINE);
720 // no line-wide markdown
723 inline_display(window, &line->text->value[offset], colors);
728 // fill rest off line with spaces
729 // we only need this if the color is inverted (e.g. code-blocks),
730 // to ensure the background fades too
731 if(CHECK_BIT(line->bits, IS_CODE))
732 for(i = getcurx(window) - x; i < max_cols; i++)
733 wprintw(window, "%s", " ");
735 // reset to default color
737 wattron(window, COLOR_PAIR(CP_WHITE));
738 wattroff(window, A_UNDERLINE);
741 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
742 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
743 const wchar_t *i = c; // iterator
744 const wchar_t *start_link_name, *start_url;
745 int length_link_name, url_num;
746 cstack_t *stack = cstack_init();
749 // for each char in line
752 // if char is in special char list
753 if(wcschr(special, *i)) {
755 // closing special char (or second backslash)
756 // only if not followed by :alnum:
757 if((stack->top)(stack, *i) &&
758 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
761 // print escaped backslash
763 waddnwstr(window, i, 1);
768 wattron(window, COLOR_PAIR(CP_WHITE));
772 wattroff(window, A_UNDERLINE);
774 // disable inline code
777 wattron(window, COLOR_PAIR(CP_WHITE));
781 // remove top special char from stack
784 // treat special as regular char
785 } else if((stack->top)(stack, L'\\')) {
786 waddnwstr(window, i, 1);
788 // remove backslash from stack
791 // opening special char
794 // emphasis or code span can start after new-line or space only
795 // and of cause after another emphasis markup
796 //TODO this condition looks ugly
798 iswspace(*(i - 1)) ||
799 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
800 ((i - 1) == c || iswspace(*(i - 2)))) ||
803 // url in pandoc style
804 if ((*i == L'[' && wcschr(i, L']')) ||
805 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
809 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
812 // turn higlighting and underlining on
814 wattron(window, COLOR_PAIR(CP_BLUE));
815 wattron(window, A_UNDERLINE);
819 // print the content of the label
820 // the label is printed as is
822 waddnwstr(window, i, 1);
824 } while (*i != L']');
826 length_link_name = i - 1 - start_link_name;
833 while (*i != L')') i++;
835 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
837 wprintw(window, " [%d]", url_num);
839 // turn highlighting and underlining off
840 wattroff(window, A_UNDERLINE);
841 wattron(window, COLOR_PAIR(CP_WHITE));
844 wprintw(window, "[");
851 wattron(window, COLOR_PAIR(CP_RED));
855 wattron(window, A_UNDERLINE);
857 // enable inline code
860 wattron(window, COLOR_PAIR(CP_BLACK));
862 // do nothing for backslashes
865 // push special char to stack
866 (stack->push)(stack, *i);
869 waddnwstr(window, i, 1);
874 // remove backslash from stack
875 if((stack->top)(stack, L'\\'))
878 // print regular char
879 waddnwstr(window, i, 1);
883 // pop stack until empty to prevent formated trailing spaces
884 while(!(stack->empty)(stack)) {
885 switch((stack->pop)(stack)) {
889 wattron(window, COLOR_PAIR(CP_WHITE));
893 wattroff(window, A_UNDERLINE);
895 // disable inline code
898 wattron(window, COLOR_PAIR(CP_WHITE));
900 // do nothing for backslashes
904 (stack->delete)(stack);
907 void fade_out(WINDOW *window, int trans, int colors, int invert) {
909 if(colors && COLORS == 256) {
910 for(i = 22; i >= 0; i--) {
914 init_pair(CP_WHITE, white_ramp_invert[i], trans);
915 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
916 init_pair(CP_RED, red_ramp_invert[i], trans);
917 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
919 init_pair(CP_WHITE, white_ramp[i], trans);
920 init_pair(CP_BLUE, blue_ramp[i], trans);
921 init_pair(CP_RED, red_ramp[i], trans);
922 init_pair(CP_BLACK, 16, white_ramp[i]);
925 // refresh virtual screen with new color
926 wnoutrefresh(window);
928 // compare virtual screen to physical screen and does the actual updates
931 // delay for our eyes to recognize the change
937 void fade_in(WINDOW *window, int trans, int colors, int invert) {
939 if(colors && COLORS == 256) {
940 for(i = 0; i <= 23; i++) {
942 // brighten color pairs
944 init_pair(CP_WHITE, white_ramp_invert[i], trans);
945 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
946 init_pair(CP_RED, red_ramp_invert[i], trans);
947 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
949 init_pair(CP_WHITE, white_ramp[i], trans);
950 init_pair(CP_BLUE, blue_ramp[i], trans);
951 init_pair(CP_RED, red_ramp[i], trans);
952 init_pair(CP_BLACK, 16, white_ramp[i]);
955 // refresh virtual screen with new color
956 wnoutrefresh(window);
958 // compare virtual screen to physical screen and does the actual updates
961 // delay for our eyes to recognize the change
967 int int_length (int val) {
976 int get_slide_number(char init) {
977 int retval = init - '0';
979 // block for tenths of a second when using getch, ERR if no input
980 halfdelay(GOTO_SLIDE_DELAY);
981 while((c = getch()) != ERR) {
982 if (c < '0' || c > '9') {
986 retval = (retval * 10) + (c - '0');
988 nocbreak(); // cancel half delay mode
989 cbreak(); // go back to cbreak