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/>.
24 #include <locale.h> // setlocale
27 #include <string.h> // strchr
30 #include "include/viewer.h"
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, 32,
39 33, 38, 39, 44, 45, 45,
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 int ncurses_display(deck_t *deck, int notrans, int nofade) {
50 int l = 0; // line number
51 int colors = 0; // amount of colors supported
52 int fade = 0; // disable color fading by default
53 int trans = -1; // enable transparency if term supports it
54 int max_lines = 0; // max lines per slide
55 int max_cols = 0; // max columns per line
56 int offset; // text offset
58 // header line 1 is displayed at the top
59 int bar_top = (deck->headers > 0) ? 1 : 0;
60 // header line 2 and 3 are displayed at the bottom
61 int bar_bottom = (deck->headers > 1) ? 1 : 0;
63 slide_t *slide = deck->slide;
67 // set max_lines if line count exceeded
68 max_lines = (slide->lines > max_lines) ? slide->lines : max_lines;
71 // set max_cols if length exceeded
72 max_cols = (line->length > max_cols) ? line->length : max_cols;
78 // set locale to display UTF-8 correctly in ncurses
79 setlocale(LC_CTYPE, "");
81 // replace stdin with current tty if markdown input was piped
82 freopen("/dev/tty", "rw", stdin);
87 if((max_cols > COLS) ||
88 (max_lines + bar_top + bar_bottom + 2 > LINES)) {
90 fprintf(stderr, "Error: Terminal size %ix%i to small. Need at least %ix%i.\n",
91 COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2);
96 // replace stdin with current tty if markdown input was piped
97 freopen("/dev/tty", "rw", stdin);
102 // disable output of keyboard typing
105 // make getch() process one char at a time
112 if(has_colors() == TRUE) {
114 use_default_colors();
116 if(notrans) trans = 0; // 0 is black
120 init_pair(CP_WHITE, 255, trans);
121 init_pair(CP_BLUE, 123, trans);
122 init_pair(CP_RED, 213, trans);
123 init_pair(CP_YELLOW, 208, trans);
125 // enable color fading
126 if(!nofade) fade = 1;
130 init_pair(CP_WHITE, 7, trans);
131 init_pair(CP_BLUE, 4, trans);
132 init_pair(CP_RED, 1, trans);
133 init_pair(CP_YELLOW, 3, trans);
139 // set background color of main window
141 wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
146 offset = next_blank(line->text, 0) + 1;
147 // add text to header
149 0, (COLS - line->length + offset) / 2,
150 "%s", &line->text->text[offset]);
155 line = deck->header->next;
156 offset = next_blank(line->text, 0) + 1;
157 // add text to left footer
160 "%s", &line->text->text[offset]);
162 if(deck->headers > 2) {
163 line = deck->header->next->next;
164 offset = next_blank(line->text, 0) + 1;
165 // add text to right footer
167 LINES - 1, COLS - line->length + offset - 3,
168 "%s", &line->text->text[offset]);
172 // make header + fooder visible
176 WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
178 wbkgd(content, COLOR_PAIR(CP_WHITE));
190 add_line(content, l, (COLS - max_cols) / 2, line, max_cols);
195 // make content visible
200 fade_in(content, trans, colors);
202 // re-enable fading after any undefined key press
203 if(COLORS == 256 && !nofade) fade = 1;
205 // wait for user input
208 // evaluate user input
211 // show previous slide
214 case 8: // BACKSPACE (ascii)
215 case 127: // BACKSPACE (xterm)
216 case 263: // BACKSPACE (getty)
236 // do not fade out on exit
242 // disable fading on undefined key press
249 fade_out(content, trans, colors);
257 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols) {
258 int i = 0; // increment
259 char *c; // char pointer for iteration
260 char *special = "\\*_"; // list of interpreted chars
261 cstack_t *stack = cstack_init();
263 if(line->text->text) {
264 int offset = 0; // text offset
265 offset = next_nonblank(line->text, 0);
268 if(CHECK_BIT(line->bits, IS_CODE)) {
270 // set static offset for code
271 offset = CODE_INDENT;
273 // reverse color for code blocks
274 wattron(window, A_REVERSE);
279 "%s", &line->text->text[offset]);
282 } else if(CHECK_BIT(line->bits, IS_QUOTE)) {
283 //TODO replace greater sign with color block
285 //FIXME remove dummy print code
288 "%s", &line->text->text[offset]);
293 if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
295 // set headline color
296 wattron(window, COLOR_PAIR(CP_BLUE));
298 // enable underline for H1
299 if(CHECK_BIT(line->bits, IS_H1))
300 wattron(window, A_UNDERLINE);
303 while(line->text->text[offset] == '#')
304 offset = next_word(line->text, offset);
309 "%s", &line->text->text[offset]);
311 wattroff(window, A_UNDERLINE);
314 // move the cursor in position
317 // for each char in line
318 c = line->text->text;
321 // if char is in special char list
322 if(strchr(special, *c)) {
324 // closing special char (or second backslash)
325 if((stack->top)(stack, *c)) {
328 // print escaped backslash
330 wprintw(window, "%c", *c);
334 wattron(window, COLOR_PAIR(CP_WHITE));
338 wattroff(window, A_UNDERLINE);
342 // remove top special char from stack
345 // treat special as regular char
346 } else if((stack->top)(stack, '\\')) {
347 wprintw(window, "%c", *c);
349 // remove backslash from stack
352 // opening special char
357 wattron(window, COLOR_PAIR(CP_RED));
361 wattron(window, A_UNDERLINE);
363 // do nothing for backslashes
366 // push special char to stack
367 (stack->push)(stack, *c);
371 // print regular char
372 wprintw(window, "%c", *c);
378 //TODO pop stack until empty
382 // fill rest off line with spaces
383 for(i = getcurx(window) - x; i < max_cols; i++)
384 wprintw(window, "%s", " ");
386 // reset to default color
387 wattron(window, COLOR_PAIR(CP_WHITE));
388 wattroff(window, A_UNDERLINE);
389 wattroff(window, A_REVERSE);
392 (stack->delete)(stack);
395 void fade_out(WINDOW *window, int trans, int colors) {
398 for(i = 22; i >= 0; i--) {
399 // darken color pairs
400 init_pair(CP_WHITE, white_ramp[i], trans);
401 init_pair(CP_BLUE, blue_ramp[i], trans);
402 init_pair(CP_RED, red_ramp[i], trans);
403 // refresh window with new color
405 // delay for our eyes to recognize the change
411 void fade_in(WINDOW *window, int trans, int colors) {
414 for(i = 0; i <= 23; i++) {
415 // lighten color pairs
416 init_pair(CP_WHITE, white_ramp[i], trans);
417 init_pair(CP_BLUE, blue_ramp[i], trans);
418 init_pair(CP_RED, red_ramp[i], trans);
419 // refresh window with new color
421 // delay for our eyes to recognize the change