FelixKratz / SketchyBar

A highly customizable macOS status bar replacement
https://felixkratz.github.io/SketchyBar/
GNU General Public License v3.0
5.45k stars 85 forks source link

Add a command option to truncate strings #414

Closed ralphptorres closed 10 months ago

ralphptorres commented 10 months ago

I am aware of delegating the string truncation to the scripts used in bar items, say with sed or cut as in here. I use this method myself (either with cut or shell parameter expansion). But I thought that since icons and labels are just strings anyway, it would be nice to have the ability to set the max length via a command option. That way, we can use sketchybar --default to enable truncation globally without having to do them manually in each item where it's necessary. Not only it will enable truncation for bar items that do not use scripts, it will also reduce computational cost compared to when using sed or cut (though I know these two aren't really costly).

FelixKratz commented 10 months ago

That is a nice idea and also should be straight forward to implement in the text file as a new property which preprocesses the text. If you like you could try to raise a PR.

ralphptorres commented 10 months ago

Not a prolific coder but will try :)

FelixKratz commented 10 months ago

I realised that it is not as straight forward as I made it seem, since caution has to be applied for multibyte unicode sequences. Simply truncating the char* is thus not enough, making this a bit subtle so I decided to implement it. The new property is called max_chars and take a positive integer. The strings are truncated by a horizontal ellipsis symbol (i.e.: …).

ralphptorres commented 10 months ago

Tested, it works. The only issue I'm aware of is char counting: max_chars means max bytes. So one multi-byte char (e.g. emoji, cjk, etc.) takes up a few from the count as expected.

ralphptorres commented 10 months ago

But since truncation got implemented, we can already close this issue. Thanks for the work!

FelixKratz commented 10 months ago

The only issue I'm aware of is char counting: max_chars means max bytes. So one multi-byte char (e.g. emoji, cjk, etc.) takes up a few from the count as expected.

I have implemented it such that multi-byte chars are only counted as one char, so max_chars=4 should allow you to print 4 unicode characters e.g. 4 emojis or 4 regular letters. So this should not be an issue?

ralphptorres commented 10 months ago

I retested and you’re right, that’s the case. Sorry for misreporting. I used the emoji sequence ones (e.g. country flags) rather than the plain ones. 😅