Improves image rendering by allowing resizing based on terminal cell size, providing more control over image dimensions. This is enabled by a new `units` parameter where 0 defaults to the previous pixel sizing and 1 enables terminal unit sizing. Removes unused dependency.
213 lines
4.4 KiB
Go
213 lines
4.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
_ "image/jpeg"
|
|
_ "image/png"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/jroimartin/gocui"
|
|
//"whspbrd/pkg/systray"
|
|
"whspbrd/pkg/render_image"
|
|
)
|
|
|
|
var messages []string
|
|
var users = []string{"Alice\n", "Bob\n", "Charlie\n"}
|
|
|
|
var prevWidth, prevHeight int
|
|
|
|
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)
|
|
//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)
|
|
//}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
v.Clear()
|
|
v.SetCursor(0, 0)
|
|
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 {
|
|
// 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
|
|
}
|
|
|
|
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() {
|
|
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)
|
|
}
|
|
}
|