new feature - reload a file with a keypress, closes #69
[smdp.git] / src / viewer.c
1 /*
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
6  *
7  * This file is part of mdp.
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  */
23
24 #include <ctype.h>  // isalnum
25 #include <locale.h> // setlocale
26 #include <string.h> // strchr
27 #include <unistd.h> // usleep
28
29 #include "viewer.h"
30
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 };
36
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 };
41
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 };
46
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};
52
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};
57
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};
62
63 int ncurses_display(deck_t *deck, int notrans, int nofade, int invert, int reload, int noreload) {
64
65     int c = 0;          // char
66     int i = 0;          // iterate
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
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
81     int bar_bottom = 1;
82
83     slide_t *slide = deck->slide;
84     line_t *line;
85
86     // set locale to display UTF-8 correctly in ncurses
87     setlocale(LC_CTYPE, "");
88
89     // init ncurses
90     initscr();
91
92     while(slide) {
93         lc = 0;
94         line = slide->line;
95
96         while(line && line->text) {
97
98             if (line->text->text)
99                 lc += url_count_inline(line->text->text);
100
101             if (line->text->text)
102                 line->length -= url_len_inline(line->text->text);
103
104             if(line->length > COLS) {
105                 i = line->length;
106                 offset = 0;
107                 while(i > COLS) {
108
109                     i = prev_blank(line->text, offset + COLS) - offset;
110
111                     // single word is > COLS
112                     if(!i) {
113                         // calculate min_width
114                         i = next_blank(line->text, offset + COLS) - offset;
115
116                         // disable ncurses
117                         endwin();
118
119                         // print error
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");
122
123                         // no reload
124                         return 0;
125                     }
126
127                     // set max_cols
128                     max_cols = MAX(i, max_cols);
129
130                     // iterate to next line
131                     offset = prev_blank(line->text, offset + COLS);
132                     i = line->length - offset;
133                     lc++;
134                 }
135                 // set max_cols one last time
136                 max_cols = MAX(i, max_cols);
137             } else {
138                 // set max_cols
139                 max_cols = MAX(line->length, max_cols);
140             }
141             lc++;
142             line = line->next;
143         }
144
145         max_lines = MAX(lc, max_lines);
146
147         slide = slide->next;
148     }
149
150     // not enough lines
151     if(max_lines + bar_top + bar_bottom > LINES) {
152
153         // disable ncurses
154         endwin();
155
156         // print error
157         fprintf(stderr, "Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
158         fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n");
159
160         // no reload
161         return 0;
162     }
163
164     // disable cursor
165     curs_set(0);
166
167     // disable output of keyboard typing
168     noecho();
169
170     // make getch() process one char at a time
171     cbreak();
172
173     // enable arrow keys
174     keypad(stdscr,TRUE);
175
176     // set colors
177     if(has_colors() == TRUE) {
178         start_color();
179         use_default_colors();
180
181         // 256 color mode
182         if(COLORS == 256) {
183
184             if(notrans) {
185                 if(invert) {
186                     trans = 15; // white in 256 color mode
187                 } else {
188                     trans = 16; // black in 256 color mode
189                 }
190             }
191
192             if(invert) {
193                 init_pair(CP_WHITE, 232, trans);
194                 init_pair(CP_BLUE, 21, trans);
195                 init_pair(CP_RED, 196, trans);
196                 init_pair(CP_BLACK, 15, 232);
197             } else {
198                 init_pair(CP_WHITE, 255, trans);
199                 init_pair(CP_BLUE, 123, trans);
200                 init_pair(CP_RED, 213, trans);
201                 init_pair(CP_BLACK, 16, 255);
202             }
203             init_pair(CP_YELLOW, 208, trans);
204
205             // enable color fading
206             if(!nofade)
207                 fade = true;
208
209         // 8 color mode
210         } else {
211
212             if(notrans) {
213                 if(invert) {
214                     trans = 7; // white in 8 color mode
215                 } else {
216                     trans = 0; // black in 8 color mode
217                 }
218             }
219
220             if(invert) {
221                 init_pair(CP_WHITE, 0, trans);
222                 init_pair(CP_BLACK, 7, 0);
223             } else {
224                 init_pair(CP_WHITE, 7, trans);
225                 init_pair(CP_BLACK, 0, 7);
226             }
227             init_pair(CP_BLUE, 4, trans);
228             init_pair(CP_RED, 1, trans);
229             init_pair(CP_YELLOW, 3, trans);
230         }
231
232         colors = 1;
233     }
234
235     // set background color of main window
236     if(colors)
237         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
238
239     // setup main window
240     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
241     if(colors)
242         wbkgd(content, COLOR_PAIR(CP_WHITE));
243
244     slide = deck->slide;
245
246     // find slide to reload
247     while(reload > 1 && reload <= deck->slides) {
248         slide = slide->next;
249         sc++;
250         reload--;
251     }
252
253     // reset reload indicator
254     reload = 0;
255
256     while(slide) {
257
258         url_init();
259
260         // clear windows
261         werase(content);
262         werase(stdscr);
263
264         // always resize window in case terminal geometry has changed
265         wresize(content, LINES - bar_top - bar_bottom, COLS);
266
267         // setup header
268         if(bar_top) {
269             line = deck->header;
270             offset = next_blank(line->text, 0) + 1;
271             // add text to header
272             mvwprintw(stdscr,
273                       0, (COLS - line->length + offset) / 2,
274                       "%s", &line->text->text[offset]);
275         }
276
277         // setup footer
278         if(deck->headers > 1) {
279             line = deck->header->next;
280             offset = next_blank(line->text, 0) + 1;
281             // add text to left footer
282             mvwprintw(stdscr,
283                       LINES - 1, 3,
284                       "%s", &line->text->text[offset]);
285         }
286         // add slide number to right footer
287         mvwprintw(stdscr,
288                   LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
289                   "%d / %d", sc, deck->slides);
290
291         // make header + fooder visible
292         wrefresh(content);
293         wrefresh(stdscr);
294
295         line = slide->line;
296         l = 0;
297
298         // print lines
299         while(line) {
300             add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
301             l += (line->length / COLS) + 1;
302             line = line->next;
303         }
304
305         int i, ymax;
306         getmaxyx( content, ymax, i );
307         for (i = 0; i < url_get_amount(); i++) {
308             mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
309                       "[%d] %s", i, url_get_target(i));
310         }
311
312         // make content visible
313         wrefresh(content);
314
315         // fade in
316         if(fade)
317             fade_in(content, trans, colors, invert);
318
319         // re-enable fading after any undefined key press
320         if(COLORS == 256 && !nofade)
321             fade = true;
322
323         // wait for user input
324         c = getch();
325
326         // evaluate user input
327         i = 0;
328         switch(c) {
329
330             // show previous slide
331             case KEY_UP:
332             case KEY_LEFT:
333             case KEY_PPAGE:
334             case 8:   // BACKSPACE (ascii)
335             case 127: // BACKSPACE (xterm)
336             case 263: // BACKSPACE (getty)
337             case 'h':
338             case 'k':
339                 if(slide->prev) {
340                     slide = slide->prev;
341                     sc--;
342                 } else {
343                     fade = false;
344                 }
345                 break;
346
347             // show next slide
348             case KEY_DOWN:
349             case KEY_RIGHT:
350             case KEY_NPAGE:
351             case '\n': // ENTER
352             case ' ':  // SPACE
353             case 'j':
354             case 'l':
355                 if(slide->next) {
356                     slide = slide->next;
357                     sc++;
358                 } else {
359                     fade = false;
360                 }
361                 break;
362
363             // show slide n
364             case '9': i++;
365             case '8': i++;
366             case '7': i++;
367             case '6': i++;
368             case '5': i++;
369             case '4': i++;
370             case '3': i++;
371             case '2': i++;
372             case '1': i++;
373                 if(i <= deck->slides) {
374                     while(sc != i) {
375                         // search forward
376                         if(sc < i) {
377                             if(slide->next) {
378                                 slide = slide->next;
379                                 sc++;
380                             }
381                         // search backward
382                         } else {
383                             if(slide->prev) {
384                                 slide = slide->prev;
385                                 sc--;
386                             }
387                         }
388                     }
389                 } else {
390                     // disable fading if slide n doesn't exist
391                     fade = false;
392                 }
393                 break;
394
395             // show first slide
396             case KEY_HOME:
397                 slide = deck->slide;
398                 sc = 1;
399                 break;
400
401             // show last slide
402             case KEY_END:
403                 for(i = sc; i <= deck->slides; i++) {
404                     if(slide->next) {
405                             slide = slide->next;
406                             sc++;
407                     }
408                 }
409                 break;
410
411             // reload
412             case 'r':
413                 if(noreload == 0) {
414                     // reload slide N
415                     reload = sc;
416                     slide = NULL;
417                 } else {
418                     // disable fading if reload is not possible
419                     fade = false;
420                 }
421                 break;
422
423             // quit
424             case 'q':
425                 // do not fade out on exit
426                 fade = false;
427                 // do not reload
428                 reload = 0;
429                 slide = NULL;
430                 break;
431
432             default:
433                 // disable fading on undefined key press
434                 fade = false;
435                 break;
436         }
437
438         // fade out
439         if(fade)
440             fade_out(content, trans, colors, invert);
441
442         url_purge();
443     }
444
445     // disable ncurses
446     endwin();
447
448     // free ncurses memory
449     delwin(content);
450     if(reload == 0)
451         delwin(stdscr);
452
453     // return reload indicator (0 means no reload)
454     return reload;
455 }
456
457 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
458
459     if(!line->text->text) {
460         return;
461     }
462
463     int i; // increment
464     int offset = 0; // text offset
465
466     // move the cursor in position
467     wmove(window, y, x);
468
469     // IS_UNORDERED_LIST_3
470     if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
471         offset = next_nonblank(line->text, 0);
472         char prompt[13];
473         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
474         strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
475
476         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
477             strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " |  " : "    ");
478         } else {
479             strcpy(&prompt[8], " +- ");
480             offset += 2;
481         }
482
483         wprintw(window,
484                 "%s", prompt);
485
486         if(!CHECK_BIT(line->bits, IS_CODE))
487             inline_display(window, &line->text->text[offset], colors);
488
489     // IS_UNORDERED_LIST_2
490     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
491         offset = next_nonblank(line->text, 0);
492         char prompt[9];
493         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
494
495         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
496             strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
497         } else {
498             strcpy(&prompt[4], " +- ");
499             offset += 2;
500         }
501
502         wprintw(window,
503                 "%s", prompt);
504
505         if(!CHECK_BIT(line->bits, IS_CODE))
506             inline_display(window, &line->text->text[offset], colors);
507
508     // IS_UNORDERED_LIST_1
509     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
510         offset = next_nonblank(line->text, 0);
511         char prompt[5];
512
513         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
514             strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
515         } else {
516             strcpy(&prompt[0], " +- ");
517             offset += 2;
518         }
519
520         wprintw(window,
521                 "%s", prompt);
522
523         if(!CHECK_BIT(line->bits, IS_CODE))
524             inline_display(window, &line->text->text[offset], colors);
525     }
526
527     // IS_CODE
528     if(CHECK_BIT(line->bits, IS_CODE)) {
529
530         // set static offset for code
531         offset = CODE_INDENT;
532
533         // reverse color for code blocks
534         if(colors)
535             wattron(window, COLOR_PAIR(CP_BLACK));
536
537         // print whole lines
538         wprintw(window,
539                 "%s", &line->text->text[offset]);
540     }
541
542     if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
543        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
544        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
545        !CHECK_BIT(line->bits, IS_CODE)) {
546
547         // IS_QUOTE
548         if(CHECK_BIT(line->bits, IS_QUOTE)) {
549             while(line->text->text[offset] == '>') {
550                 // print a reverse color block
551                 if(colors) {
552                     wattron(window, COLOR_PAIR(CP_BLACK));
553                     wprintw(window, "%s", " ");
554                     wattron(window, COLOR_PAIR(CP_WHITE));
555                     wprintw(window, "%s", " ");
556                 } else {
557                     wprintw(window, "%s", ">");
558                 }
559
560                 // find next quote or break
561                 offset++;
562                 if(line->text->text[offset] == ' ')
563                     offset = next_word(line->text, offset);
564             }
565
566             inline_display(window, &line->text->text[offset], colors);
567         } else {
568
569             // IS_CENTER
570             if(CHECK_BIT(line->bits, IS_CENTER)) {
571                 if(line->length < max_cols) {
572                     wmove(window, y, x + ((max_cols - line->length) / 2));
573                 }
574             }
575
576             // IS_H1 || IS_H2
577             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
578
579                 // set headline color
580                 if(colors)
581                     wattron(window, COLOR_PAIR(CP_BLUE));
582
583                 // enable underline for H1
584                 if(CHECK_BIT(line->bits, IS_H1))
585                     wattron(window, A_UNDERLINE);
586
587                 // skip hashes
588                 while(line->text->text[offset] == '#')
589                     offset = next_word(line->text, offset);
590
591                 // print whole lines
592                 wprintw(window,
593                         "%s", &line->text->text[offset]);
594
595                 wattroff(window, A_UNDERLINE);
596
597             // no line-wide markdown
598             } else {
599
600                 inline_display(window, &line->text->text[offset], colors);
601             }
602         }
603     }
604
605     // fill rest off line with spaces
606     for(i = getcurx(window) - x; i < max_cols; i++)
607         wprintw(window, "%s", " ");
608
609     // reset to default color
610     if(colors)
611         wattron(window, COLOR_PAIR(CP_WHITE));
612     wattroff(window, A_UNDERLINE);
613 }
614
615 void inline_display(WINDOW *window, const char *c, const int colors) {
616     const static char *special = "\\*_`!["; // list of interpreted chars
617     const char *i = c; // iterator
618     const char *start_link_name, *start_url;
619     int length_link_name, url_num;
620     cstack_t *stack = cstack_init();
621
622
623     // for each char in line
624     for(; *i; i++) {
625
626         // if char is in special char list
627         if(strchr(special, *i)) {
628
629             // closing special char (or second backslash)
630             // only if not followed by :alnum:
631             if((stack->top)(stack, *i) &&
632                (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
633
634                 switch(*i) {
635                     // print escaped backslash
636                     case '\\':
637                         wprintw(window, "%c", *i);
638                         break;
639                     // disable highlight
640                     case '*':
641                         if(colors)
642                             wattron(window, COLOR_PAIR(CP_WHITE));
643                         break;
644                     // disable underline
645                     case '_':
646                         wattroff(window, A_UNDERLINE);
647                         break;
648                     // disable inline code
649                     case '`':
650                         if(colors)
651                             wattron(window, COLOR_PAIR(CP_WHITE));
652                         break;
653                 }
654
655                 // remove top special char from stack
656                 (stack->pop)(stack);
657
658             // treat special as regular char
659             } else if((stack->top)(stack, '\\')) {
660                 wprintw(window, "%c", *i);
661
662                 // remove backslash from stack
663                 (stack->pop)(stack);
664
665             // opening special char
666             } else {
667
668                 // emphasis or code span can start after new-line or space only
669                 // and of cause after another emphasis markup
670                 //TODO this condition looks ugly
671                 if(i == c ||
672                    *(i - 1) == ' ' ||
673                    ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
674                    *i == '\\') {
675
676                     // url in pandoc style
677                     if ((*i == '[' && strchr(i, ']')) ||
678                         (*i == '!' && *(i + 1) == '[' && strchr(i, ']'))) {
679
680                         if (*i == '!') i++;
681
682                         if (strchr(i, ']')[1] == '(') {
683                             i++;
684
685                             // turn higlighting and underlining on
686                             if (colors)
687                                 wattron(window, COLOR_PAIR(CP_BLUE));
688                             wattron(window, A_UNDERLINE);
689
690                             start_link_name = i;
691
692                             // print the content of the label
693                             // the label is printed as is
694                             do {
695                                 wprintw(window, "%c", *i);
696                                 i++;
697                             } while (*i != ']');
698
699                             length_link_name = i - 1 - start_link_name;
700
701                             i++;
702                             i++;
703
704                             start_url = i;
705
706                             while (*i != ')') i++;
707
708                             url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
709
710                             wprintw(window, " [%d]", url_num);
711
712                             // turn highlighting and underlining off
713                             wattroff(window, A_UNDERLINE);
714                             wattron(window, COLOR_PAIR(CP_WHITE));
715
716                         } else {
717                             wprintw(window, "[");
718                         }
719
720                     } else switch(*i) {
721                         // enable highlight
722                         case '*':
723                             if(colors)
724                                 wattron(window, COLOR_PAIR(CP_RED));
725                             break;
726                         // enable underline
727                         case '_':
728                             wattron(window, A_UNDERLINE);
729                             break;
730                         // enable inline code
731                         case '`':
732                             if(colors)
733                                 wattron(window, COLOR_PAIR(CP_BLACK));
734                             break;
735                         // do nothing for backslashes
736                     }
737
738                     // push special char to stack
739                     (stack->push)(stack, *i);
740
741                 } else {
742                     wprintw(window, "%c", *i);
743                 }
744             }
745
746         } else {
747             // remove backslash from stack
748             if((stack->top)(stack, '\\'))
749                 (stack->pop)(stack);
750
751             // print regular char
752             wprintw(window, "%c", *i);
753         }
754     }
755
756     // pop stack until empty to prevent formated trailing spaces
757     while(!(stack->empty)(stack)) {
758         switch((stack->pop)(stack)) {
759             // disable highlight
760             case '*':
761                 if(colors)
762                     wattron(window, COLOR_PAIR(CP_WHITE));
763                 break;
764             // disable underline
765             case '_':
766                 wattroff(window, A_UNDERLINE);
767                 break;
768             // disable inline code
769             case '`':
770                 if(colors)
771                     wattron(window, COLOR_PAIR(CP_WHITE));
772                 break;
773             // do nothing for backslashes
774         }
775     }
776
777     (stack->delete)(stack);
778 }
779
780 void fade_out(WINDOW *window, int trans, int colors, int invert) {
781     int i; // increment
782     if(colors && COLORS == 256) {
783         for(i = 22; i >= 0; i--) {
784
785             // dim color pairs
786             if(invert) {
787                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
788                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
789                 init_pair(CP_RED, red_ramp_invert[i], trans);
790                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
791             } else {
792                 init_pair(CP_WHITE, white_ramp[i], trans);
793                 init_pair(CP_BLUE, blue_ramp[i], trans);
794                 init_pair(CP_RED, red_ramp[i], trans);
795                 init_pair(CP_BLACK, 16, white_ramp[i]);
796             }
797
798             // refresh window with new color
799             wrefresh(window);
800
801             // delay for our eyes to recognize the change
802             usleep(FADE_DELAY);
803         }
804     }
805 }
806
807 void fade_in(WINDOW *window, int trans, int colors, int invert) {
808     int i; // increment
809     if(colors && COLORS == 256) {
810         for(i = 0; i <= 23; i++) {
811
812             // brighten color pairs
813             if(invert) {
814                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
815                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
816                 init_pair(CP_RED, red_ramp_invert[i], trans);
817                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
818             } else {
819                 init_pair(CP_WHITE, white_ramp[i], trans);
820                 init_pair(CP_BLUE, blue_ramp[i], trans);
821                 init_pair(CP_RED, red_ramp[i], trans);
822                 init_pair(CP_BLACK, 16, white_ramp[i]);
823             }
824
825             // refresh window with new color
826             wrefresh(window);
827
828             // delay for our eyes to recognize the change
829             usleep(FADE_DELAY);
830         }
831     }
832 }
833
834 int int_length (int val) {
835     int l = 1;
836     while(val > 9) {
837         l++;
838         val /= 10;
839     }
840     return l;
841 }