245 lines
5.5 KiB
Go
245 lines
5.5 KiB
Go
package tui
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"whspbrd/pkg/cell_size"
|
|
"whspbrd/pkg/render_image"
|
|
|
|
"github.com/jroimartin/gocui"
|
|
)
|
|
|
|
type ChatMessage struct {
|
|
ID string `json:"id"`
|
|
Sender string `json:"sender"`
|
|
Receiver string `json:"receiver"`
|
|
Content string `json:"content"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
type ChatData struct {
|
|
Messages []ChatMessage `json:"messages"`
|
|
}
|
|
|
|
var messages []string
|
|
var users = []string{"Alice\n", "Bob\n", "Charlie\n"}
|
|
var prevWidth, prevHeight int
|
|
|
|
// Load messages from configs/servers/default/users/<username>/messages.json
|
|
func loadMessages(username string) {
|
|
chatFile := filepath.Join("configs", "servers", "default", "users", strings.ToLower(username), "messages.json")
|
|
data, err := os.ReadFile(chatFile)
|
|
if err != nil {
|
|
log.Printf("Error reading chat file: %v", err)
|
|
return
|
|
}
|
|
|
|
var chatData ChatData
|
|
if err := json.Unmarshal(data, &chatData); err != nil {
|
|
log.Printf("Error parsing JSON: %v", err)
|
|
return
|
|
}
|
|
|
|
for _, msg := range chatData.Messages {
|
|
decoded, err := base64.StdEncoding.DecodeString(msg.Content)
|
|
if err != nil {
|
|
log.Printf("Error decoding message: %v", err)
|
|
continue
|
|
}
|
|
// Check if message is ending with a newline and remove it
|
|
if strings.HasSuffix(string(decoded), "\n") {
|
|
decoded = []byte(strings.TrimSuffix(string(decoded), "\n"))
|
|
}
|
|
|
|
// Show timestamp in readable format
|
|
t, _ := time.Parse(time.RFC3339, msg.Timestamp)
|
|
formattedTime := t.Format("2006-01-02 15:04")
|
|
|
|
if strings.EqualFold(msg.Sender, username) {
|
|
// Sent by "you"
|
|
messages = append(messages, fmt.Sprintf("\t\t\t\tYou (%s):\n\t\t\t\t%s", formattedTime, decoded))
|
|
} else {
|
|
// Sent by other user
|
|
messages = append(messages, fmt.Sprintf("\t\t\t\t%s (%s):\n\t\t\t\t%s", msg.Sender, formattedTime, decoded))
|
|
}
|
|
}
|
|
}
|
|
|
|
func layout(g *gocui.Gui) error {
|
|
maxX, maxY := g.Size()
|
|
|
|
if maxX != prevWidth || maxY != prevHeight {
|
|
prevWidth, prevHeight = maxX, maxY
|
|
if chatView, err := g.View("chat"); err == nil {
|
|
updateChatView(chatView)
|
|
}
|
|
}
|
|
|
|
if v, err := g.SetView("users", 0, 0, 20, maxY-1); err != nil {
|
|
if err != gocui.ErrUnknownView {
|
|
return err
|
|
}
|
|
v.Title = "Users"
|
|
v.Clear()
|
|
for _, u := range users {
|
|
fmt.Fprintln(v, u)
|
|
}
|
|
}
|
|
|
|
if v, err := g.SetView("chat", 21, 0, maxX-1, maxY-5); err != nil {
|
|
if err != gocui.ErrUnknownView {
|
|
return err
|
|
}
|
|
v.Title = "Chat"
|
|
v.Wrap = true
|
|
v.Autoscroll = true
|
|
updateChatView(v)
|
|
}
|
|
|
|
if v, err := g.SetView("input", 21, maxY-4, maxX-1, maxY-1); err != nil {
|
|
if err != gocui.ErrUnknownView {
|
|
return err
|
|
}
|
|
v.Title = "Type your message"
|
|
v.Editable = true
|
|
v.Wrap = true
|
|
if _, err := g.SetCurrentView("input"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func updateChatView(v *gocui.View) {
|
|
v.Clear()
|
|
for i, msg := range messages {
|
|
fmt.Fprintf(v, "%s\n\n", msg)
|
|
w, h, err := cell_size.GetTerminalCellSizePixels()
|
|
if err != nil {
|
|
log.Println("Error getting terminal cell size:", err)
|
|
continue
|
|
}
|
|
if h > w {
|
|
h = h * 2
|
|
w = 0
|
|
} else {
|
|
w = w*3 - (w / 10)
|
|
h = 0
|
|
}
|
|
render_image.RenderImage("kogami-rounded.png", i*3+2, 23, w, h, 0)
|
|
}
|
|
}
|
|
|
|
func sendMessage(g *gocui.Gui, v *gocui.View) error {
|
|
input := strings.TrimSpace(v.Buffer())
|
|
v.Clear()
|
|
v.SetCursor(0, 0)
|
|
v.SetOrigin(0, 0)
|
|
|
|
if input != "" {
|
|
messages = append(messages, "\t\t\t\tYou:\n\t\t\t\t"+input)
|
|
if chatView, err := g.View("chat"); err == nil {
|
|
updateChatView(chatView)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func cursorDown(g *gocui.Gui, v *gocui.View) error {
|
|
if v != nil {
|
|
cx, cy := v.Cursor()
|
|
if err := v.SetCursor(cx, cy+1); err != nil {
|
|
ox, oy := v.Origin()
|
|
if err := v.SetOrigin(ox, oy+1); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func cursorUp(g *gocui.Gui, v *gocui.View) error {
|
|
if v != nil {
|
|
ox, oy := v.Origin()
|
|
cx, cy := v.Cursor()
|
|
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
|
|
if err := v.SetOrigin(ox, oy-1); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func nextView(g *gocui.Gui, v *gocui.View) error {
|
|
if v == nil || v.Name() == "chat" {
|
|
_, err := g.SetCurrentView("input")
|
|
return err
|
|
}
|
|
_, err := g.SetCurrentView("chat")
|
|
return err
|
|
}
|
|
|
|
func keybindings(g *gocui.Gui) error {
|
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
|
return err
|
|
}
|
|
if err := g.SetKeybinding("chat", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
|
return err
|
|
}
|
|
if err := g.SetKeybinding("input", gocui.KeyCtrlSpace, gocui.ModNone, nextView); err != nil {
|
|
return err
|
|
}
|
|
if err := g.SetKeybinding("input", gocui.KeyEnter, gocui.ModAlt, insertNewline); err != nil {
|
|
return err
|
|
}
|
|
if err := g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, sendMessage); err != nil {
|
|
return err
|
|
}
|
|
if err := g.SetKeybinding("chat", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
|
|
return err
|
|
}
|
|
if err := g.SetKeybinding("chat", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func insertNewline(g *gocui.Gui, v *gocui.View) error {
|
|
v.EditNewLine()
|
|
return nil
|
|
}
|
|
|
|
func quit(g *gocui.Gui, v *gocui.View) error {
|
|
return gocui.ErrQuit
|
|
}
|
|
|
|
func Run(username string) {
|
|
// Load chat history for the given user
|
|
loadMessages(username)
|
|
|
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
|
if err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
defer g.Close()
|
|
|
|
g.SetManagerFunc(layout)
|
|
|
|
if err := keybindings(g); err != nil {
|
|
log.Panicln(err)
|
|
}
|
|
|
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
|
log.Panicln(err)
|
|
}
|
|
}
|