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 = " +- ";
527 export MDP_LIST_OPEN1=" "
528 export MDP_LIST_OPEN2=" "
529 export MDP_LIST_OPEN3=" "
530 export MDP_LIST_HEAD1=" ■ "
531 export MDP_LIST_HEAD2=" ▫ "
532 export MDP_LIST_HEAD3=" ● "
534 export MDP_LIST_OPEN1=" │ "
535 export MDP_LIST_OPEN2=" │ "
536 export MDP_LIST_OPEN3=" │ "
537 export MDP_LIST_HEAD1=" ▇─ "
538 export MDP_LIST_HEAD2=" ▓─ "
539 export MDP_LIST_HEAD3=" ▒─ "
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 // set static offset for code
663 offset = CODE_INDENT;
666 // reverse color for code blocks
668 wattron(window, COLOR_PAIR(CP_BLACK));
671 waddwstr(window, &line->text->value[offset]);
674 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
675 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
676 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
677 !CHECK_BIT(line->bits, IS_CODE)) {
680 if(CHECK_BIT(line->bits, IS_QUOTE)) {
681 while(line->text->value[offset] == '>') {
682 // print a reverse color block
684 wattron(window, COLOR_PAIR(CP_BLACK));
685 wprintw(window, "%s", " ");
686 wattron(window, COLOR_PAIR(CP_WHITE));
687 wprintw(window, "%s", " ");
689 wprintw(window, "%s", ">");
692 // find next quote or break
694 if(line->text->value[offset] == ' ')
695 offset = next_word(line->text, offset);
698 inline_display(window, &line->text->value[offset], colors);
702 if(CHECK_BIT(line->bits, IS_CENTER)) {
703 if(line->length < max_cols) {
704 wmove(window, y, x + ((max_cols - line->length) / 2));
709 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
711 // set headline color
713 wattron(window, COLOR_PAIR(CP_BLUE));
715 // enable underline for H1
716 if(CHECK_BIT(line->bits, IS_H1))
717 wattron(window, A_UNDERLINE);
720 while(line->text->value[offset] == '#')
721 offset = next_word(line->text, offset);
724 waddwstr(window, &line->text->value[offset]);
726 wattroff(window, A_UNDERLINE);
728 // no line-wide markdown
731 inline_display(window, &line->text->value[offset], colors);
736 // fill rest off line with spaces
737 // we only need this if the color is inverted (e.g. code-blocks),
738 // to ensure the background fades too
739 if(CHECK_BIT(line->bits, IS_CODE))
740 for(i = getcurx(window) - x; i < max_cols; i++)
741 wprintw(window, "%s", " ");
743 // reset to default color
745 wattron(window, COLOR_PAIR(CP_WHITE));
746 wattroff(window, A_UNDERLINE);
749 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
750 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
751 const wchar_t *i = c; // iterator
752 const wchar_t *start_link_name, *start_url;
753 int length_link_name, url_num;
754 cstack_t *stack = cstack_init();
757 // for each char in line
760 // if char is in special char list
761 if(wcschr(special, *i)) {
763 // closing special char (or second backslash)
764 // only if not followed by :alnum:
765 if((stack->top)(stack, *i) &&
766 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
769 // print escaped backslash
771 waddnwstr(window, i, 1);
776 wattron(window, COLOR_PAIR(CP_WHITE));
780 wattroff(window, A_UNDERLINE);
782 // disable inline code
785 wattron(window, COLOR_PAIR(CP_WHITE));
789 // remove top special char from stack
792 // treat special as regular char
793 } else if((stack->top)(stack, L'\\')) {
794 waddnwstr(window, i, 1);
796 // remove backslash from stack
799 // opening special char
802 // emphasis or code span can start after new-line or space only
803 // and of cause after another emphasis markup
804 //TODO this condition looks ugly
806 iswspace(*(i - 1)) ||
807 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
808 ((i - 1) == c || iswspace(*(i - 2)))) ||
811 // url in pandoc style
812 if ((*i == L'[' && wcschr(i, L']')) ||
813 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
817 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
820 // turn higlighting and underlining on
822 wattron(window, COLOR_PAIR(CP_BLUE));
823 wattron(window, A_UNDERLINE);
827 // print the content of the label
828 // the label is printed as is
830 waddnwstr(window, i, 1);
832 } while (*i != L']');
834 length_link_name = i - 1 - start_link_name;
841 while (*i != L')') i++;
843 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
845 wprintw(window, " [%d]", url_num);
847 // turn highlighting and underlining off
848 wattroff(window, A_UNDERLINE);
849 wattron(window, COLOR_PAIR(CP_WHITE));
852 wprintw(window, "[");
859 wattron(window, COLOR_PAIR(CP_RED));
863 wattron(window, A_UNDERLINE);
865 // enable inline code
868 wattron(window, COLOR_PAIR(CP_BLACK));
870 // do nothing for backslashes
873 // push special char to stack
874 (stack->push)(stack, *i);
877 waddnwstr(window, i, 1);
882 // remove backslash from stack
883 if((stack->top)(stack, L'\\'))
886 // print regular char
887 waddnwstr(window, i, 1);
891 // pop stack until empty to prevent formated trailing spaces
892 while(!(stack->empty)(stack)) {
893 switch((stack->pop)(stack)) {
897 wattron(window, COLOR_PAIR(CP_WHITE));
901 wattroff(window, A_UNDERLINE);
903 // disable inline code
906 wattron(window, COLOR_PAIR(CP_WHITE));
908 // do nothing for backslashes
912 (stack->delete)(stack);
915 void fade_out(WINDOW *window, int trans, int colors, int invert) {
917 if(colors && COLORS == 256) {
918 for(i = 22; i >= 0; i--) {
922 init_pair(CP_WHITE, white_ramp_invert[i], trans);
923 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
924 init_pair(CP_RED, red_ramp_invert[i], trans);
925 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
927 init_pair(CP_WHITE, white_ramp[i], trans);
928 init_pair(CP_BLUE, blue_ramp[i], trans);
929 init_pair(CP_RED, red_ramp[i], trans);
930 init_pair(CP_BLACK, 16, white_ramp[i]);
933 // refresh virtual screen with new color
934 wnoutrefresh(window);
936 // compare virtual screen to physical screen and does the actual updates
939 // delay for our eyes to recognize the change
945 void fade_in(WINDOW *window, int trans, int colors, int invert) {
947 if(colors && COLORS == 256) {
948 for(i = 0; i <= 23; i++) {
950 // brighten color pairs
952 init_pair(CP_WHITE, white_ramp_invert[i], trans);
953 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
954 init_pair(CP_RED, red_ramp_invert[i], trans);
955 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
957 init_pair(CP_WHITE, white_ramp[i], trans);
958 init_pair(CP_BLUE, blue_ramp[i], trans);
959 init_pair(CP_RED, red_ramp[i], trans);
960 init_pair(CP_BLACK, 16, white_ramp[i]);
963 // refresh virtual screen with new color
964 wnoutrefresh(window);
966 // compare virtual screen to physical screen and does the actual updates
969 // delay for our eyes to recognize the change
975 int int_length (int val) {
984 int get_slide_number(char init) {
985 int retval = init - '0';
987 // block for tenths of a second when using getch, ERR if no input
988 halfdelay(GOTO_SLIDE_DELAY);
989 while((c = getch()) != ERR) {
990 if (c < '0' || c > '9') {
994 retval = (retval * 10) + (c - '0');
996 nocbreak(); // cancel half delay mode
997 cbreak(); // go back to cbreak