From 8315a2ff1507571ef14d77beac867b34a7d5e6da Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Tue, 30 Jan 2018 15:38:06 +0100 Subject: [PATCH] cal: follow terminal width * modify number of months in row according to the terminal width * don't print blank space behind last char on row Signed-off-by: Karel Zak --- misc-utils/cal.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/misc-utils/cal.c b/misc-utils/cal.c index 562ae2afb0..891517ba84 100644 --- a/misc-utils/cal.c +++ b/misc-utils/cal.c @@ -75,6 +75,10 @@ #include "strutils.h" #include "optutils.h" #include "timeutils.h" +#include "ttyutils.h" + +#define DOY_MONTH_WIDTH 27 /* -j month width */ +#define DOM_MONTH_WIDTH 20 /* month width */ static int has_term = 0; static const char *Senter = "", *Sexit = ""; /* enter and exit standout mode */ @@ -542,10 +546,19 @@ int main(int argc, char **argv) } } - if (ctl.num_months > 1 && ctl.months_in_row == 0) - ctl.months_in_row = ctl.julian ? MONTHS_IN_YEAR_ROW - 1 : - MONTHS_IN_YEAR_ROW; - else if (!ctl.months_in_row) + if (ctl.num_months > 1 && ctl.months_in_row == 0) { + ctl.months_in_row = MONTHS_IN_YEAR_ROW; /* default */ + + if (isatty(STDOUT_FILENO)) { + int w = get_terminal_width(STDOUT_FILENO); + int mw = ctl.julian ? DOY_MONTH_WIDTH : DOM_MONTH_WIDTH; + int extra = ((w / mw) - 1) * ctl.gutter_width; + int new_n = (w - extra) / mw; + + if (new_n < MONTHS_IN_YEAR_ROW) + ctl.months_in_row = new_n; + } + } else if (!ctl.months_in_row) ctl.months_in_row = 1; if (!ctl.num_months) @@ -798,11 +811,8 @@ static void cal_output_months(struct cal_month *month, const struct cal_control my_putstring(out); } } - if (i == NULL) { - int extra = ctl->num_months > 3 ? 0 : 1; - sprintf(out, "%*s\n", ctl->gutter_width - extra, ""); - my_putstring(out); - } + if (i == NULL) + my_putstring("\n"); } } -- 2.47.2