From 81f5b527a0f32446902b0cadd05f368210dee7b8 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Fri, 23 Feb 2024 09:34:52 +0000 Subject: [PATCH] add line wrapping support --- cmd/enbas/show.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) 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() +}