X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=src%2Fviewer.c;h=162477f891cadd9288dadfa68e8586c30610a906;hb=5ee2b6a0c18b0772a1124938f4ccff1123a56b89;hp=ee6fc905df3ae364c0e7d59052dfa0a2c0e9fe97;hpb=a1d883fc5b7acbf45fdc80a7806b4d4c21fdcd1f;p=smdp.git diff --git a/src/viewer.c b/src/viewer.c index ee6fc90..162477f 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -65,6 +65,7 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { int c = 0; // char int i = 0; // iterate int l = 0; // line number + int lc = 0; // line count int sc = 1; // slide count int colors = 0; // amount of colors supported int fade = 0; // disable color fading by default @@ -82,39 +83,71 @@ int ncurses_display(deck_t *deck, int notrans, int nofade, int invert) { slide_t *slide = deck->slide; line_t *line; + // set locale to display UTF-8 correctly in ncurses + setlocale(LC_CTYPE, ""); + + // init ncurses + initscr(); + while(slide) { - // set max_lines if line count exceeded - max_lines = (slide->lines > max_lines) ? slide->lines : max_lines; + lc = 0; line = slide->line; + while(line) { - // set max_cols if length exceeded - max_cols = (line->length > max_cols) ? line->length : max_cols; + if(line->length > COLS) { + i = line->length; + offset = 0; + while(i > COLS) { + + i = prev_blank(line->text, offset + COLS) - offset; + + // single word is > COLS + if(!i) { + // calculate min_width + i = next_blank(line->text, offset + COLS) - offset; + + // disable ncurses + endwin(); + + // print error + fprintf(stderr, "Error: Terminal width (%i columns) too small. Need at least %i columns.\n", COLS, i); + fprintf(stderr, "You may need to shorten some lines by inserting line breaks.\n"); + + return(1); + } + + // set max_cols + max_cols = (i > max_cols) ? i : max_cols; + + // iterate to next line + offset = prev_blank(line->text, offset + COLS); + i = line->length - offset; + lc++; + } + // set max_cols one last time + max_cols = (i > max_cols) ? i : max_cols; + } else { + // set max_cols + max_cols = (line->length > max_cols) ? line->length : max_cols; + } + lc++; line = line->next; } - slide = slide->next; - } - // set locale to display UTF-8 correctly in ncurses - setlocale(LC_CTYPE, ""); + max_lines = (lc > max_lines) ? lc : max_lines; - // init ncurses - initscr(); + slide = slide->next; + } - if((max_cols > COLS) || - (max_lines + bar_top + bar_bottom + 2 > LINES)) { + // not enough lines + if(max_lines + bar_top + bar_bottom > LINES) { // disable ncurses endwin(); // print error - fprintf(stderr, "Error: Terminal size %ix%i too small. Need at least %ix%i.\n", - COLS, LINES, max_cols, max_lines + bar_top + bar_bottom + 2); - - // print hint to solve it - if(max_lines + bar_top + bar_bottom + 2 > LINES) - fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n"); - if(max_cols > COLS) - fprintf(stderr, "Automatic line wrapping is not supported yet. You may need to shorten some lines by inserting line breaks.\n"); + fprintf(stderr, "Error: Terminal heigth (%i lines) too small. Need at least %i lines.\n", LINES, max_lines + bar_top + bar_bottom); + fprintf(stderr, "You may need to add additional horizontal rules ('***') to split your file in shorter slides.\n"); return(1); }