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) 2015 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);
99 if (line->text->value)
100 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 of main window
235 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
238 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
240 wbkgd(content, COLOR_PAIR(CP_WHITE));
244 // find slide to reload
245 while(reload > 1 && reload <= deck->slides) {
251 // reset reload indicator
262 // always resize window in case terminal geometry has changed
263 wresize(content, LINES - bar_top - bar_bottom, COLS);
268 offset = next_blank(line->text, 0) + 1;
269 // add text to header
271 0, (COLS - line->length + offset) / 2,
272 &line->text->value[offset]);
276 if(deck->headers > 1) {
277 line = deck->header->next;
278 offset = next_blank(line->text, 0) + 1;
280 case 0: // add text to center footer
282 LINES - 1, (COLS - line->length + offset) / 2,
283 &line->text->value[offset]);
286 case 2: // add text to left footer
289 &line->text->value[offset]);
294 // add slide number to right footer
296 case 1: // show slide number only
298 LINES - 1, COLS - int_length(sc) - 3,
301 case 2: // show current slide & number of slides
303 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
304 "%d / %d", sc, deck->slides);
308 // make header + fooder visible
317 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
319 // raise stop counter if we pass a line having a stop bit
320 if(CHECK_BIT(line->bits, IS_STOP))
323 l += (line->length / COLS) + 1;
326 // only stop here if we didn't stop here recently
327 if(stop > slide->stop)
331 // print pandoc URL references
332 // only if we already printed all lines of the current slide
335 getmaxyx( content, ymax, i );
336 for (i = 0; i < url_get_amount(); i++) {
337 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
339 waddwstr(content, url_get_target(i));
343 // make content visible
348 fade_in(content, trans, colors, invert);
350 // re-enable fading after any undefined key press
351 if(COLORS == 256 && !nofade)
354 // wait for user input
357 // evaluate user input
361 // show previous slide or stop bit
365 case 8: // BACKSPACE (ascii)
366 case 127: // BACKSPACE (xterm)
367 case 263: // BACKSPACE (getty)
370 if(stop > 1 || (stop == 1 && !line)) {
371 // show current slide again
372 // but stop one stop bit earlier
376 // show previous slide
386 // show next slide or stop bit
395 // show current slide again
396 // but stop one stop bit later (or at end of slide)
420 i = get_slide_number(c);
421 if(i > 0 && i <= deck->slides) {
438 // disable fading if slide n doesn't exist
453 for(i = sc; i <= deck->slides; i++) {
468 // disable fading if reload is not possible
475 // do not fade out on exit
483 // disable fading on undefined key press
490 fade_out(content, trans, colors, invert);
498 // free ncurses memory
503 // return reload indicator (0 means no reload)
507 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
509 if(!line->text->value) {
514 int offset = 0; // text offset
516 // move the cursor in position
519 // IS_UNORDERED_LIST_3
520 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
521 offset = next_nonblank(line->text, 0);
523 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
524 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
526 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
527 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
529 strcpy(&prompt[8], " +- ");
536 if(!CHECK_BIT(line->bits, IS_CODE))
537 inline_display(window, &line->text->value[offset], colors);
539 // IS_UNORDERED_LIST_2
540 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
541 offset = next_nonblank(line->text, 0);
543 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
545 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
546 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
548 strcpy(&prompt[4], " +- ");
555 if(!CHECK_BIT(line->bits, IS_CODE))
556 inline_display(window, &line->text->value[offset], colors);
558 // IS_UNORDERED_LIST_1
559 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
560 offset = next_nonblank(line->text, 0);
563 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
564 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
566 strcpy(&prompt[0], " +- ");
573 if(!CHECK_BIT(line->bits, IS_CODE))
574 inline_display(window, &line->text->value[offset], colors);
578 if(CHECK_BIT(line->bits, IS_CODE)) {
580 if (!CHECK_BIT(line->bits, IS_TILDE_CODE)) {
581 // set static offset for code
582 offset = CODE_INDENT;
585 // reverse color for code blocks
587 wattron(window, COLOR_PAIR(CP_BLACK));
590 waddwstr(window, &line->text->value[offset]);
593 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
594 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
595 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
596 !CHECK_BIT(line->bits, IS_CODE)) {
599 if(CHECK_BIT(line->bits, IS_QUOTE)) {
600 while(line->text->value[offset] == '>') {
601 // print a reverse color block
603 wattron(window, COLOR_PAIR(CP_BLACK));
604 wprintw(window, "%s", " ");
605 wattron(window, COLOR_PAIR(CP_WHITE));
606 wprintw(window, "%s", " ");
608 wprintw(window, "%s", ">");
611 // find next quote or break
613 if(line->text->value[offset] == ' ')
614 offset = next_word(line->text, offset);
617 inline_display(window, &line->text->value[offset], colors);
621 if(CHECK_BIT(line->bits, IS_CENTER)) {
622 if(line->length < max_cols) {
623 wmove(window, y, x + ((max_cols - line->length) / 2));
628 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
630 // set headline color
632 wattron(window, COLOR_PAIR(CP_BLUE));
634 // enable underline for H1
635 if(CHECK_BIT(line->bits, IS_H1))
636 wattron(window, A_UNDERLINE);
639 while(line->text->value[offset] == '#')
640 offset = next_word(line->text, offset);
643 waddwstr(window, &line->text->value[offset]);
645 wattroff(window, A_UNDERLINE);
647 // no line-wide markdown
650 inline_display(window, &line->text->value[offset], colors);
655 // fill rest off line with spaces
656 // we only need this if the color is inverted (e.g. code-blocks),
657 // to ensure the background fades too
658 if(CHECK_BIT(line->bits, IS_CODE))
659 for(i = getcurx(window) - x; i < max_cols; i++)
660 wprintw(window, "%s", " ");
662 // reset to default color
664 wattron(window, COLOR_PAIR(CP_WHITE));
665 wattroff(window, A_UNDERLINE);
668 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
669 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
670 const wchar_t *i = c; // iterator
671 const wchar_t *start_link_name, *start_url;
672 int length_link_name, url_num;
673 cstack_t *stack = cstack_init();
676 // for each char in line
679 // if char is in special char list
680 if(wcschr(special, *i)) {
682 // closing special char (or second backslash)
683 // only if not followed by :alnum:
684 if((stack->top)(stack, *i) &&
685 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
688 // print escaped backslash
690 waddnwstr(window, i, 1);
695 wattron(window, COLOR_PAIR(CP_WHITE));
699 wattroff(window, A_UNDERLINE);
701 // disable inline code
704 wattron(window, COLOR_PAIR(CP_WHITE));
708 // remove top special char from stack
711 // treat special as regular char
712 } else if((stack->top)(stack, L'\\')) {
713 waddnwstr(window, i, 1);
715 // remove backslash from stack
718 // opening special char
721 // emphasis or code span can start after new-line or space only
722 // and of cause after another emphasis markup
723 //TODO this condition looks ugly
725 iswspace(*(i - 1)) ||
726 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
727 ((i - 1) == c || iswspace(*(i - 2)))) ||
730 // url in pandoc style
731 if ((*i == L'[' && wcschr(i, L']')) ||
732 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
736 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
739 // turn higlighting and underlining on
741 wattron(window, COLOR_PAIR(CP_BLUE));
742 wattron(window, A_UNDERLINE);
746 // print the content of the label
747 // the label is printed as is
749 waddnwstr(window, i, 1);
751 } while (*i != L']');
753 length_link_name = i - 1 - start_link_name;
760 while (*i != L')') i++;
762 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
764 wprintw(window, " [%d]", url_num);
766 // turn highlighting and underlining off
767 wattroff(window, A_UNDERLINE);
768 wattron(window, COLOR_PAIR(CP_WHITE));
771 wprintw(window, "[");
778 wattron(window, COLOR_PAIR(CP_RED));
782 wattron(window, A_UNDERLINE);
784 // enable inline code
787 wattron(window, COLOR_PAIR(CP_BLACK));
789 // do nothing for backslashes
792 // push special char to stack
793 (stack->push)(stack, *i);
796 waddnwstr(window, i, 1);
801 // remove backslash from stack
802 if((stack->top)(stack, L'\\'))
805 // print regular char
806 waddnwstr(window, i, 1);
810 // pop stack until empty to prevent formated trailing spaces
811 while(!(stack->empty)(stack)) {
812 switch((stack->pop)(stack)) {
816 wattron(window, COLOR_PAIR(CP_WHITE));
820 wattroff(window, A_UNDERLINE);
822 // disable inline code
825 wattron(window, COLOR_PAIR(CP_WHITE));
827 // do nothing for backslashes
831 (stack->delete)(stack);
834 void fade_out(WINDOW *window, int trans, int colors, int invert) {
836 if(colors && COLORS == 256) {
837 for(i = 22; i >= 0; i--) {
841 init_pair(CP_WHITE, white_ramp_invert[i], trans);
842 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
843 init_pair(CP_RED, red_ramp_invert[i], trans);
844 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
846 init_pair(CP_WHITE, white_ramp[i], trans);
847 init_pair(CP_BLUE, blue_ramp[i], trans);
848 init_pair(CP_RED, red_ramp[i], trans);
849 init_pair(CP_BLACK, 16, white_ramp[i]);
852 // refresh window with new color
855 // delay for our eyes to recognize the change
861 void fade_in(WINDOW *window, int trans, int colors, int invert) {
863 if(colors && COLORS == 256) {
864 for(i = 0; i <= 23; i++) {
866 // brighten color pairs
868 init_pair(CP_WHITE, white_ramp_invert[i], trans);
869 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
870 init_pair(CP_RED, red_ramp_invert[i], trans);
871 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
873 init_pair(CP_WHITE, white_ramp[i], trans);
874 init_pair(CP_BLUE, blue_ramp[i], trans);
875 init_pair(CP_RED, red_ramp[i], trans);
876 init_pair(CP_BLACK, 16, white_ramp[i]);
879 // refresh window with new color
882 // delay for our eyes to recognize the change
888 int int_length (int val) {
897 int get_slide_number(char init) {
898 int retval = init - '0';
900 // block for tenths of a second when using getch, ERR if no input
901 halfdelay(GOTO_SLIDE_DELAY);
902 while((c = getch()) != ERR) {
903 if (c < '0' || c > '9') {
907 retval = (retval * 10) + (c - '0');
909 nocbreak(); // cancel half delay mode
910 cbreak(); // go back to cbreak