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
352 getmaxyx( content, ymax, i );
353 for (i = 0; i < url_get_amount(); i++) {
354 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
356 waddwstr(content, url_get_target(i));
360 // copy changed lines in content window to virtual screen
361 wnoutrefresh(content);
363 // compare virtual screen to physical screen and does the actual updates
368 fade_in(content, trans, colors, invert);
370 // re-enable fading after any undefined key press
371 if(COLORS == 256 && !nofade)
374 // wait for user input
377 // evaluate user input
381 // show previous slide or stop bit
385 case 8: // BACKSPACE (ascii)
386 case 127: // BACKSPACE (xterm)
387 case 263: // BACKSPACE (getty)
390 if(stop > 1 || (stop == 1 && !line)) {
391 // show current slide again
392 // but stop one stop bit earlier
397 // show previous slide
400 //stop on first bullet point always
410 // show next slide or stop bit
419 // show current slide again
420 // but stop one stop bit later (or at end of slide)
445 i = get_slide_number(c);
446 if(i > 0 && i <= deck->slides) {
463 // disable fading if slide n doesn't exist
478 for(i = sc; i <= deck->slides; i++) {
493 // disable fading if reload is not possible
500 // do not fade out on exit
508 // disable fading on undefined key press
515 fade_out(content, trans, colors, invert);
523 // free ncurses memory
528 // return reload indicator (0 means no reload)
532 void setup_list_strings(void)
536 /* utf8 can require 6 bytes */
537 if ((str = getenv("MDP_LIST_OPEN")) != NULL && strlen(str) <= 4*6) {
538 list_open1 = list_open2 = list_open3 = str;
540 if ((str = getenv("MDP_LIST_OPEN1")) != NULL && strlen(str) <= 4*6)
542 if ((str = getenv("MDP_LIST_OPEN2")) != NULL && strlen(str) <= 4*6)
544 if ((str = getenv("MDP_LIST_OPEN3")) != NULL && strlen(str) <= 4*6)
547 if ((str = getenv("MDP_LIST_HEAD")) != NULL && strlen(str) <= 4*6) {
548 list_head1 = list_head2 = list_head3 = str;
550 if ((str = getenv("MDP_LIST_HEAD1")) != NULL && strlen(str) <= 4*6)
552 if ((str = getenv("MDP_LIST_HEAD2")) != NULL && strlen(str) <= 4*6)
554 if ((str = getenv("MDP_LIST_HEAD3")) != NULL && strlen(str) <= 4*6)
559 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
562 int offset = 0; // text offset
564 // move the cursor in position
567 if(!line->text->value) {
569 // fill rest off line with spaces if we are in a code block
570 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
572 wattron(window, COLOR_PAIR(CP_BLACK));
573 for(i = getcurx(window) - x; i < max_cols; i++)
574 wprintw(window, "%s", " ");
581 // IS_UNORDERED_LIST_3
582 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
583 offset = next_nonblank(line->text, 0);
585 int pos = 0, len, cnt;
586 len = sizeof(prompt) - pos;
587 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
588 pos += (cnt > len - 1 ? len - 1 : cnt);
589 len = sizeof(prompt) - pos;
590 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
591 pos += (cnt > len - 1 ? len - 1 : cnt);
592 len = sizeof(prompt) - pos;
594 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
595 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : " ");
597 snprintf(&prompt[pos], len, "%s", list_head3);
604 if(!CHECK_BIT(line->bits, IS_CODE))
605 inline_display(window, &line->text->value[offset], colors);
607 // IS_UNORDERED_LIST_2
608 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
609 offset = next_nonblank(line->text, 0);
611 int pos = 0, len, cnt;
612 len = sizeof(prompt) - pos;
613 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
614 pos += (cnt > len - 1 ? len - 1 : cnt);
615 len = sizeof(prompt) - pos;
617 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
618 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
620 snprintf(&prompt[pos], len, "%s", list_head2);
627 if(!CHECK_BIT(line->bits, IS_CODE))
628 inline_display(window, &line->text->value[offset], colors);
630 // IS_UNORDERED_LIST_1
631 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
632 offset = next_nonblank(line->text, 0);
635 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
636 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
638 strcpy(&prompt[0], list_head1);
645 if(!CHECK_BIT(line->bits, IS_CODE))
646 inline_display(window, &line->text->value[offset], colors);
650 if(CHECK_BIT(line->bits, IS_CODE)) {
652 if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) {
653 // set static offset for code
654 offset = CODE_INDENT;
657 // reverse color for code blocks
659 wattron(window, COLOR_PAIR(CP_BLACK));
662 waddwstr(window, &line->text->value[offset]);
665 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
666 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
667 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
668 !CHECK_BIT(line->bits, IS_CODE)) {
671 if(CHECK_BIT(line->bits, IS_QUOTE)) {
672 while(line->text->value[offset] == '>') {
673 // print a reverse color block
675 wattron(window, COLOR_PAIR(CP_BLACK));
676 wprintw(window, "%s", " ");
677 wattron(window, COLOR_PAIR(CP_WHITE));
678 wprintw(window, "%s", " ");
680 wprintw(window, "%s", ">");
683 // find next quote or break
685 if(line->text->value[offset] == ' ')
686 offset = next_word(line->text, offset);
689 inline_display(window, &line->text->value[offset], colors);
693 if(CHECK_BIT(line->bits, IS_CENTER)) {
694 if(line->length < max_cols) {
695 wmove(window, y, x + ((max_cols - line->length) / 2));
700 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
702 // set headline color
704 wattron(window, COLOR_PAIR(CP_BLUE));
706 // enable underline for H1
707 if(CHECK_BIT(line->bits, IS_H1))
708 wattron(window, A_UNDERLINE);
711 while(line->text->value[offset] == '#')
712 offset = next_word(line->text, offset);
715 waddwstr(window, &line->text->value[offset]);
717 wattroff(window, A_UNDERLINE);
719 // no line-wide markdown
722 inline_display(window, &line->text->value[offset], colors);
727 // fill rest off line with spaces
728 // we only need this if the color is inverted (e.g. code-blocks),
729 // to ensure the background fades too
730 if(CHECK_BIT(line->bits, IS_CODE))
731 for(i = getcurx(window) - x; i < max_cols; i++)
732 wprintw(window, "%s", " ");
734 // reset to default color
736 wattron(window, COLOR_PAIR(CP_WHITE));
737 wattroff(window, A_UNDERLINE);
740 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
741 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
742 const wchar_t *i = c; // iterator
743 const wchar_t *start_link_name, *start_url;
744 int length_link_name, url_num;
745 cstack_t *stack = cstack_init();
748 // for each char in line
751 // if char is in special char list
752 if(wcschr(special, *i)) {
754 // closing special char (or second backslash)
755 // only if not followed by :alnum:
756 if((stack->top)(stack, *i) &&
757 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
760 // print escaped backslash
762 waddnwstr(window, i, 1);
767 wattron(window, COLOR_PAIR(CP_WHITE));
771 wattroff(window, A_UNDERLINE);
773 // disable inline code
776 wattron(window, COLOR_PAIR(CP_WHITE));
780 // remove top special char from stack
783 // treat special as regular char
784 } else if((stack->top)(stack, L'\\')) {
785 waddnwstr(window, i, 1);
787 // remove backslash from stack
790 // opening special char
793 // emphasis or code span can start after new-line or space only
794 // and of cause after another emphasis markup
795 //TODO this condition looks ugly
797 iswspace(*(i - 1)) ||
798 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
799 ((i - 1) == c || iswspace(*(i - 2)))) ||
802 // url in pandoc style
803 if ((*i == L'[' && wcschr(i, L']')) ||
804 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
808 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
811 // turn higlighting and underlining on
813 wattron(window, COLOR_PAIR(CP_BLUE));
814 wattron(window, A_UNDERLINE);
818 // print the content of the label
819 // the label is printed as is
821 waddnwstr(window, i, 1);
823 } while (*i != L']');
825 length_link_name = i - 1 - start_link_name;
832 while (*i != L')') i++;
834 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
836 wprintw(window, " [%d]", url_num);
838 // turn highlighting and underlining off
839 wattroff(window, A_UNDERLINE);
840 wattron(window, COLOR_PAIR(CP_WHITE));
843 wprintw(window, "[");
850 wattron(window, COLOR_PAIR(CP_RED));
854 wattron(window, A_UNDERLINE);
856 // enable inline code
859 wattron(window, COLOR_PAIR(CP_BLACK));
861 // do nothing for backslashes
864 // push special char to stack
865 (stack->push)(stack, *i);
868 waddnwstr(window, i, 1);
873 // remove backslash from stack
874 if((stack->top)(stack, L'\\'))
877 // print regular char
878 waddnwstr(window, i, 1);
882 // pop stack until empty to prevent formated trailing spaces
883 while(!(stack->empty)(stack)) {
884 switch((stack->pop)(stack)) {
888 wattron(window, COLOR_PAIR(CP_WHITE));
892 wattroff(window, A_UNDERLINE);
894 // disable inline code
897 wattron(window, COLOR_PAIR(CP_WHITE));
899 // do nothing for backslashes
903 (stack->delete)(stack);
906 void fade_out(WINDOW *window, int trans, int colors, int invert) {
908 if(colors && COLORS == 256) {
909 for(i = 22; i >= 0; i--) {
913 init_pair(CP_WHITE, white_ramp_invert[i], trans);
914 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
915 init_pair(CP_RED, red_ramp_invert[i], trans);
916 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
918 init_pair(CP_WHITE, white_ramp[i], trans);
919 init_pair(CP_BLUE, blue_ramp[i], trans);
920 init_pair(CP_RED, red_ramp[i], trans);
921 init_pair(CP_BLACK, 16, white_ramp[i]);
924 // refresh virtual screen with new color
925 wnoutrefresh(window);
927 // compare virtual screen to physical screen and does the actual updates
930 // delay for our eyes to recognize the change
936 void fade_in(WINDOW *window, int trans, int colors, int invert) {
938 if(colors && COLORS == 256) {
939 for(i = 0; i <= 23; i++) {
941 // brighten color pairs
943 init_pair(CP_WHITE, white_ramp_invert[i], trans);
944 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
945 init_pair(CP_RED, red_ramp_invert[i], trans);
946 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
948 init_pair(CP_WHITE, white_ramp[i], trans);
949 init_pair(CP_BLUE, blue_ramp[i], trans);
950 init_pair(CP_RED, red_ramp[i], trans);
951 init_pair(CP_BLACK, 16, white_ramp[i]);
954 // refresh virtual screen with new color
955 wnoutrefresh(window);
957 // compare virtual screen to physical screen and does the actual updates
960 // delay for our eyes to recognize the change
966 int int_length (int val) {
975 int get_slide_number(char init) {
976 int retval = init - '0';
978 // block for tenths of a second when using getch, ERR if no input
979 halfdelay(GOTO_SLIDE_DELAY);
980 while((c = getch()) != ERR) {
981 if (c < '0' || c > '9') {
985 retval = (retval * 10) + (c - '0');
987 nocbreak(); // cancel half delay mode
988 cbreak(); // go back to cbreak