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_lines_slide = -1; // the slide that has the most lines
88 int max_cols = 0; // max columns per line
89 int offset; // text offset
90 int stop = 0; // passed stop bits per slide
92 // header line 1 is displayed at the top
93 int bar_top = (deck->headers > 0) ? 1 : 0;
94 // header line 2 is displayed at the bottom
95 // anyway we display the slide number at the bottom
96 int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0;
98 slide_t *slide = deck->slide;
108 while(line && line->text) {
110 if (line->text->value) {
111 lc += url_count_inline(line->text->value);
112 line->length -= url_len_inline(line->text->value);
115 if(line->length > COLS) {
120 i = prev_blank(line->text, offset + COLS) - offset;
122 // single word is > COLS
124 // calculate min_width
125 i = next_blank(line->text, offset + COLS) - offset;
131 fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
132 fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n");
139 max_cols = MAX(i, max_cols);
141 // iterate to next line
142 offset = prev_blank(line->text, offset + COLS);
143 i = line->length - offset;
146 // set max_cols one last time
147 max_cols = MAX(i, max_cols);
150 max_cols = MAX(line->length, max_cols);
156 max_lines = MAX(lc, max_lines);
157 if (lc == max_lines) {
158 max_lines_slide = sc;
166 if(max_lines + bar_top + bar_bottom > LINES) {
172 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);
173 fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
182 // disable output of keyboard typing
185 // make getch() process one char at a time
192 if(has_colors() == TRUE) {
194 use_default_colors();
201 trans = 15; // white in 256 color mode
203 trans = 16; // black in 256 color mode
208 init_pair(CP_WHITE, 232, trans);
209 init_pair(CP_BLUE, 21, trans);
210 init_pair(CP_RED, 196, trans);
211 init_pair(CP_BLACK, 15, 232);
213 init_pair(CP_WHITE, 255, trans);
214 init_pair(CP_BLUE, 123, trans);
215 init_pair(CP_RED, 213, trans);
216 init_pair(CP_BLACK, 16, 255);
218 init_pair(CP_YELLOW, 208, trans);
220 // enable color fading
229 trans = 7; // white in 8 color mode
231 trans = 0; // black in 8 color mode
236 init_pair(CP_WHITE, 0, trans);
237 init_pair(CP_BLACK, 7, 0);
239 init_pair(CP_WHITE, 7, trans);
240 init_pair(CP_BLACK, 0, 7);
242 init_pair(CP_BLUE, 4, trans);
243 init_pair(CP_RED, 1, trans);
244 init_pair(CP_YELLOW, 3, trans);
250 // set background color for main window
252 wbkgd(stdscr, COLOR_PAIR(CP_WHITE));
254 // setup content window
255 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
257 // set background color of content window
259 wbkgd(content, COLOR_PAIR(CP_WHITE));
263 // find slide to reload
265 while(reload > 1 && reload <= deck->slides) {
271 // reset reload indicator
282 // always resize window in case terminal geometry has changed
283 wresize(content, LINES - bar_top - bar_bottom, COLS);
285 // set main window text color
287 wattron(stdscr, COLOR_PAIR(CP_YELLOW));
292 offset = next_blank(line->text, 0) + 1;
293 // add text to header
295 0, (COLS - line->length + offset) / 2,
296 &line->text->value[offset]);
300 if(deck->headers > 1) {
301 line = deck->header->next;
302 offset = next_blank(line->text, 0) + 1;
304 case 0: // add text to center footer
306 LINES - 1, (COLS - line->length + offset) / 2,
307 &line->text->value[offset]);
310 case 2: // add text to left footer
313 &line->text->value[offset]);
318 // add slide number to right footer
320 case 1: // show slide number only
322 LINES - 1, COLS - int_length(sc) - 3,
325 case 2: // show current slide & number of slides
327 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
328 "%d / %d", sc, deck->slides);
332 // copy changed lines in main window to virtual screen
333 wnoutrefresh(stdscr);
340 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
342 // raise stop counter if we pass a line having a stop bit
343 if(CHECK_BIT(line->bits, IS_STOP))
346 l += (line->length / COLS) + 1;
349 // only stop here if we didn't stop here recently
350 if(stop > slide->stop)
354 // print pandoc URL references
355 // only if we already printed all lines of the current slide (or output is stopped)
357 stop > slide->stop) {
359 getmaxyx( content, ymax, i );
360 for (i = 0; i < url_get_amount(); i++) {
361 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
363 waddwstr(content, url_get_target(i));
367 // copy changed lines in content window to virtual screen
368 wnoutrefresh(content);
370 // compare virtual screen to physical screen and does the actual updates
375 fade_in(content, trans, colors, invert);
377 // re-enable fading after any undefined key press
378 if(COLORS == 256 && !nofade)
381 // wait for user input
384 // evaluate user input
388 // show previous slide or stop bit
392 case 8: // BACKSPACE (ascii)
393 case 127: // BACKSPACE (xterm)
394 case 263: // BACKSPACE (getty)
397 if(stop > 1 || (stop == 1 && !line)) {
398 // show current slide again
399 // but stop one stop bit earlier
404 // show previous slide
407 //stop on first bullet point always
417 // show next slide or stop bit
426 // show current slide again
427 // but stop one stop bit later (or at end of slide)
452 i = get_slide_number(c);
453 if(i > 0 && i <= deck->slides) {
470 // disable fading if slide n doesn't exist
485 for(i = sc; i <= deck->slides; i++) {
500 // disable fading if reload is not possible
507 // do not fade out on exit
515 // disable fading on undefined key press
522 fade_out(content, trans, colors, invert);
530 // free ncurses memory
535 // return reload indicator (0 means no reload)
539 void setup_list_strings(void)
543 /* utf8 can require 6 bytes */
544 if ((str = getenv("MDP_LIST_OPEN")) != NULL && strlen(str) <= 4*6) {
545 list_open1 = list_open2 = list_open3 = str;
547 if ((str = getenv("MDP_LIST_OPEN1")) != NULL && strlen(str) <= 4*6)
549 if ((str = getenv("MDP_LIST_OPEN2")) != NULL && strlen(str) <= 4*6)
551 if ((str = getenv("MDP_LIST_OPEN3")) != NULL && strlen(str) <= 4*6)
554 if ((str = getenv("MDP_LIST_HEAD")) != NULL && strlen(str) <= 4*6) {
555 list_head1 = list_head2 = list_head3 = str;
557 if ((str = getenv("MDP_LIST_HEAD1")) != NULL && strlen(str) <= 4*6)
559 if ((str = getenv("MDP_LIST_HEAD2")) != NULL && strlen(str) <= 4*6)
561 if ((str = getenv("MDP_LIST_HEAD3")) != NULL && strlen(str) <= 4*6)
566 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
569 int offset = 0; // text offset
571 // move the cursor in position
574 if(!line->text->value) {
576 // fill rest off line with spaces if we are in a code block
577 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
579 wattron(window, COLOR_PAIR(CP_BLACK));
580 for(i = getcurx(window) - x; i < max_cols; i++)
581 wprintw(window, "%s", " ");
588 // IS_UNORDERED_LIST_3
589 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
590 offset = next_nonblank(line->text, 0);
592 int pos = 0, len, cnt;
593 len = sizeof(prompt) - pos;
594 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
595 pos += (cnt > len - 1 ? len - 1 : cnt);
596 len = sizeof(prompt) - pos;
597 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
598 pos += (cnt > len - 1 ? len - 1 : cnt);
599 len = sizeof(prompt) - pos;
601 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
602 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : " ");
604 snprintf(&prompt[pos], len, "%s", list_head3);
611 if(!CHECK_BIT(line->bits, IS_CODE))
612 inline_display(window, &line->text->value[offset], colors);
614 // IS_UNORDERED_LIST_2
615 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
616 offset = next_nonblank(line->text, 0);
618 int pos = 0, len, cnt;
619 len = sizeof(prompt) - pos;
620 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
621 pos += (cnt > len - 1 ? len - 1 : cnt);
622 len = sizeof(prompt) - pos;
624 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
625 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
627 snprintf(&prompt[pos], len, "%s", list_head2);
634 if(!CHECK_BIT(line->bits, IS_CODE))
635 inline_display(window, &line->text->value[offset], colors);
637 // IS_UNORDERED_LIST_1
638 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
639 offset = next_nonblank(line->text, 0);
642 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
643 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
645 strcpy(&prompt[0], list_head1);
652 if(!CHECK_BIT(line->bits, IS_CODE))
653 inline_display(window, &line->text->value[offset], colors);
657 if(CHECK_BIT(line->bits, IS_CODE)) {
659 if (!CHECK_BIT(line->bits, IS_TILDE_CODE) &&
660 !CHECK_BIT(line->bits, IS_GFM_CODE)) {
661 // set static offset for code
662 offset = CODE_INDENT;
665 // reverse color for code blocks
667 wattron(window, COLOR_PAIR(CP_BLACK));
670 waddwstr(window, &line->text->value[offset]);
673 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
674 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
675 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
676 !CHECK_BIT(line->bits, IS_CODE)) {
679 if(CHECK_BIT(line->bits, IS_QUOTE)) {
680 while(line->text->value[offset] == '>') {
681 // print a reverse color block
683 wattron(window, COLOR_PAIR(CP_BLACK));
684 wprintw(window, "%s", " ");
685 wattron(window, COLOR_PAIR(CP_WHITE));
686 wprintw(window, "%s", " ");
688 wprintw(window, "%s", ">");
691 // find next quote or break
693 if(line->text->value[offset] == ' ')
694 offset = next_word(line->text, offset);
697 inline_display(window, &line->text->value[offset], colors);
701 if(CHECK_BIT(line->bits, IS_CENTER)) {
702 if(line->length < max_cols) {
703 wmove(window, y, x + ((max_cols - line->length) / 2));
708 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
710 // set headline color
712 wattron(window, COLOR_PAIR(CP_BLUE));
714 // enable underline for H1
715 if(CHECK_BIT(line->bits, IS_H1))
716 wattron(window, A_UNDERLINE);
719 while(line->text->value[offset] == '#')
720 offset = next_word(line->text, offset);
723 waddwstr(window, &line->text->value[offset]);
725 wattroff(window, A_UNDERLINE);
727 // no line-wide markdown
730 inline_display(window, &line->text->value[offset], colors);
735 // fill rest off line with spaces
736 // we only need this if the color is inverted (e.g. code-blocks),
737 // to ensure the background fades too
738 if(CHECK_BIT(line->bits, IS_CODE))
739 for(i = getcurx(window) - x; i < max_cols; i++)
740 wprintw(window, "%s", " ");
742 // reset to default color
744 wattron(window, COLOR_PAIR(CP_WHITE));
745 wattroff(window, A_UNDERLINE);
748 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
749 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
750 const wchar_t *i = c; // iterator
751 const wchar_t *start_link_name, *start_url;
752 int length_link_name, url_num;
753 cstack_t *stack = cstack_init();
756 // for each char in line
759 // if char is in special char list
760 if(wcschr(special, *i)) {
762 // closing special char (or second backslash)
763 // only if not followed by :alnum:
764 if((stack->top)(stack, *i) &&
765 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
768 // print escaped backslash
770 waddnwstr(window, i, 1);
775 wattron(window, COLOR_PAIR(CP_WHITE));
779 wattroff(window, A_UNDERLINE);
781 // disable inline code
784 wattron(window, COLOR_PAIR(CP_WHITE));
788 // remove top special char from stack
791 // treat special as regular char
792 } else if((stack->top)(stack, L'\\')) {
793 waddnwstr(window, i, 1);
795 // remove backslash from stack
798 // opening special char
801 // emphasis or code span can start after new-line or space only
802 // and of cause after another emphasis markup
803 //TODO this condition looks ugly
805 iswspace(*(i - 1)) ||
806 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
807 ((i - 1) == c || iswspace(*(i - 2)))) ||
810 // url in pandoc style
811 if ((*i == L'[' && wcschr(i, L']')) ||
812 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
816 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
819 // turn higlighting and underlining on
821 wattron(window, COLOR_PAIR(CP_BLUE));
822 wattron(window, A_UNDERLINE);
826 // print the content of the label
827 // the label is printed as is
829 waddnwstr(window, i, 1);
831 } while (*i != L']');
833 length_link_name = i - 1 - start_link_name;
840 while (*i != L')') i++;
842 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
844 wprintw(window, " [%d]", url_num);
846 // turn highlighting and underlining off
847 wattroff(window, A_UNDERLINE);
848 wattron(window, COLOR_PAIR(CP_WHITE));
851 wprintw(window, "[");
858 wattron(window, COLOR_PAIR(CP_RED));
862 wattron(window, A_UNDERLINE);
864 // enable inline code
867 wattron(window, COLOR_PAIR(CP_BLACK));
869 // do nothing for backslashes
872 // push special char to stack
873 (stack->push)(stack, *i);
876 waddnwstr(window, i, 1);
881 // remove backslash from stack
882 if((stack->top)(stack, L'\\'))
885 // print regular char
886 waddnwstr(window, i, 1);
890 // pop stack until empty to prevent formated trailing spaces
891 while(!(stack->empty)(stack)) {
892 switch((stack->pop)(stack)) {
896 wattron(window, COLOR_PAIR(CP_WHITE));
900 wattroff(window, A_UNDERLINE);
902 // disable inline code
905 wattron(window, COLOR_PAIR(CP_WHITE));
907 // do nothing for backslashes
911 (stack->delete)(stack);
914 void fade_out(WINDOW *window, int trans, int colors, int invert) {
916 if(colors && COLORS == 256) {
917 for(i = 22; i >= 0; i--) {
921 init_pair(CP_WHITE, white_ramp_invert[i], trans);
922 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
923 init_pair(CP_RED, red_ramp_invert[i], trans);
924 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
926 init_pair(CP_WHITE, white_ramp[i], trans);
927 init_pair(CP_BLUE, blue_ramp[i], trans);
928 init_pair(CP_RED, red_ramp[i], trans);
929 init_pair(CP_BLACK, 16, white_ramp[i]);
932 // refresh virtual screen with new color
933 wnoutrefresh(window);
935 // compare virtual screen to physical screen and does the actual updates
938 // delay for our eyes to recognize the change
944 void fade_in(WINDOW *window, int trans, int colors, int invert) {
946 if(colors && COLORS == 256) {
947 for(i = 0; i <= 23; i++) {
949 // brighten color pairs
951 init_pair(CP_WHITE, white_ramp_invert[i], trans);
952 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
953 init_pair(CP_RED, red_ramp_invert[i], trans);
954 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
956 init_pair(CP_WHITE, white_ramp[i], trans);
957 init_pair(CP_BLUE, blue_ramp[i], trans);
958 init_pair(CP_RED, red_ramp[i], trans);
959 init_pair(CP_BLACK, 16, white_ramp[i]);
962 // refresh virtual screen with new color
963 wnoutrefresh(window);
965 // compare virtual screen to physical screen and does the actual updates
968 // delay for our eyes to recognize the change
974 int int_length (int val) {
983 int get_slide_number(char init) {
984 int retval = init - '0';
986 // block for tenths of a second when using getch, ERR if no input
987 halfdelay(GOTO_SLIDE_DELAY);
988 while((c = getch()) != ERR) {
989 if (c < '0' || c > '9') {
993 retval = (retval * 10) + (c - '0');
995 nocbreak(); // cancel half delay mode
996 cbreak(); // go back to cbreak