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) 2014 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 <locale.h> // setlocale
26 #include <string.h> // strchr
27 #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) {
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
77 // header line 1 is displayed at the top
78 int bar_top = (deck->headers > 0) ? 1 : 0;
79 // header line 2 is displayed at the bottom
80 // anyway we display the slide number at the bottom
83 slide_t *slide = deck->slide;
86 // set locale to display UTF-8 correctly in ncurses
87 setlocale(LC_CTYPE, "");
98 if (line && line->text && line->text->text)
99 lc += url_count_inline(line->text->text);
101 if (line && line->text && line->text->text)
102 line->length -= url_len_inline(line->text->text);
104 if(line->length > COLS) {
109 i = prev_blank(line->text, offset + COLS) - offset;
111 // single word is > COLS
113 // calculate min_width
114 i = next_blank(line->text, offset + COLS) - offset;
120 fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
121 fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n");
127 max_cols = MAX(i, max_cols);
129 // iterate to next line
130 offset = prev_blank(line->text, offset + COLS);
131 i = line->length - offset;
134 // set max_cols one last time
135 max_cols = MAX(i, max_cols);
138 max_cols = MAX(line->length, max_cols);
144 max_lines = MAX(lc, max_lines);
150 if(max_lines + bar_top + bar_bottom > LINES) {
156 fprintf(stderr, "Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
157 fprintf(stderr, "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));
251 // always resize window in case terminal geometry has changed
252 wresize(content, LINES - bar_top - bar_bottom, COLS);
257 offset = next_blank(line->text, 0) + 1;
258 // add text to header
260 0, (COLS - line->length + offset) / 2,
261 "%s", &line->text->text[offset]);
265 if(deck->headers > 1) {
266 line = deck->header->next;
267 offset = next_blank(line->text, 0) + 1;
268 // add text to left footer
271 "%s", &line->text->text[offset]);
273 // add slide number to right footer
275 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
276 "%d / %d", sc, deck->slides);
278 // make header + fooder visible
287 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
288 l += (line->length / COLS) + 1;
293 getmaxyx( content, ymax, i );
294 for (i = 0; i < url_get_amount(); i++) {
295 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
296 "[%d] %s", i, url_get_target(i));
299 // make content visible
304 fade_in(content, trans, colors, invert);
306 // re-enable fading after any undefined key press
307 if(COLORS == 256 && !nofade)
310 // wait for user input
313 // evaluate user input
317 // show previous slide
321 case 8: // BACKSPACE (ascii)
322 case 127: // BACKSPACE (xterm)
323 case 263: // BACKSPACE (getty)
360 if(i <= deck->slides) {
377 // disable fading if slide n doesn't exist
390 for(i = sc; i <= deck->slides; i++) {
400 // do not fade out on exit
406 // disable fading on undefined key press
413 fade_out(content, trans, colors, invert);
423 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
425 if(!line->text->text) {
430 int offset = 0; // text offset
432 // move the cursor in position
435 // IS_UNORDERED_LIST_3
436 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
437 offset = next_nonblank(line->text, 0);
439 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
440 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
442 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
443 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
445 strcpy(&prompt[8], " +- ");
452 if(!CHECK_BIT(line->bits, IS_CODE))
453 inline_display(window, &line->text->text[offset], colors);
455 // IS_UNORDERED_LIST_2
456 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
457 offset = next_nonblank(line->text, 0);
459 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
461 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
462 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
464 strcpy(&prompt[4], " +- ");
471 if(!CHECK_BIT(line->bits, IS_CODE))
472 inline_display(window, &line->text->text[offset], colors);
474 // IS_UNORDERED_LIST_1
475 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
476 offset = next_nonblank(line->text, 0);
479 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
480 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
482 strcpy(&prompt[0], " +- ");
489 if(!CHECK_BIT(line->bits, IS_CODE))
490 inline_display(window, &line->text->text[offset], colors);
494 if(CHECK_BIT(line->bits, IS_CODE)) {
496 // set static offset for code
497 offset = CODE_INDENT;
499 // reverse color for code blocks
501 wattron(window, COLOR_PAIR(CP_BLACK));
505 "%s", &line->text->text[offset]);
508 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
509 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
510 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
511 !CHECK_BIT(line->bits, IS_CODE)) {
514 if(CHECK_BIT(line->bits, IS_QUOTE)) {
515 while(line->text->text[offset] == '>') {
516 // print a reverse color block
518 wattron(window, COLOR_PAIR(CP_BLACK));
519 wprintw(window, "%s", " ");
520 wattron(window, COLOR_PAIR(CP_WHITE));
521 wprintw(window, "%s", " ");
523 wprintw(window, "%s", ">");
526 // find next quote or break
528 if(line->text->text[offset] == ' ')
529 offset = next_word(line->text, offset);
532 inline_display(window, &line->text->text[offset], colors);
536 if(CHECK_BIT(line->bits, IS_CENTER)) {
537 if(line->length < max_cols) {
538 wmove(window, y, x + ((max_cols - line->length) / 2));
543 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
545 // set headline color
547 wattron(window, COLOR_PAIR(CP_BLUE));
549 // enable underline for H1
550 if(CHECK_BIT(line->bits, IS_H1))
551 wattron(window, A_UNDERLINE);
554 while(line->text->text[offset] == '#')
555 offset = next_word(line->text, offset);
559 "%s", &line->text->text[offset]);
561 wattroff(window, A_UNDERLINE);
563 // no line-wide markdown
566 inline_display(window, &line->text->text[offset], colors);
571 // fill rest off line with spaces
572 for(i = getcurx(window) - x; i < max_cols; i++)
573 wprintw(window, "%s", " ");
575 // reset to default color
577 wattron(window, COLOR_PAIR(CP_WHITE));
578 wattroff(window, A_UNDERLINE);
581 void inline_display(WINDOW *window, const char *c, const int colors) {
582 const static char *special = "\\*_`!["; // list of interpreted chars
583 const char *i = c; // iterator
584 const char *start_link_name, *start_url;
585 int length_link_name, url_num;
586 cstack_t *stack = cstack_init();
589 // for each char in line
592 // if char is in special char list
593 if(strchr(special, *i)) {
595 // closing special char (or second backslash)
596 // only if not followed by :alnum:
597 if((stack->top)(stack, *i) &&
598 (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
601 // print escaped backslash
603 wprintw(window, "%c", *i);
608 wattron(window, COLOR_PAIR(CP_WHITE));
612 wattroff(window, A_UNDERLINE);
614 // disable inline code
617 wattron(window, COLOR_PAIR(CP_WHITE));
621 // remove top special char from stack
624 // treat special as regular char
625 } else if((stack->top)(stack, '\\')) {
626 wprintw(window, "%c", *i);
628 // remove backslash from stack
631 // opening special char
634 // emphasis or code span can start after new-line or space only
635 // and of cause after another emphasis markup
636 //TODO this condition looks ugly
639 ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
642 // url in pandoc style
643 if ((*i == '[' && strchr(i, ']')) ||
644 (*i == '!' && *(i + 1) == '[' && strchr(i, ']'))) {
648 if (strchr(i, ']')[1] == '(') {
651 // turn higlighting and underlining on
653 wattron(window, COLOR_PAIR(CP_BLUE));
654 wattron(window, A_UNDERLINE);
658 // print the content of the label
659 // the label is printed as is
661 wprintw(window, "%c", *i);
665 length_link_name = i - 1 - start_link_name;
672 while (*i != ')') i++;
674 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
676 wprintw(window, " [%d]", url_num);
678 // turn highlighting and underlining off
679 wattroff(window, A_UNDERLINE);
680 wattron(window, COLOR_PAIR(CP_WHITE));
683 wprintw(window, "[");
690 wattron(window, COLOR_PAIR(CP_RED));
694 wattron(window, A_UNDERLINE);
696 // enable inline code
699 wattron(window, COLOR_PAIR(CP_BLACK));
701 // do nothing for backslashes
704 // push special char to stack
705 (stack->push)(stack, *i);
708 wprintw(window, "%c", *i);
713 // remove backslash from stack
714 if((stack->top)(stack, '\\'))
717 // print regular char
718 wprintw(window, "%c", *i);
722 // pop stack until empty to prevent formated trailing spaces
723 while(!(stack->empty)(stack)) {
724 switch((stack->pop)(stack)) {
728 wattron(window, COLOR_PAIR(CP_WHITE));
732 wattroff(window, A_UNDERLINE);
734 // disable inline code
737 wattron(window, COLOR_PAIR(CP_WHITE));
739 // do nothing for backslashes
743 (stack->delete)(stack);
746 void fade_out(WINDOW *window, int trans, int colors, int invert) {
748 if(colors && COLORS == 256) {
749 for(i = 22; i >= 0; i--) {
753 init_pair(CP_WHITE, white_ramp_invert[i], trans);
754 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
755 init_pair(CP_RED, red_ramp_invert[i], trans);
756 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
758 init_pair(CP_WHITE, white_ramp[i], trans);
759 init_pair(CP_BLUE, blue_ramp[i], trans);
760 init_pair(CP_RED, red_ramp[i], trans);
761 init_pair(CP_BLACK, 16, white_ramp[i]);
764 // refresh window with new color
767 // delay for our eyes to recognize the change
773 void fade_in(WINDOW *window, int trans, int colors, int invert) {
775 if(colors && COLORS == 256) {
776 for(i = 0; i <= 23; i++) {
778 // brighten color pairs
780 init_pair(CP_WHITE, white_ramp_invert[i], trans);
781 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
782 init_pair(CP_RED, red_ramp_invert[i], trans);
783 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
785 init_pair(CP_WHITE, white_ramp[i], trans);
786 init_pair(CP_BLUE, blue_ramp[i], trans);
787 init_pair(CP_RED, red_ramp[i], trans);
788 init_pair(CP_BLACK, 16, white_ramp[i]);
791 // refresh window with new color
794 // delay for our eyes to recognize the change
800 int int_length (int val) {