From 014d2ab62c233d0ded665fdc9cfd09f4ec95398d Mon Sep 17 00:00:00 2001 From: GenjiFujimoto Date: Sun, 28 May 2023 21:04:04 +0200 Subject: [PATCH] Improve formatting --- format_output.c | 23 +++++------------------ popup.c | 12 +++++------- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/format_output.c b/format_output.c index dc5ed18..49e667e 100644 --- a/format_output.c +++ b/format_output.c @@ -3,30 +3,17 @@ int main() { - int second = 0; static char *buf = NULL; static size_t size = 0; - ssize_t len = 0; - for (long n=1; (len = getline(&buf, &size, stdin)) > 0; n++) + long n = 1; + while(getline(&buf, &size, stdin) > 0) { - if (n < 5) + if (n++ < 5) /* overflow possible */ continue; - if (len && buf[len - 1] == '\n') - buf[len - 1] = '\0'; if (strncmp(buf,"-->", 3) == 0) - { - if (second) - { - second = 0; - } - else - { putchar('\n'); - second = 1; - } - } - else if (buf[0] != 0) /* Don't print newline at EOF */ - puts(buf); + else if (buf[0] != 10) + fputs(buf, stdout); } return 0; } diff --git a/popup.c b/popup.c index 97d5559..c4c142e 100644 --- a/popup.c +++ b/popup.c @@ -62,7 +62,7 @@ int get_max_len(char *string, XftFont *font, int max_text_width) eol--; } - for (int i = 0; i < eol; i++) + for (int i = 0; i < MIN(eol+1, strlen(string)); i++) if (string[i] == '\n') { string[i] = ' '; @@ -73,11 +73,9 @@ int get_max_len(char *string, XftFont *font, int max_text_width) while (eol && (string[eol] & 0xC0) == 0x80) --eol; - if (info.width <= max_text_width) - return eol; - - - /* prefer splitting at spaces */ + /* uncomment to always split at spaces */ + /* if (info.width <= max_text_width) */ + /* return eol; */ /* int temp = eol; */ /* while (string[eol] != ' ' && eol) */ /* --eol; */ @@ -141,7 +139,7 @@ int main() } free(buf); if (num_of_lines == 0) - die("stdin is empty"); + die("stdin is empty"); unsigned int text_height = font->ascent - font->descent; unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;