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 // set static offset for code
661 offset = CODE_INDENT;
664 // reverse color for code blocks
666 wattron(window, COLOR_PAIR(CP_BLACK));
669 waddwstr(window, &line->text->value[offset]);
672 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
673 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
674 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
675 !CHECK_BIT(line->bits, IS_CODE)) {
678 if(CHECK_BIT(line->bits, IS_QUOTE)) {
679 while(line->text->value[offset] == '>') {
680 // print a reverse color block
682 wattron(window, COLOR_PAIR(CP_BLACK));
683 wprintw(window, "%s", " ");
684 wattron(window, COLOR_PAIR(CP_WHITE));
685 wprintw(window, "%s", " ");
687 wprintw(window, "%s", ">");
690 // find next quote or break
692 if(line->text->value[offset] == ' ')
693 offset = next_word(line->text, offset);
696 inline_display(window, &line->text->value[offset], colors);
700 if(CHECK_BIT(line->bits, IS_CENTER)) {
701 if(line->length < max_cols) {
702 wmove(window, y, x + ((max_cols - line->length) / 2));
707 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
709 // set headline color
711 wattron(window, COLOR_PAIR(CP_BLUE));
713 // enable underline for H1
714 if(CHECK_BIT(line->bits, IS_H1))
715 wattron(window, A_UNDERLINE);
718 while(line->text->value[offset] == '#')
719 offset = next_word(line->text, offset);
722 waddwstr(window, &line->text->value[offset]);
724 wattroff(window, A_UNDERLINE);
726 // no line-wide markdown
729 inline_display(window, &line->text->value[offset], colors);
734 // fill rest off line with spaces
735 // we only need this if the color is inverted (e.g. code-blocks),
736 // to ensure the background fades too
737 if(CHECK_BIT(line->bits, IS_CODE))
738 for(i = getcurx(window) - x; i < max_cols; i++)
739 wprintw(window, "%s", " ");
741 // reset to default color
743 wattron(window, COLOR_PAIR(CP_WHITE));
744 wattroff(window, A_UNDERLINE);
747 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
748 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
749 const wchar_t *i = c; // iterator
750 const wchar_t *start_link_name, *start_url;
751 int length_link_name, url_num;
752 cstack_t *stack = cstack_init();
755 // for each char in line
758 // if char is in special char list
759 if(wcschr(special, *i)) {
761 // closing special char (or second backslash)
762 // only if not followed by :alnum:
763 if((stack->top)(stack, *i) &&
764 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
767 // print escaped backslash
769 waddnwstr(window, i, 1);
774 wattron(window, COLOR_PAIR(CP_WHITE));
778 wattroff(window, A_UNDERLINE);
780 // disable inline code
783 wattron(window, COLOR_PAIR(CP_WHITE));
787 // remove top special char from stack
790 // treat special as regular char
791 } else if((stack->top)(stack, L'\\')) {
792 waddnwstr(window, i, 1);
794 // remove backslash from stack
797 // opening special char
800 // emphasis or code span can start after new-line or space only
801 // and of cause after another emphasis markup
802 //TODO this condition looks ugly
804 iswspace(*(i - 1)) ||
805 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
806 ((i - 1) == c || iswspace(*(i - 2)))) ||
809 // url in pandoc style
810 if ((*i == L'[' && wcschr(i, L']')) ||
811 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
815 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
818 // turn higlighting and underlining on
820 wattron(window, COLOR_PAIR(CP_BLUE));
821 wattron(window, A_UNDERLINE);
825 // print the content of the label
826 // the label is printed as is
828 waddnwstr(window, i, 1);
830 } while (*i != L']');
832 length_link_name = i - 1 - start_link_name;
839 while (*i != L')') i++;
841 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
843 wprintw(window, " [%d]", url_num);
845 // turn highlighting and underlining off
846 wattroff(window, A_UNDERLINE);
847 wattron(window, COLOR_PAIR(CP_WHITE));
850 wprintw(window, "[");
857 wattron(window, COLOR_PAIR(CP_RED));
861 wattron(window, A_UNDERLINE);
863 // enable inline code
866 wattron(window, COLOR_PAIR(CP_BLACK));
868 // do nothing for backslashes
871 // push special char to stack
872 (stack->push)(stack, *i);
875 waddnwstr(window, i, 1);
880 // remove backslash from stack
881 if((stack->top)(stack, L'\\'))
884 // print regular char
885 waddnwstr(window, i, 1);
889 // pop stack until empty to prevent formated trailing spaces
890 while(!(stack->empty)(stack)) {
891 switch((stack->pop)(stack)) {
895 wattron(window, COLOR_PAIR(CP_WHITE));
899 wattroff(window, A_UNDERLINE);
901 // disable inline code
904 wattron(window, COLOR_PAIR(CP_WHITE));
906 // do nothing for backslashes
910 (stack->delete)(stack);
913 void fade_out(WINDOW *window, int trans, int colors, int invert) {
915 if(colors && COLORS == 256) {
916 for(i = 22; i >= 0; i--) {
920 init_pair(CP_WHITE, white_ramp_invert[i], trans);
921 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
922 init_pair(CP_RED, red_ramp_invert[i], trans);
923 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
925 init_pair(CP_WHITE, white_ramp[i], trans);
926 init_pair(CP_BLUE, blue_ramp[i], trans);
927 init_pair(CP_RED, red_ramp[i], trans);
928 init_pair(CP_BLACK, 16, white_ramp[i]);
931 // refresh virtual screen with new color
932 wnoutrefresh(window);
934 // compare virtual screen to physical screen and does the actual updates
937 // delay for our eyes to recognize the change
943 void fade_in(WINDOW *window, int trans, int colors, int invert) {
945 if(colors && COLORS == 256) {
946 for(i = 0; i <= 23; i++) {
948 // brighten color pairs
950 init_pair(CP_WHITE, white_ramp_invert[i], trans);
951 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
952 init_pair(CP_RED, red_ramp_invert[i], trans);
953 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
955 init_pair(CP_WHITE, white_ramp[i], trans);
956 init_pair(CP_BLUE, blue_ramp[i], trans);
957 init_pair(CP_RED, red_ramp[i], trans);
958 init_pair(CP_BLACK, 16, white_ramp[i]);
961 // refresh virtual screen with new color
962 wnoutrefresh(window);
964 // compare virtual screen to physical screen and does the actual updates
967 // delay for our eyes to recognize the change
973 int int_length (int val) {
982 int get_slide_number(char init) {
983 int retval = init - '0';
985 // block for tenths of a second when using getch, ERR if no input
986 halfdelay(GOTO_SLIDE_DELAY);
987 while((c = getch()) != ERR) {
988 if (c < '0' || c > '9') {
992 retval = (retval * 10) + (c - '0');
994 nocbreak(); // cancel half delay mode
995 cbreak(); // go back to cbreak