diff options
| author | Julian Hurst <julian.hurst@digdash.com> | 2025-03-20 14:46:33 +0100 |
|---|---|---|
| committer | Julian Hurst <julian.hurst@digdash.com> | 2025-03-20 14:46:33 +0100 |
| commit | 5efb8c021fe2166648591e46591144a49bf575bf (patch) | |
| tree | a557f6cc09cf30bf55e30d7592d57ff0399f19b3 /tui/width.ha | |
| parent | a1b536135d35c7937d7f6dbf76e16ce4c48abfa6 (diff) | |
| download | hare-tui-5efb8c021fe2166648591e46591144a49bf575bf.tar.gz | |
Substring widthwise instead of runewise
Diffstat (limited to 'tui/width.ha')
| -rw-r--r-- | tui/width.ha | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/tui/width.ha b/tui/width.ha index 6f9631c..ed498e6 100644 --- a/tui/width.ha +++ b/tui/width.ha @@ -30,3 +30,17 @@ export fn strwidth(s: str) uint = { }; return sum; }; + +export fn subwidth(s: str, end: (size | strings::end)) str = { + const runes = strings::torunes(s); + defer free(runes); + let sum = 0u; + for (let i = 0z; i < len(runes); i += 1) { + const r = runes[i]; + sum += runewidth(r); + if (sum > end: uint) { + return strings::sub(s, 0, i - 1); + }; + }; + return s; +}; |
