remove unnecessary blanks at the output
[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     // we only need this if the color is inverted (e.g. code-blocks),
637     // to ensure the background fades too
638     if(CHECK_BIT(line->bits, IS_CODE))
639         for(i = getcurx(window) - x; i < max_cols; i++)
640             wprintw(window, "%s", " ");
641
642     // reset to default color
643     if(colors)
644         wattron(window, COLOR_PAIR(CP_WHITE));
645     wattroff(window, A_UNDERLINE);
646 }
647
648 void inline_display(WINDOW *window, const wchar_t *c, const int colors) {
649     const static wchar_t *special = L"\\*_`!["; // list of interpreted chars
650     const wchar_t *i = c; // iterator
651     const wchar_t *start_link_name, *start_url;
652     int length_link_name, url_num;
653     cstack_t *stack = cstack_init();
654
655
656     // for each char in line
657     for(; *i; i++) {
658
659         // if char is in special char list
660         if(wcschr(special, *i)) {
661
662             // closing special char (or second backslash)
663             // only if not followed by :alnum:
664             if((stack->top)(stack, *i) &&
665                (!iswalnum(i[1]) || *(i + 1) == L'\0' || *i == L'\\')) {
666
667                 switch(*i) {
668                     // print escaped backslash
669                     case L'\\':
670                         waddnwstr(window, i, 1);
671                         break;
672                     // disable highlight
673                     case L'*':
674                         if(colors)
675                             wattron(window, COLOR_PAIR(CP_WHITE));
676                         break;
677                     // disable underline
678                     case L'_':
679                         wattroff(window, A_UNDERLINE);
680                         break;
681                     // disable inline code
682                     case L'`':
683                         if(colors)
684                             wattron(window, COLOR_PAIR(CP_WHITE));
685                         break;
686                 }
687
688                 // remove top special char from stack
689                 (stack->pop)(stack);
690
691             // treat special as regular char
692             } else if((stack->top)(stack, L'\\')) {
693                 waddnwstr(window, i, 1);
694
695                 // remove backslash from stack
696                 (stack->pop)(stack);
697
698             // opening special char
699             } else {
700
701                 // emphasis or code span can start after new-line or space only
702                 // and of cause after another emphasis markup
703                 //TODO this condition looks ugly
704                 if(i == c ||
705                    iswspace(*(i - 1)) ||
706                    ((iswspace(*(i - 1)) || *(i - 1) == L'*' || *(i - 1) == L'_') &&
707                     ((i - 1) == c || iswspace(*(i - 2)))) ||
708                    *i == L'\\') {
709
710                     // url in pandoc style
711                     if ((*i == L'[' && wcschr(i, L']')) ||
712                         (*i == L'!' && *(i + 1) == L'[' && wcschr(i, L']'))) {
713
714                         if (*i == L'!') i++;
715
716                         if (wcschr(i, L']')[1] == L'(') {
717                             i++;
718
719                             // turn higlighting and underlining on
720                             if (colors)
721                                 wattron(window, COLOR_PAIR(CP_BLUE));
722                             wattron(window, A_UNDERLINE);
723
724                             start_link_name = i;
725
726                             // print the content of the label
727                             // the label is printed as is
728                             do {
729                                 waddnwstr(window, i, 1);
730                                 i++;
731                             } while (*i != L']');
732
733                             length_link_name = i - 1 - start_link_name;
734
735                             i++;
736                             i++;
737
738                             start_url = i;
739
740                             while (*i != L')') i++;
741
742                             url_num = url_add(start_link_name, length_link_name, start_url, i - start_url, 0, 0);
743
744                             wprintw(window, " [%d]", url_num);
745
746                             // turn highlighting and underlining off
747                             wattroff(window, A_UNDERLINE);
748                             wattron(window, COLOR_PAIR(CP_WHITE));
749
750                         } else {
751                             wprintw(window, "[");
752                         }
753
754                     } else switch(*i) {
755                         // enable highlight
756                         case L'*':
757                             if(colors)
758                                 wattron(window, COLOR_PAIR(CP_RED));
759                             break;
760                         // enable underline
761                         case L'_':
762                             wattron(window, A_UNDERLINE);
763                             break;
764                         // enable inline code
765                         case L'`':
766                             if(colors)
767                                 wattron(window, COLOR_PAIR(CP_BLACK));
768                             break;
769                         // do nothing for backslashes
770                     }
771
772                     // push special char to stack
773                     (stack->push)(stack, *i);
774
775                 } else {
776                     waddnwstr(window, i, 1);
777                 }
778             }
779
780         } else {
781             // remove backslash from stack
782             if((stack->top)(stack, L'\\'))
783                 (stack->pop)(stack);
784
785             // print regular char
786             waddnwstr(window, i, 1);
787         }
788     }
789
790     // pop stack until empty to prevent formated trailing spaces
791     while(!(stack->empty)(stack)) {
792         switch((stack->pop)(stack)) {
793             // disable highlight
794             case L'*':
795                 if(colors)
796                     wattron(window, COLOR_PAIR(CP_WHITE));
797                 break;
798             // disable underline
799             case L'_':
800                 wattroff(window, A_UNDERLINE);
801                 break;
802             // disable inline code
803             case L'`':
804                 if(colors)
805                     wattron(window, COLOR_PAIR(CP_WHITE));
806                 break;
807             // do nothing for backslashes
808         }
809     }
810
811     (stack->delete)(stack);
812 }
813
814 void fade_out(WINDOW *window, int trans, int colors, int invert) {
815     int i; // increment
816     if(colors && COLORS == 256) {
817         for(i = 22; i >= 0; i--) {
818
819             // dim color pairs
820             if(invert) {
821                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
822                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
823                 init_pair(CP_RED, red_ramp_invert[i], trans);
824                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
825             } else {
826                 init_pair(CP_WHITE, white_ramp[i], trans);
827                 init_pair(CP_BLUE, blue_ramp[i], trans);
828                 init_pair(CP_RED, red_ramp[i], trans);
829                 init_pair(CP_BLACK, 16, white_ramp[i]);
830             }
831
832             // refresh window with new color
833             wrefresh(window);
834
835             // delay for our eyes to recognize the change
836             usleep(FADE_DELAY);
837         }
838     }
839 }
840
841 void fade_in(WINDOW *window, int trans, int colors, int invert) {
842     int i; // increment
843     if(colors && COLORS == 256) {
844         for(i = 0; i <= 23; i++) {
845
846             // brighten color pairs
847             if(invert) {
848                 init_pair(CP_WHITE, white_ramp_invert[i], trans);
849                 init_pair(CP_BLUE, blue_ramp_invert[i], trans);
850                 init_pair(CP_RED, red_ramp_invert[i], trans);
851                 init_pair(CP_BLACK, 15, white_ramp_invert[i]);
852             } else {
853                 init_pair(CP_WHITE, white_ramp[i], trans);
854                 init_pair(CP_BLUE, blue_ramp[i], trans);
855                 init_pair(CP_RED, red_ramp[i], trans);
856                 init_pair(CP_BLACK, 16, white_ramp[i]);
857             }
858
859             // refresh window with new color
860             wrefresh(window);
861
862             // delay for our eyes to recognize the change
863             usleep(FADE_DELAY);
864         }
865     }
866 }
867
868 int int_length (int val) {
869     int l = 1;
870     while(val > 9) {
871         l++;
872         val /= 10;
873     }
874     return l;
875 }
876
877 int get_slide_number(char init) {
878     int retval = init - '0';
879     char c;
880     // block for tenths of a second when using getch, ERR if no input
881     halfdelay(GOTO_SLIDE_DELAY);
882     while((c = getch()) != ERR) {
883         if (c < '0' || c > '9') {
884             retval = -1;
885             break;
886         }
887         retval = (retval * 10) + (c - '0');
888     }
889     nocbreak();     // cancel half delay mode
890     cbreak();       // go back to cbreak
891     return retval;
892 }