Testing messages loading
This commit is contained in:
parent
7754b2c91c
commit
71be36fc93
@ -1,9 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"whspbrd/pkg/render_image"
|
||||
"whspbrd/internal/tui"
|
||||
//"whspbrd/pkg/render_image"
|
||||
)
|
||||
|
||||
func main() {
|
||||
render_image.RenderImage("kogami-pf-edit.jpg", 0, 3, 20, 0, 1)
|
||||
//config.NewConfigLoadTemplate()
|
||||
tui.Run("alice")
|
||||
}
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
{
|
||||
"messages" :
|
||||
[
|
||||
{
|
||||
"id": "1",
|
||||
"sender": "alice",
|
||||
"receiver": "bob",
|
||||
"content": "SGVsbG8gQm9iLCBob3cgYXJlIHlvdT8K",
|
||||
"timestamp": "2023-10-01T10:00:00Z"
|
||||
},
|
||||
{
|
||||
"id": "2",
|
||||
"sender": "bob",
|
||||
"receiver": "alice",
|
||||
"content": "SGkgQWxpY2UhIEknbSBkb2luZyB3ZWxsLCB0aGFua3MuIEhvdyBhYm91dCB5b3U/Cg==",
|
||||
"timestamp": "2023-10-01T10:05:00Z"
|
||||
},
|
||||
{
|
||||
"id": "3",
|
||||
"sender": "alice",
|
||||
"receiver": "bob",
|
||||
"content": "SSdtIGdyZWF0LCB0aGFua3MgZm9yIGFza2luZwo=",
|
||||
"timestamp": "2023-10-01T10:06:00Z"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,22 +1,77 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"whspbrd/pkg/cell_size"
|
||||
"whspbrd/pkg/render_image"
|
||||
|
||||
"github.com/jroimartin/gocui"
|
||||
//"whspbrd/pkg/systray"
|
||||
"whspbrd/pkg/render_image"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
@ -67,27 +122,19 @@ func updateChatView(v *gocui.View) {
|
||||
v.Clear()
|
||||
for i, msg := range messages {
|
||||
fmt.Fprintf(v, "%s\n\n", msg)
|
||||
//imagePath := "kogami-rounded.png"
|
||||
|
||||
//file, err := os.Open(imagePath)
|
||||
//if err != nil {
|
||||
// log.Println("Error opening image:", err)
|
||||
// continue
|
||||
//}
|
||||
//defer file.Close()
|
||||
|
||||
//img, _, err := image.Decode(file)
|
||||
//if err != nil {
|
||||
// log.Println("Error decoding image:", err)
|
||||
// continue
|
||||
//}
|
||||
|
||||
// Print image directly to terminal (stdout)
|
||||
render_image.RenderImage("kogami-rounded.png", i*3+2, 23, 50, 50, 0)
|
||||
//err = kittyimg.Fprintln(os.Stdout, img)
|
||||
//if err != nil {
|
||||
// log.Println("Error rendering image:", err)
|
||||
//}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -103,8 +150,6 @@ func sendMessage(g *gocui.Gui, v *gocui.View) error {
|
||||
updateChatView(chatView)
|
||||
}
|
||||
}
|
||||
v.Clear()
|
||||
v.SetCursor(0, 0)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -144,43 +189,27 @@ func nextView(g *gocui.Gui, v *gocui.View) error {
|
||||
}
|
||||
|
||||
func keybindings(g *gocui.Gui) error {
|
||||
// Ctrl+C to quit
|
||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Enter to send message
|
||||
// Enter adds a new line (multiline input)
|
||||
//if err := g.SetKeybinding("input", gocui.KeyArrowDown, gocui.ModNone, insertNewline); 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
|
||||
}
|
||||
|
||||
// Alt+Enter inserts newline
|
||||
if err := g.SetKeybinding("input", gocui.KeyEnter, gocui.ModAlt, insertNewline); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Enter (no modifiers) sends message
|
||||
if err := g.SetKeybinding("input", gocui.KeyEnter, gocui.ModNone, sendMessage); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Arrow up
|
||||
if err := g.SetKeybinding("chat", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
|
||||
return err
|
||||
}
|
||||
// Arrow down
|
||||
if err := g.SetKeybinding("chat", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -193,7 +222,10 @@ func quit(g *gocui.Gui, v *gocui.View) error {
|
||||
return gocui.ErrQuit
|
||||
}
|
||||
|
||||
func Run() {
|
||||
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user