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