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
31 // color ramp for fading from black to color
32 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
33 237, 238, 239, 240, 241, 242,
34 244, 245, 246, 247, 248, 249,
35 250, 251, 252, 253, 254, 255 };
37 static short blue_ramp[24] = { 16, 17, 17, 18, 18, 19,
38 19, 20, 20, 21, 27, 33,
39 32, 39, 38, 45, 44, 44,
40 81, 81, 51, 51, 123, 123 };
42 static short red_ramp[24] = { 16, 52, 52, 53, 53, 89,
43 89, 90, 90, 126, 127, 127,
44 163, 163, 164, 164, 200, 200,
45 201, 201, 207, 207, 213, 213 };
47 // color ramp for fading from white to color
48 static short white_ramp_invert[24] = { 15, 255, 254, 254, 252, 251,
49 250, 249, 248, 247, 246, 245,
50 243, 242, 241, 240, 239, 238,
51 237, 236, 235, 234, 233, 232};
53 static short blue_ramp_invert[24] = { 15, 231, 231, 195, 195, 159,
54 159, 123, 123, 87, 51, 44,
55 45, 38, 39, 32, 33, 33,
56 26, 26, 27, 27, 21, 21};
58 static short red_ramp_invert[24] = { 15, 231, 231, 224, 224, 225,
59 225, 218, 218, 219, 212, 213,
60 206, 207, 201, 200, 199, 199,
61 198, 198, 197, 197, 196, 196};
63 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload, int slidenum) {
67 int l = 0; // line number
68 int lc = 0; // line count
69 int sc = 1; // slide count
70 int colors = 0; // amount of colors supported
71 int fade = 0; // disable color fading by default
72 int trans = -1; // enable transparency if term supports it
73 int max_lines = 0; // max lines per slide
74 int max_cols = 0; // max columns per line
75 int offset; // text offset
76 int stop = 0; // passed stop bits per slide
78 // header line 1 is displayed at the top
79 int bar_top = (deck->headers > 0) ? 1 : 0;
80 // header line 2 is displayed at the bottom
81 // anyway we display the slide number at the bottom
82 int bar_bottom = (slidenum || deck->headers > 1)? 1 : 0;
84 slide_t *slide = deck->slide;
94 while(line && line->text) {
96 if (line->text->value) {
97 lc += url_count_inline(line->text->value);
98 line->length -= url_len_inline(line->text->value);
101 if(line->length > COLS) {
106 i = prev_blank(line->text, offset + COLS) - offset;
108 // single word is > COLS
110 // calculate min_width
111 i = next_blank(line->text, offset + COLS) - offset;
117 fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
118 fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n");
125 max_cols = MAX(i, max_cols);
127 // iterate to next line
128 offset = prev_blank(line->text, offset + COLS);
129 i = line->length - offset;
132 // set max_cols one last time
133 max_cols = MAX(i, max_cols);
136 max_cols = MAX(line->length, max_cols);
142 max_lines = MAX(lc, max_lines);
148 if(max_lines + bar_top + bar_bottom > LINES) {
154 fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
155 fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
164 // disable output of keyboard typing
167 // make getch() process one char at a time
174 if(has_colors() == TRUE) {
176 use_default_colors();
183 trans = 15; // white in 256 color mode
185 trans = 16; // black in 256 color mode
190 init_pair(CP_WHITE, 232, trans);
191 init_pair(CP_BLUE, 21, trans);
192 init_pair(CP_RED, 196, trans);
193 init_pair(CP_BLACK, 15, 232);
195 init_pair(CP_WHITE, 255, trans);
196 init_pair(CP_BLUE, 123, trans);
197 init_pair(CP_RED, 213, trans);
198 init_pair(CP_BLACK, 16, 255);
200 init_pair(CP_YELLOW, 208, trans);
202 // enable color fading
211 trans = 7; // white in 8 color mode
213 trans = 0; // black in 8 color mode
218 init_pair(CP_WHITE, 0, trans);
219 init_pair(CP_BLACK, 7, 0);
221 init_pair(CP_WHITE, 7, trans);
222 init_pair(CP_BLACK, 0, 7);
224 init_pair(CP_BLUE, 4, trans);
225 init_pair(CP_RED, 1, trans);
226 init_pair(CP_YELLOW, 3, trans);
232 // set background color of main window
234 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
237 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
239 wbkgd(content, COLOR_PAIR(CP_WHITE));
243 // find slide to reload
244 while(reload > 1 && reload <= deck->slides) {
250 // reset reload indicator
261 // always resize window in case terminal geometry has changed
262 wresize(content, LINES - bar_top - bar_bottom, COLS);
267 offset = next_blank(line->text, 0) + 1;
268 // add text to header
270 0, (COLS - line->length + offset) / 2,
271 &line->text->value[offset]);
275 if(deck->headers > 1) {
276 line = deck->header->next;
277 offset = next_blank(line->text, 0) + 1;
279 case 0: // add text to center footer
281 LINES - 1, (COLS - line->length + offset) / 2,
282 &line->text->value[offset]);
285 case 2: // add text to left footer
288 &line->text->value[offset]);
293 // add slide number to right footer
295 case 1: // show slide number only
297 LINES - 1, COLS - int_length(sc) - 3,
300 case 2: // show current slide & number of slides
302 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
303 "%d / %d", sc, deck->slides);
307 // make header + fooder visible
316 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
318 // raise stop counter if we pass a line having a stop bit
319 if(CHECK_BIT(line->bits, IS_STOP))
322 l += (line->length / COLS) + 1;
325 // only stop here if we didn't stop here recently
326 if(stop > slide->stop)
330 // print pandoc URL references
331 // only if we already printed all lines of the current slide
334 getmaxyx( content, ymax, i );
335 for (i = 0; i < url_get_amount(); i++) {
336 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
338 waddwstr(content, url_get_target(i));
342 // make content visible
347 fade_in(content, trans, colors, invert);
349 // re-enable fading after any undefined key press
350 if(COLORS == 256 && !nofade)
353 // wait for user input
356 // evaluate user input
360 // show previous slide or stop bit
364 case 8: // BACKSPACE (ascii)
365 case 127: // BACKSPACE (xterm)
366 case 263: // BACKSPACE (getty)
369 if(stop > 1 || (stop == 1 && !line)) {
370 // show current slide again
371 // but stop one stop bit earlier
376 // show previous slide
379 //stop on first bullet point always
389 // show next slide or stop bit
398 // show current slide again
399 // but stop one stop bit later (or at end of slide)
424 i = get_slide_number(c);
425 if(i > 0 && i <= deck->slides) {
442 // disable fading if slide n doesn't exist
457 for(i = sc; i <= deck->slides; i++) {
472 // disable fading if reload is not possible
479 // do not fade out on exit
487 // disable fading on undefined key press
494 fade_out(content, trans, colors, invert);
502 // free ncurses memory
507 // return reload indicator (0 means no reload)
511 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
514 int offset = 0; // text offset
516 // move the cursor in position
519 if(!line->text->value) {
521 // fill rest off line with spaces if we are in a code block
522 if(CHECK_BIT(line->bits, IS_CODE) && colors) {
523 wattron(window, COLOR_PAIR(CP_BLACK));
524 for(i = getcurx(window) - x; i < max_cols; i++)
525 wprintw(window, "%s", " ");
532 // IS_UNORDERED_LIST_3
533 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
534 offset = next_nonblank(line->text, 0);
536 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
537 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
539 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
540 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
542 strcpy(&prompt[8], " +- ");
549 if(!CHECK_BIT(line->bits, IS_CODE))
550 inline_display(window, &line->text->value[offset], colors);
552 // IS_UNORDERED_LIST_2
553 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
554 offset = next_nonblank(line->text, 0);
556 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
558 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
559 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
561 strcpy(&prompt[4], " +- ");
568 if(!CHECK_BIT(line->bits, IS_CODE))
569 inline_display(window, &line->text->value[offset], colors);
571 // IS_UNORDERED_LIST_1
572 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
573 offset = next_nonblank(line->text, 0);
576 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
577 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
579 strcpy(&prompt[0], " +- ");
586 if(!CHECK_BIT(line->bits, IS_CODE))
587 inline_display(window, &line->text->value[offset], colors);
591 if(CHECK_BIT(line->bits, IS_CODE)) {
593 if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) {
594 // set static offset for code
595 offset = CODE_INDENT;
598 // reverse color for code blocks
600 wattron(window, COLOR_PAIR(CP_BLACK));
603 waddwstr(window, &line->text->value[offset]);
606 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
607 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
608 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
609 !CHECK_BIT(line->bits, IS_CODE)) {
612 if(CHECK_BIT(line->bits, IS_QUOTE)) {
613 while(line->text->value[offset] == '>') {
614 // print a reverse color block
616 wattron(window, COLOR_PAIR(CP_BLACK));
617 wprintw(window, "%s", " ");
618 wattron(window, COLOR_PAIR(CP_WHITE));
619 wprintw(window, "%s", " ");
621 wprintw(window, "%s", ">");
624 // find next quote or break
626 if(line->text->value[offset] == ' ')
627 offset = next_word(line->text, offset);
630 inline_display(window, &line->text->value[offset], colors);
634 if(CHECK_BIT(line->bits, IS_CENTER)) {
635 if(line->length < max_cols) {
636 wmove(window, y, x + ((max_cols - line->length) / 2));
641 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
643 // set headline color
645 wattron(window, COLOR_PAIR(CP_BLUE));
647 // enable underline for H1
648 if(CHECK_BIT(line->bits, IS_H1))
649 wattron(window, A_UNDERLINE);
652 while(line->text->value[offset] == '#')
653 offset = next_word(line->text, offset);
656 waddwstr(window, &line->text->value[offset]);
658 wattroff(window, A_UNDERLINE);
660 // no line-wide markdown
663 inline_display(window, &line->text->value[offset], colors);
668 // fill rest off line with spaces
669 // we only need this if the color is inverted (e.g. code-blocks),
670 // to ensure the background fades too
671 if(CHECK_BIT(line->bits, IS_CODE))
672 for(i = getcurx(window) - x; i < max_cols; i++)
673 wprintw(window, "%s", " ");
675 // reset to default color
677 wattron(window, COLOR_PAIR(CP_WHITE));
678 wattroff(window, A_UNDERLINE);
681 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
682 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
683 const wchar_t *i = c; // iterator
684 const wchar_t *start_link_name, *start_url;
685 int length_link_name, url_num;
686 cstack_t *stack = cstack_init();
689 // for each char in line
692 // if char is in special char list
693 if(wcschr(special, *i)) {
695 // closing special char (or second backslash)
696 // only if not followed by :alnum:
697 if((stack->top)(stack, *i) &&
698 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
701 // print escaped backslash
703 waddnwstr(window, i, 1);
708 wattron(window, COLOR_PAIR(CP_WHITE));
712 wattroff(window, A_UNDERLINE);
714 // disable inline code
717 wattron(window, COLOR_PAIR(CP_WHITE));
721 // remove top special char from stack
724 // treat special as regular char
725 } else if((stack->top)(stack, L'\\')) {
726 waddnwstr(window, i, 1);
728 // remove backslash from stack
731 // opening special char
734 // emphasis or code span can start after new-line or space only
735 // and of cause after another emphasis markup
736 //TODO this condition looks ugly
738 iswspace(*(i - 1)) ||
739 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
740 ((i - 1) == c || iswspace(*(i - 2)))) ||
743 // url in pandoc style
744 if ((*i == L'[' && wcschr(i, L']')) ||
745 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
749 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
752 // turn higlighting and underlining on
754 wattron(window, COLOR_PAIR(CP_BLUE));
755 wattron(window, A_UNDERLINE);
759 // print the content of the label
760 // the label is printed as is
762 waddnwstr(window, i, 1);
764 } while (*i != L']');
766 length_link_name = i - 1 - start_link_name;
773 while (*i != L')') i++;
775 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
777 wprintw(window, " [%d]", url_num);
779 // turn highlighting and underlining off
780 wattroff(window, A_UNDERLINE);
781 wattron(window, COLOR_PAIR(CP_WHITE));
784 wprintw(window, "[");
791 wattron(window, COLOR_PAIR(CP_RED));
795 wattron(window, A_UNDERLINE);
797 // enable inline code
800 wattron(window, COLOR_PAIR(CP_BLACK));
802 // do nothing for backslashes
805 // push special char to stack
806 (stack->push)(stack, *i);
809 waddnwstr(window, i, 1);
814 // remove backslash from stack
815 if((stack->top)(stack, L'\\'))
818 // print regular char
819 waddnwstr(window, i, 1);
823 // pop stack until empty to prevent formated trailing spaces
824 while(!(stack->empty)(stack)) {
825 switch((stack->pop)(stack)) {
829 wattron(window, COLOR_PAIR(CP_WHITE));
833 wattroff(window, A_UNDERLINE);
835 // disable inline code
838 wattron(window, COLOR_PAIR(CP_WHITE));
840 // do nothing for backslashes
844 (stack->delete)(stack);
847 void fade_out(WINDOW *window, int trans, int colors, int invert) {
849 if(colors && COLORS == 256) {
850 for(i = 22; i >= 0; i--) {
854 init_pair(CP_WHITE, white_ramp_invert[i], trans);
855 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
856 init_pair(CP_RED, red_ramp_invert[i], trans);
857 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
859 init_pair(CP_WHITE, white_ramp[i], trans);
860 init_pair(CP_BLUE, blue_ramp[i], trans);
861 init_pair(CP_RED, red_ramp[i], trans);
862 init_pair(CP_BLACK, 16, white_ramp[i]);
865 // refresh window with new color
868 // delay for our eyes to recognize the change
874 void fade_in(WINDOW *window, int trans, int colors, int invert) {
876 if(colors && COLORS == 256) {
877 for(i = 0; i <= 23; i++) {
879 // brighten color pairs
881 init_pair(CP_WHITE, white_ramp_invert[i], trans);
882 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
883 init_pair(CP_RED, red_ramp_invert[i], trans);
884 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
886 init_pair(CP_WHITE, white_ramp[i], trans);
887 init_pair(CP_BLUE, blue_ramp[i], trans);
888 init_pair(CP_RED, red_ramp[i], trans);
889 init_pair(CP_BLACK, 16, white_ramp[i]);
892 // refresh window with new color
895 // delay for our eyes to recognize the change
901 int int_length (int val) {
910 int get_slide_number(char init) {
911 int retval = init - '0';
913 // block for tenths of a second when using getch, ERR if no input
914 halfdelay(GOTO_SLIDE_DELAY);
915 while((c = getch()) != ERR) {
916 if (c < '0' || c > '9') {
920 retval = (retval * 10) + (c - '0');
922 nocbreak(); // cancel half delay mode
923 cbreak(); // go back to cbreak