diff options
author | Karel Zak | 2018-01-30 15:38:06 +0100 |
---|---|---|
committer | Karel Zak | 2018-01-30 16:10:06 +0100 |
commit | 8315a2ff1507571ef14d77beac867b34a7d5e6da (patch) | |
tree | 3c5ed8a88c1e9f3e4b0f463f3ae11ebaa7f82fa6 /misc-utils/cal.c | |
parent | build-sys: (configure) fix bashisms (diff) | |
download | kernel-qcow2-util-linux-8315a2ff1507571ef14d77beac867b34a7d5e6da.tar.gz kernel-qcow2-util-linux-8315a2ff1507571ef14d77beac867b34a7d5e6da.tar.xz kernel-qcow2-util-linux-8315a2ff1507571ef14d77beac867b34a7d5e6da.zip |
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 <kzak@redhat.com>
Diffstat (limited to 'misc-utils/cal.c')
-rw-r--r-- | misc-utils/cal.c | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/misc-utils/cal.c b/misc-utils/cal.c index 562ae2afb..891517ba8 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"); } } |