add line wrapping support

This commit is contained in:
Dan Anglin 2024-02-23 09:34:52 +00:00
parent db6017dccf
commit 81f5b527a0
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638

View file

@ -5,6 +5,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"strings" "strings"
"unicode"
"codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/client"
"codeflow.dananglin.me.uk/apollo/enbas/internal/config" "codeflow.dananglin.me.uk/apollo/enbas/internal/config"
@ -148,7 +149,7 @@ func (c *showCommand) showAccount(gts *client.Client) error {
account.FollowersCount, account.FollowersCount,
account.FollowingCount, account.FollowingCount,
account.StatusCount, account.StatusCount,
stripHTMLTags(account.Note), wrapLine(stripHTMLTags(account.Note), "\n ", 80),
metadata, metadata,
account.URL, account.URL,
) )
@ -171,3 +172,26 @@ func stripHTMLTags(text string) string {
} }
} }
} }
func wrapLine(line, separator string, charLimit int) string {
if len(line) <= charLimit {
return line
}
leftcursor, rightcursor := 0, 0
var builder strings.Builder
for rightcursor < (len(line) - charLimit) {
rightcursor += charLimit
for !unicode.IsSpace(rune(line[rightcursor-1])) {
rightcursor--
}
builder.WriteString(line[leftcursor:rightcursor] + separator)
leftcursor = rightcursor
}
builder.WriteString(line[rightcursor:])
return builder.String()
}