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 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum) {
68 int l = 0; // line number
69 int lc = 0; // line count
70 int sc = 1; // slide count
71 int colors = 0; // amount of colors supported
72 int fade = 0; // disable color fading by default
73 int trans = -1; // enable transparency if term supports it
74 int max_lines = 0; // max lines per slide
75 int max_cols = 0; // max columns per line
76 int offset; // text offset
77 int stop = 0; // passed stop bits per slide
79 // header line 1 is displayed at the top
80 int bar_top = (deck->headers > 0) ? 1 : 0;
81 // header line 2 is displayed at the bottom
82 // anyway we display the slide number at the bottom
83 int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0;
85 slide_t *slide = deck->slide;
95 while(line && line->text) {
97 if (line->text->value) {
98 lc += url_count_inline(line->text->value);
99 line->length -= url_len_inline(line->text->value);
102 if(line->length > COLS) {
107 i = prev_blank(line->text, offset + COLS) - offset;
109 // single word is > COLS
111 // calculate min_width
112 i = next_blank(line->text, offset + COLS) - offset;
118 fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
119 fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n");
126 max_cols = MAX(i, max_cols);
128 // iterate to next line
129 offset = prev_blank(line->text, offset + COLS);
130 i = line->length - offset;
133 // set max_cols one last time
134 max_cols = MAX(i, max_cols);
137 max_cols = MAX(line->length, max_cols);
143 max_lines = MAX(lc, max_lines);
149 if(max_lines + bar_top + bar_bottom > LINES) {
155 fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
156 fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
165 // disable output of keyboard typing
168 // make getch() process one char at a time
175 if(has_colors() == TRUE) {
177 use_default_colors();
184 trans = 15; // white in 256 color mode
186 trans = 16; // black in 256 color mode
191 init_pair(CP_WHITE, 232, trans);
192 init_pair(CP_BLUE, 21, trans);
193 init_pair(CP_RED, 196, trans);
194 init_pair(CP_BLACK, 15, 232);
196 init_pair(CP_WHITE, 255, trans);
197 init_pair(CP_BLUE, 123, trans);
198 init_pair(CP_RED, 213, trans);
199 init_pair(CP_BLACK, 16, 255);
201 init_pair(CP_YELLOW, 208, trans);
203 // enable color fading
212 trans = 7; // white in 8 color mode
214 trans = 0; // black in 8 color mode
219 init_pair(CP_WHITE, 0, trans);
220 init_pair(CP_BLACK, 7, 0);
222 init_pair(CP_WHITE, 7, trans);
223 init_pair(CP_BLACK, 0, 7);
225 init_pair(CP_BLUE, 4, trans);
226 init_pair(CP_RED, 1, trans);
227 init_pair(CP_YELLOW, 3, trans);
233 // set background color for main window
235 wbkgd(stdscr, COLOR_PAIR(CP_WHITE));
237 // setup content window
238 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
240 // set background color of content window
242 wbkgd(content, COLOR_PAIR(CP_WHITE));
246 // find slide to reload
247 while(reload > 1 && reload <= deck->slides) {
253 // reset reload indicator
264 // always resize window in case terminal geometry has changed
265 wresize(content, LINES - bar_top - bar_bottom, COLS);
267 // set main window text color
269 wattron(stdscr, COLOR_PAIR(CP_YELLOW));
274 offset = next_blank(line->text, 0) + 1;
275 // add text to header
277 0, (COLS - line->length + offset) / 2,
278 &line->text->value[offset]);
282 if(deck->headers > 1) {
283 line = deck->header->next;
284 offset = next_blank(line->text, 0) + 1;
286 case 0: // add text to center footer
288 LINES - 1, (COLS - line->length + offset) / 2,
289 &line->text->value[offset]);
292 case 2: // add text to left footer
295 &line->text->value[offset]);
300 // add slide number to right footer
302 case 1: // show slide number only
304 LINES - 1, COLS - int_length(sc) - 3,
307 case 2: // show current slide & number of slides
309 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
310 "%d / %d", sc, deck->slides);
314 // copy changed lines in main window to virtual screen
315 wnoutrefresh(stdscr);
322 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
324 // raise stop counter if we pass a line having a stop bit
325 if(CHECK_BIT(line->bits, IS_STOP))
328 l += (line->length / COLS) + 1;
331 // only stop here if we didn't stop here recently
332 if(stop > slide->stop)
336 // print pandoc URL references
337 // only if we already printed all lines of the current slide
340 getmaxyx( content, ymax, i );
341 for (i = 0; i < url_get_amount(); i++) {
342 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
344 waddwstr(content, url_get_target(i));
348 // copy changed lines in content window to virtual screen
349 wnoutrefresh(content);
351 // compare virtual screen to physical screen and does the actual updates
356 fade_in(content, trans, colors, invert);
358 // re-enable fading after any undefined key press
359 if(COLORS == 256 && !nofade)
362 // wait for user input
365 // evaluate user input
369 // show previous slide or stop bit
373 case 8: // BACKSPACE (ascii)
374 case 127: // BACKSPACE (xterm)
375 case 263: // BACKSPACE (getty)
378 if(stop > 1 || (stop == 1 && !line)) {
379 // show current slide again
380 // but stop one stop bit earlier
385 // show previous slide
388 //stop on first bullet point always
398 // show next slide or stop bit
407 // show current slide again
408 // but stop one stop bit later (or at end of slide)
433 i = get_slide_number(c);
434 if(i > 0 && i <= deck->slides) {
451 // disable fading if slide n doesn't exist
466 for(i = sc; i <= deck->slides; i++) {
481 // disable fading if reload is not possible
488 // do not fade out on exit
496 // disable fading on undefined key press
503 fade_out(content, trans, colors, invert);
511 // free ncurses memory
516 // return reload indicator (0 means no reload)
520 static const char *list_open1 = " | ";
521 static const char *list_open2 = " | ";
522 static const char *list_open3 = " | ";
523 static const char *list_head1 = " +- ";
524 static const char *list_head2 = " +- ";
525 static const char *list_head3 = " +- ";
528 export MDP_LIST_OPEN3=" "
529 export MDP_LIST_OPEN2=" "
530 export MDP_LIST_OPEN3=" "
531 export MDP_LIST_HEAD1=" ■ "
532 export MDP_LIST_HEAD2=" ▫ "
533 export MDP_LIST_HEAD3=" ● "
535 export MDP_LIST_OPEN2=" │ "
536 export MDP_LIST_OPEN2=" │ "
537 export MDP_LIST_OPEN3=" │ "
538 export MDP_LIST_HEAD1=" ▇─ "
539 export MDP_LIST_HEAD2=" ▓─ "
540 export MDP_LIST_HEAD3=" ▒─ "
543 void setup_list_strings(void)
547 if ((str = getenv("MDP_LIST_OPEN")) != NULL) {
548 list_open1 = list_open2 = list_open3 = str;
550 if ((str = getenv("MDP_LIST_OPEN1")) != NULL)
552 if ((str = getenv("MDP_LIST_OPEN2")) != NULL)
554 if ((str = getenv("MDP_LIST_OPEN3")) != NULL)
557 if ((str = getenv("MDP_LIST_HEAD")) != NULL) {
558 list_head1 = list_head2 = list_head3 = str;
560 if ((str = getenv("MDP_LIST_HEAD1")) != NULL)
562 if ((str = getenv("MDP_LIST_HEAD2")) != NULL)
564 if ((str = getenv("MDP_LIST_HEAD3")) != NULL)
569 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
572 int offset = 0; // text offset
574 // move the cursor in position
577 if(!line->text->value) {
579 // fill rest off line with spaces if we are in a code block
580 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
582 wattron(window, COLOR_PAIR(CP_BLACK));
583 for(i = getcurx(window) - x; i < max_cols; i++)
584 wprintw(window, "%s", " ");
591 // IS_UNORDERED_LIST_3
592 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
593 offset = next_nonblank(line->text, 0);
595 int pos = 0, len, cnt;
596 len = sizeof(prompt) - pos;
597 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
598 pos += (cnt > len - 1 ? len - 1 : cnt);
599 len = sizeof(prompt) - pos;
600 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
601 pos += (cnt > len - 1 ? len - 1 : cnt);
602 len = sizeof(prompt) - pos;
604 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
605 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? list_open3 : " ");
607 snprintf(&prompt[pos], len, "%s", list_head3);
614 if(!CHECK_BIT(line->bits, IS_CODE))
615 inline_display(window, &line->text->value[offset], colors);
617 // IS_UNORDERED_LIST_2
618 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
619 offset = next_nonblank(line->text, 0);
621 int pos = 0, len, cnt;
622 len = sizeof(prompt) - pos;
623 cnt = snprintf(&prompt[pos], len, "%s", CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
624 pos += (cnt > len - 1 ? len - 1 : cnt);
625 len = sizeof(prompt) - pos;
627 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
628 snprintf(&prompt[pos], len, "%s", line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? list_open2 : " ");
630 snprintf(&prompt[pos], len, "%s", list_head2);
637 if(!CHECK_BIT(line->bits, IS_CODE))
638 inline_display(window, &line->text->value[offset], colors);
640 // IS_UNORDERED_LIST_1
641 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
642 offset = next_nonblank(line->text, 0);
645 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
646 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? list_open1 : " ");
648 strcpy(&prompt[0], list_head1);
655 if(!CHECK_BIT(line->bits, IS_CODE))
656 inline_display(window, &line->text->value[offset], colors);
660 if(CHECK_BIT(line->bits, IS_CODE)) {
662 if (!CHECK_BIT(line->bits, IS_TILDE_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