6
1
mirror of https://git.mills.io/saltyim/saltyim.git synced 2024-06-30 18:51:03 +00:00
prologic-saltyim/format.go
2022-03-20 13:44:22 +10:00

45 lines
920 B
Go

package saltyim
import (
"fmt"
"hash/fnv"
"os"
"github.com/logrusorgru/aurora"
"go.yarn.social/lextwt"
)
func boundedInt(value, low, high uint8) uint8 {
diff := high - low
return (((value - low) % diff) + low)
}
func GetUserColor(user string, lower, upper uint8) uint8 {
h := fnv.New32()
h.Sum([]byte(user))
return boundedInt(uint8(h.Sum32()), lower, upper)
}
func FormatMessage(msg string) string {
s, err := lextwt.ParseSalty(msg)
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing message %q: %s", msg, err)
return ""
}
st, ok := s.(*lextwt.SaltyText)
if !ok {
fmt.Fprintf(os.Stderr, "unexpected error")
return ""
}
userColor := GetUserColor(st.User.String(), 16, 231)
return fmt.Sprintf(
"%s\t%s\t%s\n",
aurora.Sprintf(aurora.Blue(st.Timestamp.DateTime().Local().Format(dateTimeFormat))),
aurora.Sprintf(aurora.Index(userColor, st.User.String())),
st.LiteralText(),
)
}