Skip to content

Commit

Permalink
rework max_lines/cols detection to support line wrapping (#15 part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Göhler committed Sep 27, 2014
1 parent a1d883f commit c6516c7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 20 deletions.
1 change: 1 addition & 0 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
73 changes: 53 additions & 20 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down

0 comments on commit c6516c7

Please sign in to comment.