From: Michael Göhler Date: Fri, 26 Sep 2014 16:33:57 +0000 (+0200) Subject: rework max_lines/cols detection to support line wrapping (#15 part 1) X-Git-Url: https://git.danieliu.xyz/?p=smdp.git;a=commitdiff_plain;h=c6516c715699de465423de60089efee1ec5857e1 rework max_lines/cols detection to support line wrapping (#15 part 1) --- diff --git a/include/parser.h b/include/parser.h index 1a113e0..4eb9dfe 100644 --- a/include/parser.h +++ b/include/parser.h @@ -48,6 +48,7 @@ void markdown_debug(deck_t *deck, int debug); int is_utf8(char ch); int length_utf8(char ch); int next_nonblank(cstring_t *text, int i); +int prev_blank(cstring_t *text, int i); int next_blank(cstring_t *text, int i); int next_word(cstring_t *text, int i); diff --git a/src/parser.c b/src/parser.c index c04facc..c8120cd 100644 --- a/src/parser.c +++ b/src/parser.c @@ -501,6 +501,13 @@ int next_nonblank(cstring_t *text, int i) { return i; } +int prev_blank(cstring_t *text, int i) { + while ((i > 0) && !isspace((unsigned char) (text->text)[i])) + i--; + + return i; +} + int next_blank(cstring_t *text, int i) { while ((i < text->size) && !isspace((unsigned char) (text->text)[i])) i++; 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); }