diff options
-rw-r--r-- | go.mod | 1 | ||||
-rw-r--r-- | lib/format/format.go | 26 |
2 files changed, 27 insertions, 0 deletions
@@ -32,6 +32,7 @@ require ( github.com/miolini/datacounter v1.0.2 github.com/mitchellh/go-homedir v1.1.0 github.com/pkg/errors v0.9.1 + github.com/rivo/uniseg v0.2.0 github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab github.com/stretchr/testify v1.8.0 github.com/syndtr/goleveldb v1.0.0 diff --git a/lib/format/format.go b/lib/format/format.go index 3f00fe18..ad0b778c 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -11,6 +11,7 @@ import ( "git.sr.ht/~rjarry/aerc/models" "github.com/emersion/go-message/mail" "github.com/mattn/go-runewidth" + "github.com/rivo/uniseg" ) // AddressForHumans formats the address. If the address's name @@ -67,6 +68,31 @@ func CompactPath(name string, sep rune) (compact string) { return } +func TruncateHead(s string, w int, head string) string { + width := runewidth.StringWidth(s) + if width <= w { + return s + } + w -= runewidth.StringWidth(head) + pos := 0 + g := uniseg.NewGraphemes(s) + for g.Next() { + var chWidth int + for _, r := range g.Runes() { + chWidth = runewidth.RuneWidth(r) + if chWidth > 0 { + break + } + } + if width-chWidth <= w { + pos, _ = g.Positions() + break + } + width -= chWidth + } + return head + s[pos:] +} + type Ctx struct { FromAddress string AccountName string |