~adnano/astronaut

ref: 5b2ba7d7bffd153f57a7eac22f172a5d4d56c6a9 astronaut/ui/splice.go -rw-r--r-- 389 bytes
5b2ba7d7Adnan Maolood ui/view: Remove focus variable 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package ui

import (
	"github.com/mattn/go-runewidth"
)

// Splice splits the string s at the point x.
func Splice(s string, x int) (string, string) {
	if runewidth.StringWidth(s) <= x {
		return s, ""
	}
	r := []rune(s)
	width := 0
	i := 0
	for ; i < len(r); i++ {
		cw := runewidth.RuneWidth(r[i])
		if width+cw > x {
			break
		}
		width += cw
	}
	return string(r[:i]), string(r[i:])
}