From cb28b39b24df64d658e805e99a0aa2e300aa768c Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Tue, 4 Oct 2022 18:44:21 +0200 Subject: [PATCH] string shorten: Make max of 0 mean no shortening This makes it easier to just slot in `string shorten` wherever, without having to do a weird "if test $max -gt 0" check. --- doc_src/cmds/string-shorten.rst | 2 +- src/builtins/string.cpp | 16 ++++++++++++++++ tests/checks/string.fish | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/doc_src/cmds/string-shorten.rst b/doc_src/cmds/string-shorten.rst index 87b8ba444..ab27ea2d2 100644 --- a/doc_src/cmds/string-shorten.rst +++ b/doc_src/cmds/string-shorten.rst @@ -22,7 +22,7 @@ Description The escape sequences reflect what fish knows about, and how it computes its output. Your terminal might support more escapes, or not support escape sequences that fish knows about. -If **-m** or **--max** is given, truncate at the given width. Otherwise, the lowest non-zero width of all input strings is used. +If **-m** or **--max** is given, truncate at the given width. Otherwise, the lowest non-zero width of all input strings is used. A max of 0 means no shortening takes place, all STRINGs are printed as-is. If **-N** or **--no-newline** is given, only the first line (or last line with **--left**) of each STRING is used, and an ellipsis is added if it was multiline. This only works for STRINGs being given as arguments, multiple lines given on stdin will be interpreted as separate STRINGs instead. diff --git a/src/builtins/string.cpp b/src/builtins/string.cpp index 566826ec6..88782a826 100644 --- a/src/builtins/string.cpp +++ b/src/builtins/string.cpp @@ -1664,6 +1664,22 @@ static int string_shorten(parser_t &parser, io_streams_t &streams, int argc, con auto ell_width = fish_wcswidth(ell); arg_iterator_t aiter_width(argv, optind, streams); + + if (opts.max == 0) { + // Special case: Max of 0 means no shortening. + // This makes this more reusable, so you don't need special-cases like + // + // if test $shorten -gt 0 + // string shorten -m $shorten whatever + // else + // echo whatever + // end + while (const wcstring *arg = aiter_width.nextstr()) { + streams.out.append(*arg + L"\n"); + } + return STATUS_CMD_ERROR; + } + while (const wcstring *arg = aiter_width.nextstr()) { // Visible width only makes sense line-wise. // So either we have no-newlines (which means we shorten on the first newline), diff --git a/tests/checks/string.fish b/tests/checks/string.fish index b35969af4..52dcec924 100644 --- a/tests/checks/string.fish +++ b/tests/checks/string.fish @@ -950,3 +950,8 @@ for i in (seq 1 (string length -V -- $str)) test $len = $i or echo Oopsie ellipsizing to $i failed end + +string shorten -m0 foo bar asodjsaoidj +# CHECK: foo +# CHECK: bar +# CHECK: asodjsaoidj