cleaner implementation of do_word_wrap

This commit is contained in:
Sascha Kruse 2012-08-18 21:45:33 +02:00
parent a9fec26148
commit 48c3df1134

38
dunst.c
View File

@ -365,28 +365,30 @@ void update_lists()
/* TODO get draw_txt_buf as argument */ /* TODO get draw_txt_buf as argument */
int do_word_wrap(char *source, int max_width) int do_word_wrap(char *source, int max_width)
{ {
char *last_space = NULL; char *eol = source;
char *cur = source; while (True) {
int lines = 1; if ( *eol == '\0')
return 1;
if (max_width < 1) if (textnw(dc, source, (eol - source) + 1) >= max_width) {
return 1; /* go back to previous space */
char *space = eol;
while (space > source && !isspace(*space))
space--;
while (*cur != '\0') { if (space <= source) {
if (isspace(*cur)) { /* whe have a word longer than width, so we
if (textnw(dc, source, cur - source) > max_width) { * split mid-word. That one letter is
*last_space = '\0'; * collateral damage */
lines++; space = eol;
cur = last_space + 1;
source = cur;
continue;
} else {
last_space = cur;
} }
*space = '\0';
if (*(space + 1) == '\0')
return 1;
return 1 + do_word_wrap(space+1, max_width);
} }
cur++; eol++;
} }
return lines;
} }
void update_draw_txt_buf(notification * n, int max_width) void update_draw_txt_buf(notification * n, int max_width)
@ -581,7 +583,7 @@ void draw_win(void)
dc->x = calculate_x_offset(width, textw(dc, line)); dc->x = calculate_x_offset(width, textw(dc, line));
dc->y += (line_height - font_h) / 2; dc->y += (line_height - font_h) / 2;
drawtext(dc, line, n->colors); drawtextn(dc, line, strlen(line), n->colors);
dc->y += line_height - ((line_height - font_h) / 2); dc->y += line_height - ((line_height - font_h) / 2);
} }
} }