summaryrefslogtreecommitdiffstats
path: root/src/utils/runes.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/runes.go')
-rw-r--r--src/utils/runes.go24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/utils/runes.go b/src/utils/runes.go
new file mode 100644
index 0000000..76cb5e3
--- /dev/null
+++ b/src/utils/runes.go
@@ -0,0 +1,24 @@
+package utils
+
+import (
+ rw "github.com/mattn/go-runewidth"
+)
+
+func TruncateFront(s string, w int, prefix string) string {
+ if rw.StringWidth(s) <= w {
+ return s
+ }
+ r := []rune(s)
+ pw := rw.StringWidth(prefix)
+ w -= pw
+ width := 0
+ i := len(r) - 1
+ for ; i >= 0; i-- {
+ cw := rw.RuneWidth(r[i])
+ width += cw
+ if width > w {
+ break
+ }
+ }
+ return prefix + string(r[i+1:len(r)])
+}