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.
This commit is contained in:
Fabian Boehm 2022-10-04 18:44:21 +02:00
parent e4f07fe010
commit cb28b39b24
3 changed files with 22 additions and 1 deletions

View File

@ -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.

View File

@ -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),

View File

@ -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