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 mpd.
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/>.
26 #include <string.h> // strchr
29 #include "include/viewer.h"
31 static short white_ramp[24] = { 16, 232, 233, 234, 235, 236,
32 237, 238, 239, 240, 241, 242,
33 244, 245, 246, 247, 248, 249,
34 250, 251, 252, 253, 254, 255 };
36 static short blue_ramp[24] = { 16, 17, 17, 18, 18, 19,
37 19, 20, 20, 21, 27, 32,
38 33, 38, 39, 44, 45, 45,
39 81, 81, 51, 51, 123, 123 };
41 static short red_ramp[24] = { 16, 52, 52, 53, 53, 89,
42 89, 90, 90, 126, 127, 127,
43 163, 163, 164, 164, 200, 200,
44 201, 201, 207, 207, 213, 213 };
46 int ncurses_display(deck_t *deck, int notrans, int nofade) {
49 int l = 0; // line number
50 int colors = 0; // amount of colors supported
51 int fade = 0; // disable color fading by default
52 int trans = -1; // enable transparency if term supports it
53 int max_lines = 0; // max lines per slide
54 int max_cols = 0; // max columns per line
55 int offset; // text offset
57 // header line 1 is displayed at the top
58 int bar_top = (deck->headers > 0) ? 1 : 0;
59 // header line 2 and 3 are displayed at the bottom
60 int bar_bottom = (deck->headers > 1) ? 1 : 0;
62 slide_t *slide = deck->slide;
66 // set max_lines if line count exceeded
67 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
70 // set max_cols if length exceeded
71 max_cols = (line->length > max_cols) ? line->length : max_cols;
77 // replace stdin with current tty if markdown input was piped
78 freopen("/dev/tty", "rw", stdin);
83 if((max_cols > COLS) ||
84 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
86 fprintf(stderr, "Error: Terminal size %ix%i to small. Need at least %ix%i.\n",
87 COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
92 // replace stdin with current tty if markdown input was piped
93 freopen("/dev/tty", "rw", stdin);
98 // disable output of keyboard typing
101 // make getch() process one char at a time
108 if(has_colors() == TRUE) {
110 use_default_colors();
112 if(notrans) trans = 0; // 0 is black
116 init_pair(CP_WHITE, 255, trans);
117 init_pair(CP_BLUE, 123, trans);
118 init_pair(CP_RED, 213, trans);
119 init_pair(CP_YELLOW, 208, trans);
121 // enable color fading
122 if(!nofade) fade = 1;
126 init_pair(CP_WHITE, 7, trans);
127 init_pair(CP_BLUE, 4, trans);
128 init_pair(CP_RED, 1, trans);
129 init_pair(CP_YELLOW, 3, trans);
135 // set background color of main window
137 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
142 offset = next_blank(line->text, 0) + 1;
143 // add text to header
145 0, (COLS - line->length + offset) / 2,
146 "%s", &line->text->text[offset]);
151 line = deck->header->next;
152 offset = next_blank(line->text, 0) + 1;
153 // add text to left footer
156 "%s", &line->text->text[offset]);
158 if(deck->headers > 2) {
159 line = deck->header->next->next;
160 offset = next_blank(line->text, 0) + 1;
161 // add text to right footer
163 LINES - 1, COLS - line->length + offset - 3,
164 "%s", &line->text->text[offset]);
168 // make header + fooder visible
172 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
174 wbkgd(content, COLOR_PAIR(CP_WHITE));
186 add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
191 // make content visible
196 fade_in(content, trans, colors);
198 // re-enable fading after any undefined key press
199 if(COLORS == 256 && !nofade) fade = 1;
201 // wait for user input
204 // evaluate user input
207 // show previous slide
210 case 8: // BACKSPACE (ascii)
211 case 127: // BACKSPACE (xterm)
212 case 263: // BACKSPACE (getty)
232 // do not fade out on exit
238 // disable fading on undefined key press
245 fade_out(content, trans, colors);
253 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
254 int i = 0; // increment
255 char *c; // char pointer for iteration
256 char *special = "\\*_"; // list of interpreted chars
257 cstack_t *stack = cstack_init();
259 if(line->text->text) {
260 int offset = 0; // text offset
261 offset = next_nonblank(line->text, 0);
264 if(CHECK_BIT(line->bits, IS_CODE)) {
266 // set static offset for code
267 offset = CODE_INDENT;
269 // reverse color for code blocks
270 wattron(window, A_REVERSE);
275 "%s", &line->text->text[offset]);
278 } else if(CHECK_BIT(line->bits, IS_QUOTE)) {
279 //TODO replace greater sign with color block
281 //FIXME remove dummy print code
284 "%s", &line->text->text[offset]);
289 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
291 // set headline color
292 wattron(window, COLOR_PAIR(CP_BLUE));
294 // enable underline for H1
295 if(CHECK_BIT(line->bits, IS_H1))
296 wattron(window, A_UNDERLINE);
299 while(line->text->text[offset] == '#')
300 offset = next_word(line->text, offset);
305 "%s", &line->text->text[offset]);
307 wattroff(window, A_UNDERLINE);
310 // move the cursor in position
313 // for each char in line
314 c = line->text->text;
317 // if char is in special char list
318 if(strchr(special, *c)) {
320 // closing special char (or second backslash)
321 if((stack->top)(stack, *c)) {
324 // print escaped backslash
326 wprintw(window, "%c", *c);
330 wattron(window, COLOR_PAIR(CP_WHITE));
334 wattroff(window, A_UNDERLINE);
338 // remove top special char from stack
341 // treat special as regular char
342 } else if((stack->top)(stack, '\\')) {
343 wprintw(window, "%c", *c);
345 // remove backslash from stack
348 // opening special char
353 wattron(window, COLOR_PAIR(CP_RED));
357 wattron(window, A_UNDERLINE);
359 // do nothing for backslashes
362 // push special char to stack
363 (stack->push)(stack, *c);
367 // print regular char
368 wprintw(window, "%c", *c);
374 //TODO pop stack until empty
378 // fill rest off line with spaces
379 for(i = getcurx(window) - x; i < max_cols; i++)
380 wprintw(window, "%s", " ");
382 // reset to default color
383 wattron(window, COLOR_PAIR(CP_WHITE));
384 wattroff(window, A_UNDERLINE);
385 wattroff(window, A_REVERSE);
388 (stack->delete)(stack);
391 void fade_out(WINDOW *window, int trans, int colors) {
394 for(i = 22; i >= 0; i--) {
395 // darken color pairs
396 init_pair(CP_WHITE, white_ramp[i], trans);
397 init_pair(CP_BLUE, blue_ramp[i], trans);
398 init_pair(CP_RED, red_ramp[i], trans);
399 // refresh window with new color
401 // delay for our eyes to recognize the change
407 void fade_in(WINDOW *window, int trans, int colors) {
410 for(i = 0; i <= 23; i++) {
411 // lighten color pairs
412 init_pair(CP_WHITE, white_ramp[i], trans);
413 init_pair(CP_BLUE, blue_ramp[i], trans);
414 init_pair(CP_RED, red_ramp[i], trans);
415 // refresh window with new color
417 // delay for our eyes to recognize the change