herbe

[fork] notifications
Log | Files | Refs | README | LICENSE

commit 6a655a7cbd0a0d8e0e6a89e6c82b7787ab27dd59
parent 5035afe94f5e50d30249781fb6ab686f72e365d5
Author: Samuel Dudik <samuel.dudik@gmail.com>
Date:   Fri, 31 Jul 2020 18:58:35 +0200

Text now respects right padding

Diffstat:
Mconfig.h | 2+-
Mherbe.c | 32++++++++++++++++++++++++++++++--
2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/config.h b/config.h @@ -2,7 +2,7 @@ const static char *background_color = "#3e3e3e"; const static char *border_color = "#ececec"; const static char *font_color = "#ececec"; const static char *font_pattern = "Inconsolata:style=Medium:size=15"; -const static unsigned int padding = 20; +const static unsigned int padding = 15; const static unsigned int width = 300; const static unsigned int border_size = 2; diff --git a/herbe.c b/herbe.c @@ -10,6 +10,29 @@ Display *display; Window window; +int get_eol(char *body, XftFont *font) +{ + int body_len = strlen(body); + XGlyphInfo info; + XftTextExtentsUtf8(display, font, body, body_len, &info); + + int max_text_width = width - 2 * padding; + + if (info.width < max_text_width) + return body_len; + + int eol = max_text_width / font->max_advance_width; + info.width = 0; + + while (info.width < max_text_width) + { + eol++; + XftTextExtentsUtf8(display, font, body, eol, &info); + } + + return --eol; +} + void expire() { XEvent event; @@ -26,6 +49,8 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + char *body = argv[1]; + signal(SIGALRM, expire); alarm(duration); @@ -84,6 +109,8 @@ int main(int argc, char *argv[]) XMapWindow(display, window); + int eol = get_eol(body, font); + XEvent event; while (1) @@ -93,7 +120,7 @@ int main(int argc, char *argv[]) if (event.type == Expose) { XClearWindow(display, window); - XftDrawStringUtf8(draw, &color, font, padding, height - padding, (XftChar8 *)argv[1], strlen(argv[1])); + XftDrawStringUtf8(draw, &color, font, padding, height - padding, body, eol); } if (event.type == ButtonPress) break; @@ -105,4 +132,4 @@ int main(int argc, char *argv[]) XCloseDisplay(display); return EXIT_SUCCESS; -} +} +\ No newline at end of file