commit b41b280509d879904aaebf68d68817d71829d335
parent 30b0961b2e0b8187289ec7b9b038a344a85b0171
Author: Samuel Dudik <samuel.dudik@gmail.com>
Date: Thu, 13 Aug 2020 20:45:08 +0200
New lines are now printed properly
Diffstat:
2 files changed, 25 insertions(+), 14 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -1,4 +1,5 @@
* Handle multiple notifications at the same time
-* Properly handle duration = 0
+* ~~Properly handle duration = 0~~
* Keyboard shortcut to dismiss and accept notifications (also mouse)
-* Properly print new lines
+* ~~Properly print new lines~~
+* Refactor code
+\ No newline at end of file
diff --git a/herbe.c b/herbe.c
@@ -24,23 +24,31 @@ static void die(const char *format, ...)
int get_max_len(char *body, XftFont *font, int max_text_width)
{
- int body_len = strlen(body);
+ int eol = strlen(body);
XGlyphInfo info;
- XftTextExtentsUtf8(display, font, (FcChar8 *)body, body_len, &info);
+ XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
- if (info.width < max_text_width)
- return body_len;
+ if (info.width > max_text_width)
+ {
- int eol = max_text_width / font->max_advance_width;
- info.width = 0;
+ eol = max_text_width / font->max_advance_width;
+ info.width = 0;
- while (info.width < max_text_width)
- {
- eol++;
- XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
+ while (info.width < max_text_width)
+ {
+ eol++;
+ XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
+ }
+
+ eol--;
}
- eol--;
+ for (int i = 0; i < eol; i++)
+ if (body[i] == '\n')
+ return ++i;
+
+ if (info.width < max_text_width)
+ return eol;
int temp = eol;
@@ -167,4 +175,4 @@ int main(int argc, char *argv[])
XCloseDisplay(display);
exit(EXIT_SUCCESS);
-}
+}
+\ No newline at end of file