replace pseudo UTF-8 with real wide character support
[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 <wchar.h>  // wcschr
26 #include <wctype.h> // iswalnum
27 #include <string.h> // strcpy
28 #include <unistd.h> // usleep
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     // init ncurses
87     initscr();
88
89     while(slide) {
90         lc = 0;
91         line = slide->line;
92
93         while(line && line->text) {
94
95             if (line->text->text)
96                 lc += url_count_inline(line->text->text);
97
98             if (line->text->text)
99                 line->length -= url_len_inline(line->text->text);
100
101             if(line->length > COLS) {
102                 i = line->length;
103                 offset = 0;
104                 while(i > COLS) {
105
106                     i = prev_blank(line->text, offset + COLS) - offset;
107
108                     // single word is > COLS
109                     if(!i) {
110                         // calculate min_width
111                         i = next_blank(line->text, offset + COLS) - offset;
112
113                         // disable ncurses
114                         endwin();
115
116                         // print error
117                         fwprintf(stderr, L"Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i);
118                         fwprintf(stderr, L"You may need to shorten some lines by inserting line breaks.\n");
119
120                         // no reload
121                         return 0;
122                     }
123
124                     // set max_cols
125                     max_cols = MAX(i, max_cols);
126
127                     // iterate to next line
128                     offset = prev_blank(line->text, offset + COLS);
129                     i = line->length - offset;
130                     lc++;
131                 }
132                 // set max_cols one last time
133                 max_cols = MAX(i, max_cols);
134             } else {
135                 // set max_cols
136                 max_cols = MAX(line->length, max_cols);
137             }
138             lc++;
139             line = line->next;
140         }
141
142         max_lines = MAX(lc, max_lines);
143
144         slide = slide->next;
145     }
146
147     // not enough lines
148     if(max_lines + bar_top + bar_bottom > LINES) {
149
150         // disable ncurses
151         endwin();
152
153         // print error
154         fwprintf(stderr, L"Error: Terminal height (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom);
155         fwprintf(stderr, L"You may need to add additional horizontal rules (---) to split your file in shorter slides.\n");
156
157         // no reload
158         return 0;
159     }
160
161     // disable cursor
162     curs_set(0);
163
164     // disable output of keyboard typing
165     noecho();
166
167     // make getch() process one char at a time
168     cbreak();
169
170     // enable arrow keys
171     keypad(stdscr,TRUE);
172
173     // set colors
174     if(has_colors() == TRUE) {
175         start_color();
176         use_default_colors();
177
178         // 256 color mode
179         if(COLORS == 256) {
180
181             if(notrans) {
182                 if(invert) {
183                     trans = 15; // white in 256 color mode
184                 } else {
185                     trans = 16; // black in 256 color mode
186                 }
187             }
188
189             if(invert) {
190                 init_pair(CP_WHITE, 232, trans);
191                 init_pair(CP_BLUE, 21, trans);
192                 init_pair(CP_RED, 196, trans);
193                 init_pair(CP_BLACK, 15, 232);
194             } else {
195                 init_pair(CP_WHITE, 255, trans);
196                 init_pair(CP_BLUE, 123, trans);
197                 init_pair(CP_RED, 213, trans);
198                 init_pair(CP_BLACK, 16, 255);
199             }
200             init_pair(CP_YELLOW, 208, trans);
201
202             // enable color fading
203             if(!nofade)
204                 fade = true;
205
206         // 8 color mode
207         } else {
208
209             if(notrans) {
210                 if(invert) {
211                     trans = 7; // white in 8 color mode
212                 } else {
213                     trans = 0; // black in 8 color mode
214                 }
215             }
216
217             if(invert) {
218                 init_pair(CP_WHITE, 0, trans);
219                 init_pair(CP_BLACK, 7, 0);
220             } else {
221                 init_pair(CP_WHITE, 7, trans);
222                 init_pair(CP_BLACK, 0, 7);
223             }
224             init_pair(CP_BLUE, 4, trans);
225             init_pair(CP_RED, 1, trans);
226             init_pair(CP_YELLOW, 3, trans);
227         }
228
229         colors = 1;
230     }
231
232     // set background color of main window
233     if(colors)
234         wbkgd(stdscr, COLOR_PAIR(CP_YELLOW));
235
236     // setup main window
237     WINDOW *content = newwin(LINES - bar_top - bar_bottom, COLS, 0 + bar_top, 0);
238     if(colors)
239         wbkgd(content, COLOR_PAIR(CP_WHITE));
240
241     slide = deck->slide;
242
243     // find slide to reload
244     while(reload > 1 && reload <= deck->slides) {
245         slide = slide->next;
246         sc++;
247         reload--;
248     }
249
250     // reset reload indicator
251     reload = 0;
252
253     while(slide) {
254
255         url_init();
256
257         // clear windows
258         werase(content);
259         werase(stdscr);
260
261         // always resize window in case terminal geometry has changed
262         wresize(content, LINES - bar_top - bar_bottom, COLS);
263
264         // setup header
265         if(bar_top) {
266             line = deck->header;
267             offset = next_blank(line->text, 0) + 1;
268             // add text to header
269             mvwaddwstr(stdscr,
270                        0, (COLS - line->length + offset) / 2,
271                        &line->text->text[offset]);
272         }
273
274         // setup footer
275         if(deck->headers > 1) {
276             line = deck->header->next;
277             offset = next_blank(line->text, 0) + 1;
278             // add text to left footer
279             mvwaddwstr(stdscr,
280                        LINES - 1, 3,
281                        &line->text->text[offset]);
282         }
283
284         // add slide number to right footer
285         mvwprintw(stdscr,
286                   LINES - 1, COLS - int_length(deck->slides) - int_length(sc) - 6,
287                   "%d / %d", sc, deck->slides);
288
289         // make header + fooder visible
290         wrefresh(content);
291         wrefresh(stdscr);
292
293         line = slide->line;
294         l = 0;
295
296         // print lines
297         while(line) {
298             add_line(content, l, (COLS - max_cols) / 2, line, max_cols, colors);
299             l += (line->length / COLS) + 1;
300             line = line->next;
301         }
302
303         int i, ymax;
304         getmaxyx( content, ymax, i );
305         for (i = 0; i < url_get_amount(); i++) {
306             mvwprintw(content, ymax - url_get_amount() - 1 + i, 3,
307                       "[%d] ", i);
308             waddwstr(content, url_get_target(i));
309         }
310
311         // make content visible
312         wrefresh(content);
313
314         // fade in
315         if(fade)
316             fade_in(content, trans, colors, invert);
317
318         // re-enable fading after any undefined key press
319         if(COLORS == 256 && !nofade)
320             fade = true;
321
322         // wait for user input
323         c = getch();
324
325         // evaluate user input
326         i = 0;
327         switch(c) {
328
329             // show previous slide
330             case KEY_UP:
331             case KEY_LEFT:
332             case KEY_PPAGE:
333             case 8:   // BACKSPACE (ascii)
334             case 127: // BACKSPACE (xterm)
335             case 263: // BACKSPACE (getty)
336             case 'h':
337             case 'k':
338                 if(slide->prev) {
339                     slide = slide->prev;
340                     sc--;
341                 } else {
342                     fade = false;
343                 }
344                 break;
345
346             // show next slide
347             case KEY_DOWN:
348             case KEY_RIGHT:
349             case KEY_NPAGE:
350             case '\n': // ENTER
351             case ' ':  // SPACE
352             case 'j':
353             case 'l':
354                 if(slide->next) {
355                     slide = slide->next;
356                     sc++;
357                 } else {
358                     fade = false;
359                 }
360                 break;
361
362             // show slide n
363             case '9':
364             case '8':
365             case '7':
366             case '6':
367             case '5':
368             case '4':
369             case '3':
370             case '2':
371             case '1':
372                 i = get_slide_number(c);
373                 if(i > 0 && 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 'g':
397             case KEY_HOME:
398                 slide = deck->slide;
399                 sc = 1;
400                 break;
401
402             // show last slide
403             case 'G':
404             case KEY_END:
405                 for(i = sc; i <= deck->slides; i++) {
406                     if(slide->next) {
407                             slide = slide->next;
408                             sc++;
409                     }
410                 }
411                 break;
412
413             // reload
414             case 'r':
415                 if(noreload == 0) {
416                     // reload slide N
417                     reload = sc;
418                     slide = NULL;
419                 } else {
420                     // disable fading if reload is not possible
421                     fade = false;
422                 }
423                 break;
424
425             // quit
426             case 'q':
427                 // do not fade out on exit
428                 fade = false;
429                 // do not reload
430                 reload = 0;
431                 slide = NULL;
432                 break;
433
434             default:
435                 // disable fading on undefined key press
436                 fade = false;
437                 break;
438         }
439
440         // fade out
441         if(fade)
442             fade_out(content, trans, colors, invert);
443
444         url_purge();
445     }
446
447     // disable ncurses
448     endwin();
449
450     // free ncurses memory
451     delwin(content);
452     if(reload == 0)
453         delwin(stdscr);
454
455     // return reload indicator (0 means no reload)
456     return reload;
457 }
458
459 void add_line(WINDOW *window, int y, int x, line_t *line, int max_cols, int colors) {
460
461     if(!line->text->text) {
462         return;
463     }
464
465     int i; // increment
466     int offset = 0; // text offset
467
468     // move the cursor in position
469     wmove(window, y, x);
470
471     // IS_UNORDERED_LIST_3
472     if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_3)) {
473         offset = next_nonblank(line->text, 0);
474         char prompt[13];
475         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
476         strcpy(&prompt[4], CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
477
478         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
479             strcpy(&prompt[8], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_3)? " |  " : "    ");
480         } else {
481             strcpy(&prompt[8], " +- ");
482             offset += 2;
483         }
484
485         wprintw(window,
486                 "%s", prompt);
487
488         if(!CHECK_BIT(line->bits, IS_CODE))
489             inline_display(window, &line->text->text[offset], colors);
490
491     // IS_UNORDERED_LIST_2
492     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_2)) {
493         offset = next_nonblank(line->text, 0);
494         char prompt[9];
495         strcpy(&prompt[0], CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
496
497         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
498             strcpy(&prompt[4], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_2)? " |  " : "    ");
499         } else {
500             strcpy(&prompt[4], " +- ");
501             offset += 2;
502         }
503
504         wprintw(window,
505                 "%s", prompt);
506
507         if(!CHECK_BIT(line->bits, IS_CODE))
508             inline_display(window, &line->text->text[offset], colors);
509
510     // IS_UNORDERED_LIST_1
511     } else if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_1)) {
512         offset = next_nonblank(line->text, 0);
513         char prompt[5];
514
515         if(CHECK_BIT(line->bits, IS_UNORDERED_LIST_EXT)) {
516             strcpy(&prompt[0], line->next && CHECK_BIT(line->next->bits, IS_UNORDERED_LIST_1)? " |  " : "    ");
517         } else {
518             strcpy(&prompt[0], " +- ");
519             offset += 2;
520         }
521
522         wprintw(window,
523                 "%s", prompt);
524
525         if(!CHECK_BIT(line->bits, IS_CODE))
526             inline_display(window, &line->text->text[offset], colors);
527     }
528
529     // IS_CODE
530     if(CHECK_BIT(line->bits, IS_CODE)) {
531
532         // set static offset for code
533         offset = CODE_INDENT;
534
535         // reverse color for code blocks
536         if(colors)
537             wattron(window, COLOR_PAIR(CP_BLACK));
538
539         // print whole lines
540         waddwstr(window, &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                 waddwstr(window, &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 wchar_t *c, const int colors) {
616     const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
617     const wchar_t *i = c; // iterator
618     const wchar_t *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(wcschr(special, *i)) {
628
629             // closing special char (or second backslash)
630             // only if not followed by :alnum:
631             if((stack->top)(stack, *i) &&
632                (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
633
634                 switch(*i) {
635                     // print escaped backslash
636                     case L'\\':
637                         waddnwstr(window, i, 1);
638                         break;
639                     // disable highlight
640                     case L'*':
641                         if(colors)
642                             wattron(window, COLOR_PAIR(CP_WHITE));
643                         break;
644                     // disable underline
645                     case L'_':
646                         wattroff(window, A_UNDERLINE);
647                         break;
648                     // disable inline code
649                     case L'`':
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, L'\\')) {
660                 waddnwstr(window, i, 1);
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                    iswspace(*(i - 1)) ||
673                    ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
674                     ((i - 1) == c || iswspace(*(i - 2)))) ||
675                    *i == L'\\') {
676
677                     // url in pandoc style
678                     if ((*i == L'[' && wcschr(i, L']')) ||
679                         (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
680
681                         if (*i == L'!') i++;
682
683                         if (wcschr(i, L']')[1] == L'(') {
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                                 waddnwstr(window, i, 1);
697                                 i++;
698                             } while (*i != L']');
699
700                             length_link_name = i - 1 - start_link_name;
701
702                             i++;
703                             i++;
704
705                             start_url = i;
706
707                             while (*i != L')') 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 L'*':
724                             if(colors)
725                                 wattron(window, COLOR_PAIR(CP_RED));
726                             break;
727                         // enable underline
728                         case L'_':
729                             wattron(window, A_UNDERLINE);
730                             break;
731                         // enable inline code
732                         case L'`':
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                     waddnwstr(window, i, 1);
744                 }
745             }
746
747         } else {
748             // remove backslash from stack
749             if((stack->top)(stack, L'\\'))
750                 (stack->pop)(stack);
751
752             // print regular char
753             waddnwstr(window, i, 1);
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 L'*':
762                 if(colors)
763                     wattron(window, COLOR_PAIR(CP_WHITE));
764                 break;
765             // disable underline
766             case L'_':
767                 wattroff(window, A_UNDERLINE);
768                 break;
769             // disable inline code
770             case L'`':
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 }