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
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) {
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
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;
87 // set locale to display UTF-8 correctly in ncurses
88 setlocale(LC_CTYPE, "");
98 if(line->length > COLS) {
103 i = prev_blank(line->text, offset + COLS) - offset;
105 // single word is > COLS
107 // calculate min_width
108 i = next_blank(line->text, offset + COLS) - offset;
114 fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
115 fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n");
121 max_cols = MAX(i, max_cols);
123 // iterate to next line
124 offset = prev_blank(line->text, offset + COLS);
125 i = line->length - offset;
128 // set max_cols one last time
129 max_cols = MAX(i, max_cols);
132 max_cols = MAX(line->length, max_cols);
138 max_lines = MAX(lc, max_lines);
144 if(max_lines + bar_top + bar_bottom > LINES) {
150 fprintf(stderr, "Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
151 fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
159 // disable output of keyboard typing
162 // make getch() process one char at a time
169 if(has_colors() == TRUE) {
171 use_default_colors();
178 trans = 15; // white in 256 color mode
180 trans = 16; // black in 256 color mode
185 init_pair(CP_WHITE, 232, trans);
186 init_pair(CP_BLUE, 21, trans);
187 init_pair(CP_RED, 196, trans);
188 init_pair(CP_BLACK, 15, 232);
190 init_pair(CP_WHITE, 255, trans);
191 init_pair(CP_BLUE, 123, trans);
192 init_pair(CP_RED, 213, trans);
193 init_pair(CP_BLACK, 16, 255);
195 init_pair(CP_YELLOW, 208, trans);
197 // enable color fading
206 trans = 7; // white in 8 color mode
208 trans = 0; // black in 8 color mode
213 init_pair(CP_WHITE, 0, trans);
214 init_pair(CP_BLACK, 7, 0);
216 init_pair(CP_WHITE, 7, trans);
217 init_pair(CP_BLACK, 0, 7);
219 init_pair(CP_BLUE, 4, trans);
220 init_pair(CP_RED, 1, trans);
221 init_pair(CP_YELLOW, 3, trans);
227 // set background color of main window
229 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
232 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
234 wbkgd(content, COLOR_PAIR(CP_WHITE));
245 // always resize window in case terminal geometry has changed
246 wresize(content, LINES - bar_top - bar_bottom, COLS);
251 offset = next_blank(line->text, 0) + 1;
252 // add text to header
254 0, (COLS - line->length + offset) / 2,
255 "%s", &line->text->text[offset]);
259 if(deck->headers > 1) {
260 line = deck->header->next;
261 offset = next_blank(line->text, 0) + 1;
262 // add text to left footer
265 "%s", &line->text->text[offset]);
267 // add slide number to right footer
269 LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
270 "%d / %d", sc, deck->slides);
272 // make header + fooder visible
281 add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
282 l += (line->length / COLS) + 1;
287 getmaxyx( content, ymax, i );
288 for (i = 0; i < url_get_amount(); i++) {
289 mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
290 "[%d] %s", i, url_get_target(i));
293 // make content visible
298 fade_in(content, trans, colors, invert);
300 // re-enable fading after any undefined key press
301 if(COLORS == 256 && !nofade)
304 // wait for user input
307 // evaluate user input
311 // show previous slide
315 case 8: // BACKSPACE (ascii)
316 case 127: // BACKSPACE (xterm)
317 case 263: // BACKSPACE (getty)
354 if(i <= deck->slides) {
371 // disable fading if slide n doesn't exist
384 for(i = sc; i <= deck->slides; i++) {
394 // do not fade out on exit
400 // disable fading on undefined key press
407 fade_out(content, trans, colors, invert);
417 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
419 if(!line->text->text) {
424 int offset = 0; // text offset
426 // move the cursor in position
429 // IS_UNORDERED_LIST_3
430 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
431 offset = next_nonblank(line->text, 0);
433 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
434 strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " | " : " ");
436 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
437 strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " | " : " ");
439 strcpy(&prompt[8], " +- ");
446 if(!CHECK_BIT(line->bits, IS_CODE))
447 inline_display(window, &line->text->text[offset], colors);
449 // IS_UNORDERED_LIST_2
450 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
451 offset = next_nonblank(line->text, 0);
453 strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " | " : " ");
455 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
456 strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " | " : " ");
458 strcpy(&prompt[4], " +- ");
465 if(!CHECK_BIT(line->bits, IS_CODE))
466 inline_display(window, &line->text->text[offset], colors);
468 // IS_UNORDERED_LIST_1
469 } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
470 offset = next_nonblank(line->text, 0);
473 if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
474 strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " | " : " ");
476 strcpy(&prompt[0], " +- ");
483 if(!CHECK_BIT(line->bits, IS_CODE))
484 inline_display(window, &line->text->text[offset], colors);
488 if(CHECK_BIT(line->bits, IS_CODE)) {
490 // set static offset for code
491 offset = CODE_INDENT;
493 // reverse color for code blocks
495 wattron(window, COLOR_PAIR(CP_BLACK));
499 "%s", &line->text->text[offset]);
502 if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
503 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
504 !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
505 !CHECK_BIT(line->bits, IS_CODE)) {
508 if(CHECK_BIT(line->bits, IS_QUOTE)) {
509 while(line->text->text[offset] == '>') {
510 // print a reverse color block
512 wattron(window, COLOR_PAIR(CP_BLACK));
513 wprintw(window, "%s", " ");
514 wattron(window, COLOR_PAIR(CP_WHITE));
515 wprintw(window, "%s", " ");
517 wprintw(window, "%s", ">");
520 // find next quote or break
522 if(line->text->text[offset] == ' ')
523 offset = next_word(line->text, offset);
526 inline_display(window, &line->text->text[offset], colors);
530 if(CHECK_BIT(line->bits, IS_CENTER)) {
531 if(line->length < max_cols) {
532 wmove(window, y, x + ((max_cols - line->length) / 2));
537 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
539 // set headline color
541 wattron(window, COLOR_PAIR(CP_BLUE));
543 // enable underline for H1
544 if(CHECK_BIT(line->bits, IS_H1))
545 wattron(window, A_UNDERLINE);
548 while(line->text->text[offset] == '#')
549 offset = next_word(line->text, offset);
553 "%s", &line->text->text[offset]);
555 wattroff(window, A_UNDERLINE);
557 // no line-wide markdown
560 inline_display(window, &line->text->text[offset], colors);
565 // fill rest off line with spaces
566 for(i = getcurx(window) - x; i < max_cols; i++)
567 wprintw(window, "%s", " ");
569 // reset to default color
571 wattron(window, COLOR_PAIR(CP_WHITE));
572 wattroff(window, A_UNDERLINE);
575 void inline_display(WINDOW *window, const char *c, const int colors) {
576 const static char *special = "\\*_`["; // list of interpreted chars
577 const char *i = c; // iterator
578 const char *start_link_name, *start_url;
579 int length_link_name, url_num;
580 cstack_t *stack = cstack_init();
583 // for each char in line
586 // if char is in special char list
587 if(strchr(special, *i)) {
589 // closing special char (or second backslash)
590 // only if not followed by :alnum:
591 if((stack->top)(stack, *i) &&
592 (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
595 // print escaped backslash
597 wprintw(window, "%c", *i);
602 wattron(window, COLOR_PAIR(CP_WHITE));
606 wattroff(window, A_UNDERLINE);
608 // disable inline code
611 wattron(window, COLOR_PAIR(CP_WHITE));
615 // remove top special char from stack
618 // treat special as regular char
619 } else if((stack->top)(stack, '\\')) {
620 wprintw(window, "%c", *i);
622 // remove backslash from stack
625 // opening special char
628 // emphasis or code span can start after new-line or space only
629 // and of cause after another emphasis markup
630 //TODO this condition looks ugly
633 ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
636 if (*i == '[' && strchr(i, ']')) {
638 if (strchr(i, ']')[1] == '(') {// url in pandoc style
640 // turn higlighting and underlining on
642 wattron(window, COLOR_PAIR(CP_BLUE));
643 wattron(window, A_UNDERLINE);
647 // print the content of the label
648 // the label is printed as is
650 wprintw(window, "%c", *i);
654 length_link_name = i - 1 - start_link_name;
661 while (*i != ')') i++;
663 url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
665 wprintw(window, "[%d]", url_num);
666 // turn highlighting and undelining off
667 wattroff(window, A_UNDERLINE);
668 wattron(window, COLOR_PAIR(CP_WHITE));
669 } else wprintw(window, "[");
674 wattron(window, COLOR_PAIR(CP_RED));
678 wattron(window, A_UNDERLINE);
680 // enable inline code
683 wattron(window, COLOR_PAIR(CP_BLACK));
685 // do nothing for backslashes
688 // push special char to stack
689 (stack->push)(stack, *i);
692 wprintw(window, "%c", *i);
697 // remove backslash from stack
698 if((stack->top)(stack, '\\'))
701 // print regular char
702 wprintw(window, "%c", *i);
706 // pop stack until empty to prevent formated trailing spaces
707 while(!(stack->empty)(stack)) {
708 switch((stack->pop)(stack)) {
712 wattron(window, COLOR_PAIR(CP_WHITE));
716 wattroff(window, A_UNDERLINE);
718 // disable inline code
721 wattron(window, COLOR_PAIR(CP_WHITE));
723 // do nothing for backslashes
727 (stack->delete)(stack);
730 void fade_out(WINDOW *window, int trans, int colors, int invert) {
732 if(colors && COLORS == 256) {
733 for(i = 22; i >= 0; i--) {
737 init_pair(CP_WHITE, white_ramp_invert[i], trans);
738 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
739 init_pair(CP_RED, red_ramp_invert[i], trans);
740 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
742 init_pair(CP_WHITE, white_ramp[i], trans);
743 init_pair(CP_BLUE, blue_ramp[i], trans);
744 init_pair(CP_RED, red_ramp[i], trans);
745 init_pair(CP_BLACK, 16, white_ramp[i]);
748 // refresh window with new color
751 // delay for our eyes to recognize the change
757 void fade_in(WINDOW *window, int trans, int colors, int invert) {
759 if(colors && COLORS == 256) {
760 for(i = 0; i <= 23; i++) {
762 // brighten color pairs
764 init_pair(CP_WHITE, white_ramp_invert[i], trans);
765 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
766 init_pair(CP_RED, red_ramp_invert[i], trans);
767 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
769 init_pair(CP_WHITE, white_ramp[i], trans);
770 init_pair(CP_BLUE, blue_ramp[i], trans);
771 init_pair(CP_RED, red_ramp[i], trans);
772 init_pair(CP_BLACK, 16, white_ramp[i]);
775 // refresh window with new color
778 // delay for our eyes to recognize the change
784 int int_length (int val) {