Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a command option to truncate strings #414

Closed
ralphptorres opened this issue Oct 12, 2023 · 7 comments
Closed

Add a command option to truncate strings #414

ralphptorres opened this issue Oct 12, 2023 · 7 comments
Labels

Comments

@ralphptorres
Copy link

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
Copy link
Owner

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.

@FelixKratz FelixKratz added the enhancement New feature or request label Oct 12, 2023
@ralphptorres
Copy link
Author

Not a prolific coder but will try :)

@FelixKratz
Copy link
Owner

FelixKratz commented Oct 15, 2023

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
Copy link
Author

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
Copy link
Author

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

@FelixKratz
Copy link
Owner

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants