6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-16 11:58:24 +00:00

Further improve the formatting

This commit is contained in:
James Mills 2022-03-20 15:46:38 +10:00
parent 93589b15b0
commit b2a648a2bc
4 changed files with 38 additions and 4 deletions

@ -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()

@ -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(),
)
}

1
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

2
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=