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
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;
161 slide->lines_consumed = lc;
167 if(max_lines + bar_top + bar_bottom > LINES) {
173 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);
174 fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
183 // disable output of keyboard typing
186 // make getch() process one char at a time
193 if(has_colors() == TRUE) {
195 use_default_colors();
202 trans = 15; // white in 256 color mode
204 trans = 16; // black in 256 color mode
209 init_pair(CP_WHITE, 232, trans);
210 init_pair(CP_BLUE, 21, trans);
211 init_pair(CP_RED, 196, trans);
212 init_pair(CP_BLACK, 15, 232);
214 init_pair(CP_WHITE, 255, trans);
215 init_pair(CP_BLUE, 123, trans);
216 init_pair(CP_RED, 213, trans);
217 init_pair(CP_BLACK, 16, 255);
219 init_pair(CP_YELLOW, 208, trans);
221 // enable color fading
230 trans = 7; // white in 8 color mode
232 trans = 0; // black in 8 color mode
237 init_pair(CP_WHITE, 0, trans);
238 init_pair(CP_BLACK, 7, 0);
240 init_pair(CP_WHITE, 7, trans);
241 init_pair(CP_BLACK, 0, 7);
243 init_pair(CP_BLUE, 4, trans);
244 init_pair(CP_RED, 1, trans);
245 init_pair(CP_YELLOW, 3, trans);
251 // set background color for main window
253 wbkgd(stdscr, COLOR_PAIR(CP_WHITE));
255 // setup content window
256 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
258 // set background color of content window
260 wbkgd(content, COLOR_PAIR(CP_WHITE));
264 // find slide to reload
266 while(reload > 1 && reload <= deck->slides) {
272 // reset reload indicator
283 // always resize window in case terminal geometry has changed
284 wresize(content, LINES - bar_top - bar_bottom, COLS);
286 // set main window text color
288 wattron(stdscr, COLOR_PAIR(CP_YELLOW));
293 offset = next_blank(line->text, 0) + 1;
294 // add text to header
296 0, (COLS - line->length + offset) / 2,
297 &line->text->value[offset]);
301 if(deck->headers > 1) {
302 line = deck->header->next;
303 offset = next_blank(line->text, 0) + 1;
305 case 0: // add text to center footer
307 LINES - 1, (COLS - line->length + offset) / 2,
308 &line->text->value[offset]);
311 case 2: // add text to left footer
314 &line->text->value[offset]);
319 // add slide number to right footer
321 case 1: // show slide number only
323 LINES - 1, COLS - int_length(sc) - 3,
326 case 2: // show current slide & number of slides
328 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
329 "%d / %d", sc, deck->slides);
333 // copy changed lines in main window to virtual screen
334 wnoutrefresh(stdscr);
341 add_line(content, l + ((LINES - slide->lines_consumed - bar_top - bar_bottom) / 2),
342 (COLS - max_cols) / 2, line, max_cols, colors);
344 // raise stop counter if we pass a line having a stop bit
345 if(CHECK_BIT(line->bits, IS_STOP))
348 l += (line->length / COLS) + 1;
351 // only stop here if we didn't stop here recently
352 if(stop > slide->stop)
356 // print pandoc URL references
357 // only if we already printed all lines of the current slide (or output is stopped)
359 stop > slide->stop) {
361 getmaxyx( content, ymax, i );
362 for (i = 0; i < url_get_amount(); i++) {
363 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
365 waddwstr(content, url_get_target(i));
369 // copy changed lines in content window to virtual screen
370 wnoutrefresh(content);
372 // compare virtual screen to physical screen and does the actual updates
377 fade_in(content, trans, colors, invert);
379 // re-enable fading after any undefined key press
380 if(COLORS == 256 && !nofade)
383 // wait for user input
386 // evaluate user input
390 // show previous slide or stop bit
394 case 8: // BACKSPACE (ascii)
395 case 127: // BACKSPACE (xterm)
396 case 263: // BACKSPACE (getty)
399 if(stop > 1 || (stop == 1 && !line)) {
400 // show current slide again
401 // but stop one stop bit earlier
406 // show previous slide
409 //stop on first bullet point always
419 // show next slide or stop bit
428 // show current slide again
429 // but stop one stop bit later (or at end of slide)
454 i = get_slide_number(c);
455 if(i > 0 && i <= deck->slides) {
472 // disable fading if slide n doesn't exist
487 for(i = sc; i <= deck->slides; i++) {
502 // disable fading if reload is not possible
509 // do not fade out on exit
517 // disable fading on undefined key press
524 fade_out(content, trans, colors, invert);
532 // free ncurses memory
537 // return reload indicator (0 means no reload)
541 void setup_list_strings(void)
545 /* utf8 can require 6 bytes */
546 if ((str = getenv("MDP_LIST_OPEN")) != NULL && strlen(str) <= 4*6) {
547 list_open1 = list_open2 = list_open3 = str;
549 if ((str = getenv("MDP_LIST_OPEN1")) != NULL && strlen(str) <= 4*6)
551 if ((str = getenv("MDP_LIST_OPEN2")) != NULL && strlen(str) <= 4*6)
553 if ((str = getenv("MDP_LIST_OPEN3")) != NULL && strlen(str) <= 4*6)
556 if ((str = getenv("MDP_LIST_HEAD")) != NULL && strlen(str) <= 4*6) {
557 list_head1 = list_head2 = list_head3 = str;
559 if ((str = getenv("MDP_LIST_HEAD1")) != NULL && strlen(str) <= 4*6)
561 if ((str = getenv("MDP_LIST_HEAD2")) != NULL && strlen(str) <= 4*6)
563 if ((str = getenv("MDP_LIST_HEAD3")) != NULL && strlen(str) <= 4*6)
568 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
571 int offset = 0; // text offset
573 // move the cursor in position
576 if(!line->text->value) {
578 // fill rest off line with spaces if we are in a code block
579 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
581 wattron(window, COLOR_PAIR(CP_BLACK));
582 for(i = getcurx(window) - x; i < max_cols; i++)
583 wprintw(window, "%s", " ");
590 // IS_UNORDERED_LIST_3
591 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
592 offset = next_nonblank(line->text, 0);
594 int pos = 0, len, cnt;
595 len = sizeof(prompt) - pos;
596 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
597 pos += (cnt > len - 1 ? len - 1 : cnt);
598 len = sizeof(prompt) - pos;
599 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
600 pos += (cnt > len - 1 ? len - 1 : cnt);
601 len = sizeof(prompt) - pos;
603 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
604 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : " ");
606 snprintf(&prompt[pos], len, "%s", list_head3);
613 if(!CHECK_BIT(line->bits, IS_CODE))
614 inline_display(window, &line->text->value[offset], colors);
616 // IS_UNORDERED_LIST_2
617 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
618 offset = next_nonblank(line->text, 0);
620 int pos = 0, len, cnt;
621 len = sizeof(prompt) - pos;
622 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
623 pos += (cnt > len - 1 ? len - 1 : cnt);
624 len = sizeof(prompt) - pos;
626 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
627 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
629 snprintf(&prompt[pos], len, "%s", list_head2);
636 if(!CHECK_BIT(line->bits, IS_CODE))
637 inline_display(window, &line->text->value[offset], colors);
639 // IS_UNORDERED_LIST_1
640 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
641 offset = next_nonblank(line->text, 0);
644 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
645 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
647 strcpy(&prompt[0], list_head1);
654 if(!CHECK_BIT(line->bits, IS_CODE))
655 inline_display(window, &line->text->value[offset], colors);
659 if(CHECK_BIT(line->bits, IS_CODE)) {
661 if (!CHECK_BIT(line->bits, IS_TILDE_CODE) &&
662 !CHECK_BIT(line->bits, IS_GFM_CODE)) {
663 // set static offset for code
664 offset = CODE_INDENT;
667 // reverse color for code blocks
669 wattron(window, COLOR_PAIR(CP_BLACK));
672 waddwstr(window, &line->text->value[offset]);
675 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
676 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
677 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
678 !CHECK_BIT(line->bits, IS_CODE)) {
681 if(CHECK_BIT(line->bits, IS_QUOTE)) {
682 while(line->text->value[offset] == '>') {
683 // print a reverse color block
685 wattron(window, COLOR_PAIR(CP_BLACK));
686 wprintw(window, "%s", " ");
687 wattron(window, COLOR_PAIR(CP_WHITE));
688 wprintw(window, "%s", " ");
690 wprintw(window, "%s", ">");
693 // find next quote or break
695 if(line->text->value[offset] == ' ')
696 offset = next_word(line->text, offset);
699 inline_display(window, &line->text->value[offset], colors);
703 if(CHECK_BIT(line->bits, IS_CENTER)) {
704 if(line->length < max_cols) {
705 wmove(window, y, x + ((max_cols - line->length) / 2));
710 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
712 // set headline color
714 wattron(window, COLOR_PAIR(CP_BLUE));
716 // enable underline for H1
717 if(CHECK_BIT(line->bits, IS_H1))
718 wattron(window, A_UNDERLINE);
721 while(line->text->value[offset] == '#')
722 offset = next_word(line->text, offset);
725 waddwstr(window, &line->text->value[offset]);
727 wattroff(window, A_UNDERLINE);
729 // no line-wide markdown
732 inline_display(window, &line->text->value[offset], colors);
737 // fill rest off line with spaces
738 // we only need this if the color is inverted (e.g. code-blocks),
739 // to ensure the background fades too
740 if(CHECK_BIT(line->bits, IS_CODE))
741 for(i = getcurx(window) - x; i < max_cols; i++)
742 wprintw(window, "%s", " ");
744 // reset to default color
746 wattron(window, COLOR_PAIR(CP_WHITE));
747 wattroff(window, A_UNDERLINE);
750 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
751 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
752 const wchar_t *i = c; // iterator
753 const wchar_t *start_link_name, *start_url;
754 int length_link_name, url_num;
755 cstack_t *stack = cstack_init();
758 // for each char in line
761 // if char is in special char list
762 if(wcschr(special, *i)) {
764 // closing special char (or second backslash)
765 // only if not followed by :alnum:
766 if((stack->top)(stack, *i) &&
767 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
770 // print escaped backslash
772 waddnwstr(window, i, 1);
777 wattron(window, COLOR_PAIR(CP_WHITE));
781 wattroff(window, A_UNDERLINE);
783 // disable inline code
786 wattron(window, COLOR_PAIR(CP_WHITE));
790 // remove top special char from stack
793 // treat special as regular char
794 } else if((stack->top)(stack, L'\\')) {
795 waddnwstr(window, i, 1);
797 // remove backslash from stack
800 // opening special char
803 // emphasis or code span can start after new-line or space only
804 // and of cause after another emphasis markup
805 //TODO this condition looks ugly
807 iswspace(*(i - 1)) ||
808 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
809 ((i - 1) == c || iswspace(*(i - 2)))) ||
812 // url in pandoc style
813 if ((*i == L'[' && wcschr(i, L']')) ||
814 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
818 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
821 // turn higlighting and underlining on
823 wattron(window, COLOR_PAIR(CP_BLUE));
824 wattron(window, A_UNDERLINE);
828 // print the content of the label
829 // the label is printed as is
831 waddnwstr(window, i, 1);
833 } while (*i != L']');
835 length_link_name = i - 1 - start_link_name;
842 while (*i != L')') i++;
844 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
846 wprintw(window, " [%d]", url_num);
848 // turn highlighting and underlining off
849 wattroff(window, A_UNDERLINE);
850 wattron(window, COLOR_PAIR(CP_WHITE));
853 wprintw(window, "[");
860 wattron(window, COLOR_PAIR(CP_RED));
864 wattron(window, A_UNDERLINE);
866 // enable inline code
869 wattron(window, COLOR_PAIR(CP_BLACK));
871 // do nothing for backslashes
874 // push special char to stack
875 (stack->push)(stack, *i);
878 waddnwstr(window, i, 1);
883 // remove backslash from stack
884 if((stack->top)(stack, L'\\'))
887 // print regular char
888 waddnwstr(window, i, 1);
892 // pop stack until empty to prevent formated trailing spaces
893 while(!(stack->empty)(stack)) {
894 switch((stack->pop)(stack)) {
898 wattron(window, COLOR_PAIR(CP_WHITE));
902 wattroff(window, A_UNDERLINE);
904 // disable inline code
907 wattron(window, COLOR_PAIR(CP_WHITE));
909 // do nothing for backslashes
913 (stack->delete)(stack);
916 void fade_out(WINDOW *window, int trans, int colors, int invert) {
918 if(colors && COLORS == 256) {
919 for(i = 22; i >= 0; i--) {
923 init_pair(CP_WHITE, white_ramp_invert[i], trans);
924 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
925 init_pair(CP_RED, red_ramp_invert[i], trans);
926 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
928 init_pair(CP_WHITE, white_ramp[i], trans);
929 init_pair(CP_BLUE, blue_ramp[i], trans);
930 init_pair(CP_RED, red_ramp[i], trans);
931 init_pair(CP_BLACK, 16, white_ramp[i]);
934 // refresh virtual screen with new color
935 wnoutrefresh(window);
937 // compare virtual screen to physical screen and does the actual updates
940 // delay for our eyes to recognize the change
946 void fade_in(WINDOW *window, int trans, int colors, int invert) {
948 if(colors && COLORS == 256) {
949 for(i = 0; i <= 23; i++) {
951 // brighten color pairs
953 init_pair(CP_WHITE, white_ramp_invert[i], trans);
954 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
955 init_pair(CP_RED, red_ramp_invert[i], trans);
956 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
958 init_pair(CP_WHITE, white_ramp[i], trans);
959 init_pair(CP_BLUE, blue_ramp[i], trans);
960 init_pair(CP_RED, red_ramp[i], trans);
961 init_pair(CP_BLACK, 16, white_ramp[i]);
964 // refresh virtual screen with new color
965 wnoutrefresh(window);
967 // compare virtual screen to physical screen and does the actual updates
970 // delay for our eyes to recognize the change
976 int int_length (int val) {
985 int get_slide_number(char init) {
986 int retval = init - '0';
988 // block for tenths of a second when using getch, ERR if no input
989 halfdelay(GOTO_SLIDE_DELAY);
990 while((c = getch()) != ERR) {
991 if (c < '0' || c > '9') {
995 retval = (retval * 10) + (c - '0');
997 nocbreak(); // cancel half delay mode
998 cbreak(); // go back to cbreak