From: Karel Zak Date: Wed, 8 Jun 2022 09:28:02 +0000 (+0200) Subject: column: add -1 placeholder to address last visible column X-Git-Tag: v2.39-rc1~620 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=32e4c34f60a9fd20b2075eac9f7cefc364ed75b8;p=thirdparty%2Futil-linux.git column: add -1 placeholder to address last visible column Addresses: https://github.com/util-linux/util-linux/issues/1700 Signed-off-by: Karel Zak --- diff --git a/text-utils/column.1.adoc b/text-utils/column.1.adoc index 6da1dccfee..99559ac46b 100644 --- a/text-utils/column.1.adoc +++ b/text-utils/column.1.adoc @@ -69,7 +69,7 @@ The argument _columns_ for *--table-** options is a comma separated list of the column names as defined by *--table-columns*, or names defined by *--table-column* or it's column number in order as specified by input. It's possible to mix names and numbers. The special placeholder '0' (e.g. -R0) may -be used to specify all columns. +be used to specify all columns and '-1' (e.g. -R -1) to specify the last visible column. *-J, --json*:: Use JSON output format to print the table, the option *--table-columns* is required and the option *--table-name* is recommended. diff --git a/text-utils/column.c b/text-utils/column.c index 245c2dd8d6..d8eae9ee59 100644 --- a/text-utils/column.c +++ b/text-utils/column.c @@ -251,23 +251,6 @@ static void init_table(struct column_control *ctl) } -static struct libscols_column *string_to_column(struct column_control *ctl, const char *str) -{ - struct libscols_column *cl; - - if (isdigit_string(str)) { - uint32_t n = strtou32_or_err(str, _("failed to parse column")) - 1; - - cl = scols_table_get_column(ctl->tab, n); - } else - cl = scols_table_get_column_by_name(ctl->tab, str); - - if (!cl) - errx(EXIT_FAILURE, _("undefined column name '%s'"), str); - - return cl; -} - static struct libscols_column *get_last_visible_column(struct column_control *ctl) { struct libscols_iter *itr; @@ -287,6 +270,25 @@ static struct libscols_column *get_last_visible_column(struct column_control *ct return last; } +static struct libscols_column *string_to_column(struct column_control *ctl, const char *str) +{ + struct libscols_column *cl; + + if (isdigit_string(str)) { + uint32_t n = strtou32_or_err(str, _("failed to parse column")) - 1; + + cl = scols_table_get_column(ctl->tab, n); + } else if (strcmp(str, "-1") == 0) + cl = get_last_visible_column(ctl); + else + cl = scols_table_get_column_by_name(ctl->tab, str); + + if (!cl) + errx(EXIT_FAILURE, _("undefined column name '%s'"), str); + + return cl; +} + static int column_set_flag(struct libscols_column *cl, int fl) { int cur = scols_column_get_flags(cl);