From b2a648a2bc81ba503725d3d42db66469496a8a3e Mon Sep 17 00:00:00 2001 From: James Mills Date: Sun, 20 Mar 2022 15:46:38 +1000 Subject: [PATCH] Further improve the formatting --- client.go | 19 +++++++++++++++++-- format.go | 20 ++++++++++++++++++-- go.mod | 1 + go.sum | 2 ++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index c147209..d38267f 100644 --- a/client.go +++ b/client.go @@ -1,16 +1,18 @@ package saltyim import ( + "bytes" "context" "fmt" - "log" "strings" "sync" "github.com/dim13/crc24" "github.com/gdamore/tcell/v2" "github.com/keys-pub/keys" + "github.com/posener/formatter" "github.com/rivo/tview" + log "github.com/sirupsen/logrus" "go.mills.io/salty" "go.yarn.social/lextwt" ) @@ -99,6 +101,19 @@ func (cc *chatClient) updateChatBox(inCh <-chan string, app *tview.Application, continue } + buf := &bytes.Buffer{} + + f := formatter.Formatter{ + Writer: buf, + Indent: []byte("> "), + Width: 80, + } + + if _, err := f.Write([]byte(s.LiteralText())); err != nil { + log.WithError(err).Error("error formatting message") + continue + } + cc.mu.RLock() app.QueueUpdateDraw(func() { fmt.Fprintf(chatBox, @@ -108,7 +123,7 @@ func (cc *chatClient) updateChatBox(inCh <-chan string, app *tview.Application, getUserColor(s.User.String()).Hex(), s.User.String(), cc.palette["text"], - s.LiteralText(), + buf.String(), ) }) cc.mu.RUnlock() diff --git a/format.go b/format.go index 377eb87..81fe42f 100644 --- a/format.go +++ b/format.go @@ -1,11 +1,13 @@ package saltyim import ( + "bytes" "fmt" "hash/fnv" "os" "github.com/logrusorgru/aurora" + "github.com/posener/formatter" "go.yarn.social/lextwt" ) @@ -20,6 +22,7 @@ func GetUserColor(user string, lower, upper uint8) uint8 { return boundedInt(uint8(h.Sum32()), lower, upper) } +// FormatMessage formats the msg for display on the terminal func FormatMessage(msg string) string { s, err := lextwt.ParseSalty(msg) if err != nil { @@ -35,10 +38,23 @@ func FormatMessage(msg string) string { userColor := GetUserColor(st.User.String(), 16, 231) + buf := &bytes.Buffer{} + + f := formatter.Formatter{ + Writer: buf, + Indent: []byte("> "), + Width: 80, + } + + if _, err := f.Write([]byte(st.LiteralText())); err != nil { + fmt.Fprintf(os.Stderr, "error formatting message: %s", err) + return "" + } + return fmt.Sprintf( - "%s\t%s\t%s\n", + "%s\t%s\n%s\n", aurora.Sprintf(aurora.Blue(st.Timestamp.DateTime().Local().Format(dateTimeFormat))), aurora.Sprintf(aurora.Index(userColor, st.User.String())), - st.LiteralText(), + buf.String(), ) } diff --git a/go.mod b/go.mod index 6a7a7ba..53ae927 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/logrusorgru/aurora v2.0.3+incompatible github.com/magiconair/properties v1.8.6 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/posener/formatter v1.0.0 github.com/prometheus/client_golang v1.12.1 // indirect github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8 github.com/shopspring/decimal v1.3.1 // indirect diff --git a/go.sum b/go.sum index 23cc9c1..cede19b 100644 --- a/go.sum +++ b/go.sum @@ -399,6 +399,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= github.com/posener/complete v1.2.3/go.mod h1:WZIdtGGp+qx0sLrYKtIRAruyNpv6hFCicSgv7Sy7s/s= +github.com/posener/formatter v1.0.0 h1:TwXJq26f9ERTjCpZj8xEWj77WPWfX/nBgGx52Ap/gYM= +github.com/posener/formatter v1.0.0/go.mod h1:xrC89js6vw5dde/9yUKKU9MY5ivn980yX4VG7gYQTvU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=