Navigate to any slide number
[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':
365             case '8':
366             case '7':
367             case '6':
368             case '5':
369             case '4':
370             case '3':
371             case '2':
372             case '1':
373                 i = get_slide_number(c);
374                 if(i > 0 && i <= deck->slides) {
375                     while(sc != i) {
376                         // search forward
377                         if(sc < i) {
378                             if(slide->next) {
379                                 slide = slide->next;
380                                 sc++;
381                             }
382                         // search backward
383                         } else {
384                             if(slide->prev) {
385                                 slide = slide->prev;
386                                 sc--;
387                             }
388                         }
389                     }
390                 } else {
391                     // disable fading if slide n doesn't exist
392                     fade = false;
393                 }
394                 break;
395
396             // show first slide
397             case KEY_HOME:
398                 slide = deck->slide;
399                 sc = 1;
400                 break;
401
402             // show last slide
403             case KEY_END:
404                 for(i = sc; i <= deck->slides; i++) {
405                     if(slide->next) {
406                             slide = slide->next;
407                             sc++;
408                     }
409                 }
410                 break;
411
412             // reload
413             case 'r':
414                 if(noreload == 0) {
415                     // reload slide N
416                     reload = sc;
417                     slide = NULL;
418                 } else {
419                     // disable fading if reload is not possible
420                     fade = false;
421                 }
422                 break;
423
424             // quit
425             case 'q':
426                 // do not fade out on exit
427                 fade = false;
428                 // do not reload
429                 reload = 0;
430                 slide = NULL;
431                 break;
432
433             default:
434                 // disable fading on undefined key press
435                 fade = false;
436                 break;
437         }
438
439         // fade out
440         if(fade)
441             fade_out(content, trans, colors, invert);
442
443         url_purge();
444     }
445
446     // disable ncurses
447     endwin();
448
449     // free ncurses memory
450     delwin(content);
451     if(reload == 0)
452         delwin(stdscr);
453
454     // return reload indicator (0 means no reload)
455     return reload;
456 }
457
458 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
459
460     if(!line->text->text) {
461         return;
462     }
463
464     int i; // increment
465     int offset = 0; // text offset
466
467     // move the cursor in position
468     wmove(window, y, x);
469
470     // IS_UNORDERED_LIST_3
471     if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
472         offset = next_nonblank(line->text, 0);
473         char prompt[13];
474         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
475         strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
476
477         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
478             strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " |  " : "    ");
479         } else {
480             strcpy(&prompt[8], " +- ");
481             offset += 2;
482         }
483
484         wprintw(window,
485                 "%s", prompt);
486
487         if(!CHECK_BIT(line->bits, IS_CODE))
488             inline_display(window, &line->text->text[offset], colors);
489
490     // IS_UNORDERED_LIST_2
491     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
492         offset = next_nonblank(line->text, 0);
493         char prompt[9];
494         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
495
496         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
497             strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
498         } else {
499             strcpy(&prompt[4], " +- ");
500             offset += 2;
501         }
502
503         wprintw(window,
504                 "%s", prompt);
505
506         if(!CHECK_BIT(line->bits, IS_CODE))
507             inline_display(window, &line->text->text[offset], colors);
508
509     // IS_UNORDERED_LIST_1
510     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
511         offset = next_nonblank(line->text, 0);
512         char prompt[5];
513
514         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
515             strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
516         } else {
517             strcpy(&prompt[0], " +- ");
518             offset += 2;
519         }
520
521         wprintw(window,
522                 "%s", prompt);
523
524         if(!CHECK_BIT(line->bits, IS_CODE))
525             inline_display(window, &line->text->text[offset], colors);
526     }
527
528     // IS_CODE
529     if(CHECK_BIT(line->bits, IS_CODE)) {
530
531         // set static offset for code
532         offset = CODE_INDENT;
533
534         // reverse color for code blocks
535         if(colors)
536             wattron(window, COLOR_PAIR(CP_BLACK));
537
538         // print whole lines
539         wprintw(window,
540                 "%s", &line->text->text[offset]);
541     }
542
543     if(!CHECK_BIT(line->bits, IS_UNORDERED_LIST_1) &&
544        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_2) &&
545        !CHECK_BIT(line->bits, IS_UNORDERED_LIST_3) &&
546        !CHECK_BIT(line->bits, IS_CODE)) {
547
548         // IS_QUOTE
549         if(CHECK_BIT(line->bits, IS_QUOTE)) {
550             while(line->text->text[offset] == '>') {
551                 // print a reverse color block
552                 if(colors) {
553                     wattron(window, COLOR_PAIR(CP_BLACK));
554                     wprintw(window, "%s", " ");
555                     wattron(window, COLOR_PAIR(CP_WHITE));
556                     wprintw(window, "%s", " ");
557                 } else {
558                     wprintw(window, "%s", ">");
559                 }
560
561                 // find next quote or break
562                 offset++;
563                 if(line->text->text[offset] == ' ')
564                     offset = next_word(line->text, offset);
565             }
566
567             inline_display(window, &line->text->text[offset], colors);
568         } else {
569
570             // IS_CENTER
571             if(CHECK_BIT(line->bits, IS_CENTER)) {
572                 if(line->length < max_cols) {
573                     wmove(window, y, x + ((max_cols - line->length) / 2));
574                 }
575             }
576
577             // IS_H1 || IS_H2
578             if(CHECK_BIT(line->bits, IS_H1) || CHECK_BIT(line->bits, IS_H2)) {
579
580                 // set headline color
581                 if(colors)
582                     wattron(window, COLOR_PAIR(CP_BLUE));
583
584                 // enable underline for H1
585                 if(CHECK_BIT(line->bits, IS_H1))
586                     wattron(window, A_UNDERLINE);
587
588                 // skip hashes
589                 while(line->text->text[offset] == '#')
590                     offset = next_word(line->text, offset);
591
592                 // print whole lines
593                 wprintw(window,
594                         "%s", &line->text->text[offset]);
595
596                 wattroff(window, A_UNDERLINE);
597
598             // no line-wide markdown
599             } else {
600
601                 inline_display(window, &line->text->text[offset], colors);
602             }
603         }
604     }
605
606     // fill rest off line with spaces
607     for(i = getcurx(window) - x; i < max_cols; i++)
608         wprintw(window, "%s", " ");
609
610     // reset to default color
611     if(colors)
612         wattron(window, COLOR_PAIR(CP_WHITE));
613     wattroff(window, A_UNDERLINE);
614 }
615
616 void inline_display(WINDOW *window, const char *c, const int colors) {
617     const static char *special = "\\*_`!["; // list of interpreted chars
618     const char *i = c; // iterator
619     const char *start_link_name, *start_url;
620     int length_link_name, url_num;
621     cstack_t *stack = cstack_init();
622
623
624     // for each char in line
625     for(; *i; i++) {
626
627         // if char is in special char list
628         if(strchr(special, *i)) {
629
630             // closing special char (or second backslash)
631             // only if not followed by :alnum:
632             if((stack->top)(stack, *i) &&
633                (!isalnum((int)i[1]) || *(i + 1) == '\0' || *i == '\\')) {
634
635                 switch(*i) {
636                     // print escaped backslash
637                     case '\\':
638                         wprintw(window, "%c", *i);
639                         break;
640                     // disable highlight
641                     case '*':
642                         if(colors)
643                             wattron(window, COLOR_PAIR(CP_WHITE));
644                         break;
645                     // disable underline
646                     case '_':
647                         wattroff(window, A_UNDERLINE);
648                         break;
649                     // disable inline code
650                     case '`':
651                         if(colors)
652                             wattron(window, COLOR_PAIR(CP_WHITE));
653                         break;
654                 }
655
656                 // remove top special char from stack
657                 (stack->pop)(stack);
658
659             // treat special as regular char
660             } else if((stack->top)(stack, '\\')) {
661                 wprintw(window, "%c", *i);
662
663                 // remove backslash from stack
664                 (stack->pop)(stack);
665
666             // opening special char
667             } else {
668
669                 // emphasis or code span can start after new-line or space only
670                 // and of cause after another emphasis markup
671                 //TODO this condition looks ugly
672                 if(i == c ||
673                    *(i - 1) == ' ' ||
674                    ((*(i - 1) == '_' || *(i - 1) == '*') && ((i - 1) == c || *(i - 2) == ' ')) ||
675                    *i == '\\') {
676
677                     // url in pandoc style
678                     if ((*i == '[' && strchr(i, ']')) ||
679                         (*i == '!' && *(i + 1) == '[' && strchr(i, ']'))) {
680
681                         if (*i == '!') i++;
682
683                         if (strchr(i, ']')[1] == '(') {
684                             i++;
685
686                             // turn higlighting and underlining on
687                             if (colors)
688                                 wattron(window, COLOR_PAIR(CP_BLUE));
689                             wattron(window, A_UNDERLINE);
690
691                             start_link_name = i;
692
693                             // print the content of the label
694                             // the label is printed as is
695                             do {
696                                 wprintw(window, "%c", *i);
697                                 i++;
698                             } while (*i != ']');
699
700                             length_link_name = i - 1 - start_link_name;
701
702                             i++;
703                             i++;
704
705                             start_url = i;
706
707                             while (*i != ')') i++;
708
709                             url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0,0);
710
711                             wprintw(window, " [%d]", url_num);
712
713                             // turn highlighting and underlining off
714                             wattroff(window, A_UNDERLINE);
715                             wattron(window, COLOR_PAIR(CP_WHITE));
716
717                         } else {
718                             wprintw(window, "[");
719                         }
720
721                     } else switch(*i) {
722                         // enable highlight
723                         case '*':
724                             if(colors)
725                                 wattron(window, COLOR_PAIR(CP_RED));
726                             break;
727                         // enable underline
728                         case '_':
729                             wattron(window, A_UNDERLINE);
730                             break;
731                         // enable inline code
732                         case '`':
733                             if(colors)
734                                 wattron(window, COLOR_PAIR(CP_BLACK));
735                             break;
736                         // do nothing for backslashes
737                     }
738
739                     // push special char to stack
740                     (stack->push)(stack, *i);
741
742                 } else {
743                     wprintw(window, "%c", *i);
744                 }
745             }
746
747         } else {
748             // remove backslash from stack
749             if((stack->top)(stack, '\\'))
750                 (stack->pop)(stack);
751
752             // print regular char
753             wprintw(window, "%c", *i);
754         }
755     }
756
757     // pop stack until empty to prevent formated trailing spaces
758     while(!(stack->empty)(stack)) {
759         switch((stack->pop)(stack)) {
760             // disable highlight
761             case '*':
762                 if(colors)
763                     wattron(window, COLOR_PAIR(CP_WHITE));
764                 break;
765             // disable underline
766             case '_':
767                 wattroff(window, A_UNDERLINE);
768                 break;
769             // disable inline code
770             case '`':
771                 if(colors)
772                     wattron(window, COLOR_PAIR(CP_WHITE));
773                 break;
774             // do nothing for backslashes
775         }
776     }
777
778     (stack->delete)(stack);
779 }
780
781 void fade_out(WINDOW *window, int trans, int colors, int invert) {
782     int i; // increment
783     if(colors && COLORS == 256) {
784         for(i = 22; i >= 0; i--) {
785
786             // dim color pairs
787             if(invert) {
788                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
789                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
790                 init_pair(CP_RED, red_ramp_invert[i], trans);
791                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
792             } else {
793                 init_pair(CP_WHITE, white_ramp[i], trans);
794                 init_pair(CP_BLUE, blue_ramp[i], trans);
795                 init_pair(CP_RED, red_ramp[i], trans);
796                 init_pair(CP_BLACK, 16, white_ramp[i]);
797             }
798
799             // refresh window with new color
800             wrefresh(window);
801
802             // delay for our eyes to recognize the change
803             usleep(FADE_DELAY);
804         }
805     }
806 }
807
808 void fade_in(WINDOW *window, int trans, int colors, int invert) {
809     int i; // increment
810     if(colors && COLORS == 256) {
811         for(i = 0; i <= 23; i++) {
812
813             // brighten color pairs
814             if(invert) {
815                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
816                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
817                 init_pair(CP_RED, red_ramp_invert[i], trans);
818                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
819             } else {
820                 init_pair(CP_WHITE, white_ramp[i], trans);
821                 init_pair(CP_BLUE, blue_ramp[i], trans);
822                 init_pair(CP_RED, red_ramp[i], trans);
823                 init_pair(CP_BLACK, 16, white_ramp[i]);
824             }
825
826             // refresh window with new color
827             wrefresh(window);
828
829             // delay for our eyes to recognize the change
830             usleep(FADE_DELAY);
831         }
832     }
833 }
834
835 int int_length (int val) {
836     int l = 1;
837     while(val > 9) {
838         l++;
839         val /= 10;
840     }
841     return l;
842 }
843
844 int get_slide_number(char init) {
845     int retval = init - '0';
846     char c;
847     // block for tenths of a second when using getch, ERR if no input
848     halfdelay(GOTO_SLIDE_DELAY);
849     while((c = getch()) != ERR) {
850         if (c < '0' || c > '9') {
851             retval = -1;
852             break;
853         }
854         retval = (retval * 10) + (c - '0');
855     }
856     nocbreak();     // cancel half delay mode
857     cbreak();       // go back to cbreak
858     return retval;
859 }