From: Thomas Weißschuh Date: Sat, 21 Jan 2023 01:28:12 +0000 (+0000) Subject: cal: allow to specifiy columns X-Git-Tag: v2.39-rc1~88^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0e68b637fcf8b5d1f8b034de7f9611bb42bb2b3b;p=thirdparty%2Futil-linux.git cal: allow to specifiy columns Replaces #117 Signed-off-by: Thomas Weißschuh --- diff --git a/misc-utils/cal.1.adoc b/misc-utils/cal.1.adoc index fde97a8655..4ca9d0cde0 100644 --- a/misc-utils/cal.1.adoc +++ b/misc-utils/cal.1.adoc @@ -117,6 +117,9 @@ Display week numbers in the calendar (US or ISO-8601). See the *NOTES* section f *--color*[=_when_]:: Colorize the output. The optional argument _when_ can be *auto*, *never* or *always*. If the _when_ argument is omitted, it defaults to *auto*. The colors can be disabled; for the current built-in default see the *--help* output. See also the *COLORS* section. +*-c, --columns*[=_columns_]:: +Number of columns to use. *auto* uses as many as fit the terminal. + include::man-common/help-version.adoc[] == PARAMETERS diff --git a/misc-utils/cal.c b/misc-utils/cal.c index 59bd4f539f..eb2cfed92f 100644 --- a/misc-utils/cal.c +++ b/misc-utils/cal.c @@ -193,6 +193,11 @@ enum { WEEK_NUM_US=0x200, }; +enum { + COLUMNS_MAX_THREE = -1, + COLUMNS_AUTO = -2, +}; + /* utf-8 can have up to 6 bytes per char; and an extra byte for ending \0 */ static char day_headings[(WEEK_LEN + 1) * 6 + 1]; @@ -281,7 +286,7 @@ int main(int argc, char **argv) struct tm local_time; char *term; time_t now; - int ch = 0, yflag = 0, Yflag = 0; + int ch = 0, yflag = 0, Yflag = 0, cols = COLUMNS_MAX_THREE; static struct cal_control ctl = { .reform_year = DEFAULT_REFORM_YEAR, @@ -318,6 +323,7 @@ int main(int argc, char **argv) {"twelve", no_argument, NULL, 'Y'}, {"help", no_argument, NULL, 'h'}, {"vertical", no_argument, NULL,'v'}, + {"column", required_argument, NULL,'c'}, {NULL, 0, NULL, 0} }; @@ -376,7 +382,7 @@ int main(int argc, char **argv) ctl.weekstart = (wfd + *nl_langinfo(_NL_TIME_FIRST_WEEKDAY) - 1) % DAYS_IN_WEEK; } #endif - while ((ch = getopt_long(argc, argv, "13mjn:sSywYvVh", longopts, NULL)) != -1) { + while ((ch = getopt_long(argc, argv, "13mjn:sSywYvc:Vh", longopts, NULL)) != -1) { err_exclusive_options(ch, longopts, excl, excl_st); @@ -435,6 +441,12 @@ int main(int argc, char **argv) case 'v': ctl.vertical = 1; break; + case 'c': + if (strcmp(optarg, "auto") == 0) + cols = COLUMNS_AUTO; + else + cols = strtosize_or_err(optarg, "foo"); + break; case 'V': print_version(EXIT_SUCCESS); case 'h': @@ -589,7 +601,9 @@ int main(int argc, char **argv) if (ctl.num_months > 1 && ctl.months_in_row == 0) { ctl.months_in_row = MONTHS_IN_YEAR_ROW; /* default */ - if (isatty(STDOUT_FILENO)) { + if (cols > 0) + ctl.months_in_row = cols; + else if (isatty(STDOUT_FILENO)) { int w, mw, extra, new_n; w = get_terminal_width(80); @@ -601,8 +615,15 @@ int main(int argc, char **argv) extra = ((w / mw) - 1) * ctl.gutter_width; new_n = (w - extra) / mw; - if (new_n < MONTHS_IN_YEAR_ROW) + switch (cols) { + case COLUMNS_MAX_THREE: + if (new_n < MONTHS_IN_YEAR_ROW) + ctl.months_in_row = new_n > 0 ? new_n : 1; + break; + case COLUMNS_AUTO: ctl.months_in_row = new_n > 0 ? new_n : 1; + break; + } } } else if (!ctl.months_in_row) ctl.months_in_row = 1; @@ -1263,6 +1284,7 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -Y, --twelve show the next twelve months\n"), out); fputs(_(" -w, --week[=] show US or ISO-8601 week numbers\n"), out); fputs(_(" -v, --vertical show day vertically instead of line\n"), out); + fputs(_(" -c, --columns amount of columns to use\n"), out); fprintf(out, _(" --color[=] colorize messages (%s, %s or %s)\n"), "auto", "always", "never"); fprintf(out, diff --git a/tests/expected/cal/column-5 b/tests/expected/cal/column-5 new file mode 100644 index 0000000000..5b340aff98 --- /dev/null +++ b/tests/expected/cal/column-5 @@ -0,0 +1,27 @@ +5 columns + 2006 + + January February March April May +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 1 1 2 3 4 5 6 + 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13 +15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20 +22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27 +29 30 31 26 27 28 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31 + 30 + June July August September October +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 1 1 2 3 4 5 1 2 1 2 3 4 5 6 7 + 4 5 6 7 8 9 10 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 +11 12 13 14 15 16 17 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 +18 19 20 21 22 23 24 16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 +25 26 27 28 29 30 23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31 + 30 31 + November +Su Mo Tu We Th Fr Sa + 1 2 3 4 + 5 6 7 8 9 10 11 +12 13 14 15 16 17 18 +19 20 21 22 23 24 25 +26 27 28 29 30 + diff --git a/tests/expected/cal/column-6 b/tests/expected/cal/column-6 new file mode 100644 index 0000000000..f3f7f28bbd --- /dev/null +++ b/tests/expected/cal/column-6 @@ -0,0 +1,19 @@ +6 columns + 2006 + + January February March April May June +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 1 1 2 3 4 5 6 1 2 3 + 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 +15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 +22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 +29 30 31 26 27 28 26 27 28 29 30 31 23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 + 30 + July August September October November December +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 1 2 3 4 5 1 2 1 2 3 4 5 6 7 1 2 3 4 1 2 + 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 + 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 +16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 +23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 +30 31 31 diff --git a/tests/expected/cal/column-auto b/tests/expected/cal/column-auto new file mode 100644 index 0000000000..2a3ea1fee1 --- /dev/null +++ b/tests/expected/cal/column-auto @@ -0,0 +1,35 @@ +auto columns + 2006 + + January February March +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 7 1 2 3 4 1 2 3 4 + 8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11 +15 16 17 18 19 20 21 12 13 14 15 16 17 18 12 13 14 15 16 17 18 +22 23 24 25 26 27 28 19 20 21 22 23 24 25 19 20 21 22 23 24 25 +29 30 31 26 27 28 26 27 28 29 30 31 + + April May June +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 1 2 3 4 5 6 1 2 3 + 2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10 + 9 10 11 12 13 14 15 14 15 16 17 18 19 20 11 12 13 14 15 16 17 +16 17 18 19 20 21 22 21 22 23 24 25 26 27 18 19 20 21 22 23 24 +23 24 25 26 27 28 29 28 29 30 31 25 26 27 28 29 30 +30 + July August September +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 1 2 3 4 5 1 2 + 2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9 + 9 10 11 12 13 14 15 13 14 15 16 17 18 19 10 11 12 13 14 15 16 +16 17 18 19 20 21 22 20 21 22 23 24 25 26 17 18 19 20 21 22 23 +23 24 25 26 27 28 29 27 28 29 30 31 24 25 26 27 28 29 30 +30 31 + October November December +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 5 6 7 1 2 3 4 1 2 + 8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9 +15 16 17 18 19 20 21 12 13 14 15 16 17 18 10 11 12 13 14 15 16 +22 23 24 25 26 27 28 19 20 21 22 23 24 25 17 18 19 20 21 22 23 +29 30 31 26 27 28 29 30 24 25 26 27 28 29 30 + 31 diff --git a/tests/ts/cal/column b/tests/ts/cal/column new file mode 100755 index 0000000000..e3abcf4eb6 --- /dev/null +++ b/tests/ts/cal/column @@ -0,0 +1,49 @@ +#!/bin/bash + +# +# Copyright (C) 2007-2018 Karel Zak +# Copyright (C) 2023 Thomas Weißschuh +# +# This file is part of util-linux. +# +# This file is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This file is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +TS_TOPDIR="${0%/*}/../.." +TS_DESC="year" + +. "$TS_TOPDIR"/functions.sh +ts_init "$*" + +ts_check_test_command "$TS_CMD_CAL" + +export TERM=linux + +USETERM=$( ts_has_option "useterm" "$*" ) +MYTIME="29 11 2006" + +function call_cal { + + ts_init_subtest "$(echo "$1" | cut -d ' ' -f 1)" + ts_log "$1" + shift + if [ "$USETERM" == "yes" ]; then + $TS_CMD_CAL "$@" + fi + $TS_CMD_CAL "$@" >> $TS_OUTPUT + ts_finalize_subtest +} + +call_cal "6 columns" --year --column 6 $MYTIME +call_cal "5 columns" --year --column 5 $MYTIME +call_cal "auto columns" --year --column auto $MYTIME + +ts_finalize +