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) {
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
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;
279 // add text to left footer
282 &line->text->value[offset]);
285 // add slide number to right footer
287 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
288 "%d / %d", sc, deck->slides);
290 // make header + fooder visible
299 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
301 // raise stop counter if we pass a line having a stop bit
302 if(CHECK_BIT(line->bits, IS_STOP))
305 l += (line->length / COLS) + 1;
308 // only stop here if we didn't stop here recently
309 if(stop > slide->stop)
313 // print pandoc URL references
314 // only if we already printed all lines of the current slide
317 getmaxyx( content, ymax, i );
318 for (i = 0; i < url_get_amount(); i++) {
319 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
321 waddwstr(content, url_get_target(i));
325 // make content visible
330 fade_in(content, trans, colors, invert);
332 // re-enable fading after any undefined key press
333 if(COLORS == 256 && !nofade)
336 // wait for user input
339 // evaluate user input
343 // show previous slide or stop bit
347 case 8: // BACKSPACE (ascii)
348 case 127: // BACKSPACE (xterm)
349 case 263: // BACKSPACE (getty)
352 if(stop > 1 || (stop == 1 && !line)) {
353 // show current slide again
354 // but stop one stop bit earlier
358 // show previous slide
368 // show next slide or stop bit
377 // show current slide again
378 // but stop one stop bit later (or at end of slide)
402 i = get_slide_number(c);
403 if(i > 0 && i <= deck->slides) {
420 // disable fading if slide n doesn't exist
435 for(i = sc; i <= deck->slides; i++) {
450 // disable fading if reload is not possible
457 // do not fade out on exit
465 // disable fading on undefined key press
472 fade_out(content, trans, colors, invert);
480 // free ncurses memory
485 // return reload indicator (0 means no reload)
489 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
491 if(!line->text->value) {
496 int offset = 0; // text offset
498 // move the cursor in position
501 // IS_UNORDERED_LIST_3
502 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
503 offset = next_nonblank(line->text, 0);
505 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
506 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
508 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
509 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
511 strcpy(&prompt[8], " +- ");
518 if(!CHECK_BIT(line->bits, IS_CODE))
519 inline_display(window, &line->text->value[offset], colors);
521 // IS_UNORDERED_LIST_2
522 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
523 offset = next_nonblank(line->text, 0);
525 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
527 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
528 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
530 strcpy(&prompt[4], " +- ");
537 if(!CHECK_BIT(line->bits, IS_CODE))
538 inline_display(window, &line->text->value[offset], colors);
540 // IS_UNORDERED_LIST_1
541 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
542 offset = next_nonblank(line->text, 0);
545 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
546 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
548 strcpy(&prompt[0], " +- ");
555 if(!CHECK_BIT(line->bits, IS_CODE))
556 inline_display(window, &line->text->value[offset], colors);
560 if(CHECK_BIT(line->bits, IS_CODE)) {
562 // set static offset for code
563 offset = CODE_INDENT;
565 // reverse color for code blocks
567 wattron(window, COLOR_PAIR(CP_BLACK));
570 waddwstr(window, &line->text->value[offset]);
573 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
574 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
575 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
576 !CHECK_BIT(line->bits, IS_CODE)) {
579 if(CHECK_BIT(line->bits, IS_QUOTE)) {
580 while(line->text->value[offset] == '>') {
581 // print a reverse color block
583 wattron(window, COLOR_PAIR(CP_BLACK));
584 wprintw(window, "%s", " ");
585 wattron(window, COLOR_PAIR(CP_WHITE));
586 wprintw(window, "%s", " ");
588 wprintw(window, "%s", ">");
591 // find next quote or break
593 if(line->text->value[offset] == ' ')
594 offset = next_word(line->text, offset);
597 inline_display(window, &line->text->value[offset], colors);
601 if(CHECK_BIT(line->bits, IS_CENTER)) {
602 if(line->length < max_cols) {
603 wmove(window, y, x + ((max_cols - line->length) / 2));
608 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
610 // set headline color
612 wattron(window, COLOR_PAIR(CP_BLUE));
614 // enable underline for H1
615 if(CHECK_BIT(line->bits, IS_H1))
616 wattron(window, A_UNDERLINE);
619 while(line->text->value[offset] == '#')
620 offset = next_word(line->text, offset);
623 waddwstr(window, &line->text->value[offset]);
625 wattroff(window, A_UNDERLINE);
627 // no line-wide markdown
630 inline_display(window, &line->text->value[offset], colors);
635 // fill rest off line with spaces
636 // we only need this if the color is inverted (e.g. code-blocks),
637 // to ensure the background fades too
638 if(CHECK_BIT(line->bits, IS_CODE))
639 for(i = getcurx(window) - x; i < max_cols; i++)
640 wprintw(window, "%s", " ");
642 // reset to default color
644 wattron(window, COLOR_PAIR(CP_WHITE));
645 wattroff(window, A_UNDERLINE);
648 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
649 const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
650 const wchar_t *i = c; // iterator
651 const wchar_t *start_link_name, *start_url;
652 int length_link_name, url_num;
653 cstack_t *stack = cstack_init();
656 // for each char in line
659 // if char is in special char list
660 if(wcschr(special, *i)) {
662 // closing special char (or second backslash)
663 // only if not followed by :alnum:
664 if((stack->top)(stack, *i) &&
665 (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
668 // print escaped backslash
670 waddnwstr(window, i, 1);
675 wattron(window, COLOR_PAIR(CP_WHITE));
679 wattroff(window, A_UNDERLINE);
681 // disable inline code
684 wattron(window, COLOR_PAIR(CP_WHITE));
688 // remove top special char from stack
691 // treat special as regular char
692 } else if((stack->top)(stack, L'\\')) {
693 waddnwstr(window, i, 1);
695 // remove backslash from stack
698 // opening special char
701 // emphasis or code span can start after new-line or space only
702 // and of cause after another emphasis markup
703 //TODO this condition looks ugly
705 iswspace(*(i - 1)) ||
706 ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
707 ((i - 1) == c || iswspace(*(i - 2)))) ||
710 // url in pandoc style
711 if ((*i == L'[' && wcschr(i, L']')) ||
712 (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
716 if (wcschr(i, L']')[1] == L'(' && wcschr(i, L')')) {
719 // turn higlighting and underlining on
721 wattron(window, COLOR_PAIR(CP_BLUE));
722 wattron(window, A_UNDERLINE);
726 // print the content of the label
727 // the label is printed as is
729 waddnwstr(window, i, 1);
731 } while (*i != L']');
733 length_link_name = i - 1 - start_link_name;
740 while (*i != L')') i++;
742 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
744 wprintw(window, " [%d]", url_num);
746 // turn highlighting and underlining off
747 wattroff(window, A_UNDERLINE);
748 wattron(window, COLOR_PAIR(CP_WHITE));
751 wprintw(window, "[");
758 wattron(window, COLOR_PAIR(CP_RED));
762 wattron(window, A_UNDERLINE);
764 // enable inline code
767 wattron(window, COLOR_PAIR(CP_BLACK));
769 // do nothing for backslashes
772 // push special char to stack
773 (stack->push)(stack, *i);
776 waddnwstr(window, i, 1);
781 // remove backslash from stack
782 if((stack->top)(stack, L'\\'))
785 // print regular char
786 waddnwstr(window, i, 1);
790 // pop stack until empty to prevent formated trailing spaces
791 while(!(stack->empty)(stack)) {
792 switch((stack->pop)(stack)) {
796 wattron(window, COLOR_PAIR(CP_WHITE));
800 wattroff(window, A_UNDERLINE);
802 // disable inline code
805 wattron(window, COLOR_PAIR(CP_WHITE));
807 // do nothing for backslashes
811 (stack->delete)(stack);
814 void fade_out(WINDOW *window, int trans, int colors, int invert) {
816 if(colors && COLORS == 256) {
817 for(i = 22; i >= 0; i--) {
821 init_pair(CP_WHITE, white_ramp_invert[i], trans);
822 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
823 init_pair(CP_RED, red_ramp_invert[i], trans);
824 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
826 init_pair(CP_WHITE, white_ramp[i], trans);
827 init_pair(CP_BLUE, blue_ramp[i], trans);
828 init_pair(CP_RED, red_ramp[i], trans);
829 init_pair(CP_BLACK, 16, white_ramp[i]);
832 // refresh window with new color
835 // delay for our eyes to recognize the change
841 void fade_in(WINDOW *window, int trans, int colors, int invert) {
843 if(colors && COLORS == 256) {
844 for(i = 0; i <= 23; i++) {
846 // brighten color pairs
848 init_pair(CP_WHITE, white_ramp_invert[i], trans);
849 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
850 init_pair(CP_RED, red_ramp_invert[i], trans);
851 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
853 init_pair(CP_WHITE, white_ramp[i], trans);
854 init_pair(CP_BLUE, blue_ramp[i], trans);
855 init_pair(CP_RED, red_ramp[i], trans);
856 init_pair(CP_BLACK, 16, white_ramp[i]);
859 // refresh window with new color
862 // delay for our eyes to recognize the change
868 int int_length (int val) {
877 int get_slide_number(char init) {
878 int retval = init - '0';
880 // block for tenths of a second when using getch, ERR if no input
881 halfdelay(GOTO_SLIDE_DELAY);
882 while((c = getch()) != ERR) {
883 if (c < '0' || c > '9') {
887 retval = (retval * 10) + (c - '0');
889 nocbreak(); // cancel half delay mode
890 cbreak(); // go back to cbreak