Shell Script formatting broken due to whitespace trimming

I use a two-line shell widget that displays a small table via pre-formatting the output with whitespaces.
It used to work well until a recent update where the whitespaces at the beginning and end of the output got trimmed, thus breaking the formatting.


Note that the whitespace is lacking at the beginning of the first line and the end of the second line.

I understand that this behavior is useful for many widgets. This could be resolved via an additional option on the widget whether to trim whitespaces or not.

Device information:

  • Type of Mac: MacBook Pro M1 16" and MacBook Air M2.
  • macOS version: Ventura 13.4.1 (22F82)
  • BetterTouchTool version: 4.075 (2291)


I managed to restore formatting by inserting one of invisible unicode spaces from here (but most of them still get trimmed).

Can you post your script? I can't think of any trimming setting that was recently changed

Yeah it's this one: cpu top 2 (and top 30) (toolbar widget preset)

(note the screenshot from an earlier version)

The script being this:

ps -A -o %cpu,comm | perl -naE '
$c+=$F[0] if /Chrome\.app/;
$p = m{/([^/]+)\.app/} ? $1 : m{/([^/]+)$} ? $1 : m{^\s*[\d.]+\s++(.*)} ? $1 : $_;
    join "\n",
    map {sprintf "%16s %5.1f", substr($_,0,16), $p{$_}}
    (sort {$p{$b} <=> $p{$a}} keys %p)[0,1];

I think it might be because of some changes to 3rd party libraries where they attempt to center the line and automatically trim spaces when doing that.

One more observation:

This one is wrongly formatted:

"   123 \n   456 "

This one is formatted well:

"\xc2\xad   123 \n   456 \xc2\xad"

("\xc2\xad") is an invisible space in UTF8

BTT is indeed trimming the white space at the beginning and the end (but has always done so).

I'll add an option to disable this.

1 Like

The new alpha 4.188 now has an option to trim or not to trim whitespace:

1 Like