From ca5b7c0ec429f4581a9a592c1801bba4a14cb7e9 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Wed, 13 Feb 2019 12:54:19 +0100 Subject: [PATCH] math: Allow --scale=max --- CHANGELOG.md | 1 + doc_src/math.txt | 2 +- src/builtin_math.cpp | 15 ++++++++++----- tests/math.in | 1 + tests/math.out | 1 + 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c22802f5a..646bbe08c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - The default escape delay (to differentiate between the escape key and an alt-combination) has been reduced to 30ms, down from 300ms for the default mode and 100ms for vi-mode (#3904). - In the interest of consistency, `builtin -q` and `command -q` can now be used to query if a builtin or command exists (#5631). - The `path_helper` on macOS now only runs in login shells, matching the bash implementation. +- `math` now accepts `--scale=max` for the maximum scale (#5579). --- diff --git a/doc_src/math.txt b/doc_src/math.txt index 4a53f572a..566b561cd 100644 --- a/doc_src/math.txt +++ b/doc_src/math.txt @@ -17,7 +17,7 @@ Keep in mind that parameter expansion takes before expressions are evaluated. Th The following options are available: -- `-sN` or `--scale=N` sets the scale of the result. `N` must be an integer. A scale of zero causes results to be rounded down to the nearest integer. So `3/2` returns `1` rather than `2` which `1.5` would normally round to. This is for compatibility with `bc` which was the basis for this command prior to fish 3.0.0. Scale values greater than zero causes the result to be rounded using the usual rules to the specified number of decimal places. +- `-sN` or `--scale=N` sets the scale of the result. `N` must be an integer or the word "max" for the maximum scale. A scale of zero causes results to be rounded down to the nearest integer. So `3/2` returns `1` rather than `2` which `1.5` would normally round to. This is for compatibility with `bc` which was the basis for this command prior to fish 3.0.0. Scale values greater than zero causes the result to be rounded using the usual rules to the specified number of decimal places. \subsection return-values Return Values diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 284e47a2b..506f80a5c 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -48,11 +48,16 @@ static int parse_cmd_opts(math_cmd_opts_t &opts, int *optind, //!OCLINT(high nc while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (opt) { case 's': { - opts.scale = fish_wcstoi(w.woptarg); - if (errno || opts.scale < 0 || opts.scale > 15) { - streams.err.append_format(_(L"%ls: '%ls' is not a valid scale value\n"), cmd, - w.woptarg); - return STATUS_INVALID_ARGS; + // "max" is the special value that tells us to pick the maximum scale. + if (wcscmp(w.woptarg, L"max") == 0) { + opts.scale = 15; + } else { + opts.scale = fish_wcstoi(w.woptarg); + if (errno || opts.scale < 0 || opts.scale > 15) { + streams.err.append_format(_(L"%ls: '%ls' is not a valid scale value\n"), cmd, + w.woptarg); + return STATUS_INVALID_ARGS; + } } break; } diff --git a/tests/math.in b/tests/math.in index 48c0afd44..2c92ee052 100644 --- a/tests/math.in +++ b/tests/math.in @@ -8,6 +8,7 @@ math '10 % 6' math -s0 '10 % 6' math '23 % 7' math --scale=6 '5 / 3 * 0.3' +math --scale=max '5 / 3' math "7^2" math -1 + 1 math '-2 * -2' diff --git a/tests/math.out b/tests/math.out index 1f385b54c..05aadc40a 100644 --- a/tests/math.out +++ b/tests/math.out @@ -10,6 +10,7 @@ 4 2 0.5 +1.666666666666667 49 0 4