diff --git a/cmd/enbas/show.go b/cmd/enbas/show.go index d88c7a6..dcd811a 100644 --- a/cmd/enbas/show.go +++ b/cmd/enbas/show.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" "strings" + "unicode" "codeflow.dananglin.me.uk/apollo/enbas/internal/client" "codeflow.dananglin.me.uk/apollo/enbas/internal/config" @@ -148,7 +149,7 @@ func (c *showCommand) showAccount(gts *client.Client) error { account.FollowersCount, account.FollowingCount, account.StatusCount, - stripHTMLTags(account.Note), + wrapLine(stripHTMLTags(account.Note), "\n ", 80), metadata, 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() +}