mini-chat/cmd/clientFunc.go

245 lines
5.0 KiB
Go

/*
Copyright © 2024 Raul <raul@bulgariu.xyz>
*/
package cmd
import (
"bufio"
"fmt"
"github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
"log"
"net"
"os"
"strings"
"time"
)
type Message struct {
Contents string
//Date time.Time
Server net.Conn
}
type ProfileData struct {
Username string
}
var Profile ProfileData
func (m Message) toSend() {
m.Server.Write([]byte(m.Contents))
}
var (
serverPort string = "1302"
serverIP string
data Message
)
func Client() {
conn, err := net.Dial("tcp", serverIP+":"+serverPort)
if err != nil {
log.Fatalf("Error occurred trying to connect to server: %v\n", err)
}
defer conn.Close()
data.Server = conn
nameRequest, err := receiveMessage(conn)
if err != nil {
log.Fatalf("Error occurred reading from server while requesting name: %v\n", err)
}
fmt.Print(nameRequest)
sendName(conn)
GUI()
}
////////////////////////////////////////////////////
// REFACTORING BELOW //
////////////////////////////////////////////////////
func listenMessages(g *gocui.Gui) {
time.Sleep(time.Millisecond * 250)
chatbox, err := g.View("chatbox")
if err != nil {
log.Panicln(err)
}
for {
messageFromServer, err := receiveMessage(data.Server)
if err != nil {
// Avoid triggering an error if client quits the client
if err == gocui.ErrQuit {
g.Close()
} else {
g.Close()
log.Fatalf("Error occurred reading from server: %v\n", err)
}
}
formattedMessage := strings.TrimRight(messageFromServer, "\n")
fmt.Fprintln(chatbox, formattedMessage)
termbox.Interrupt()
}
}
func receiveMessage(conn net.Conn) (s string, err error) {
serverMessage := make([]byte, 2048)
if _, err := conn.Read(serverMessage); err != nil {
return "", err
}
finalMessage := string(serverMessage)
return finalMessage, nil
}
func sendName(conn net.Conn) {
message, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
log.Fatalf("Error occurred sending message to server: %v\n", err)
}
Profile.Username = strings.TrimRight(message, "\n")
if _, err := conn.Write([]byte(message)); err != nil {
log.Fatalf("Error occurred writing to server: %v\n", err)
}
}
func GUI() {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.SetManagerFunc(layout)
g.Mouse = true
g.Cursor = true
initKeybindings(g)
go listenMessages(g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
func quit(*gocui.Gui, *gocui.View) error {
return gocui.ErrQuit
}
func sendToServer(g *gocui.Gui, v *gocui.View) error {
textarea, err := g.View("textarea")
if err != nil {
log.Panicln(err)
}
message := textarea.Buffer()
msg := string(message)
if msg == "" {
return nil
}
if msg == "" {
return nil
}
data.Contents = msg
data.toSend()
textarea.Clear()
textarea.SetCursor(0, 0)
return nil
}
func scrollView(v *gocui.View, dy int) error {
if v != nil {
v.Autoscroll = false
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+dy); err != nil {
return err
}
}
return nil
}
func autoscroll(g *gocui.Gui, v *gocui.View) error {
chatbox, err := g.View("chatbox")
if err != nil {
log.Panicf("Error happened setting autoscroll: %v\n", err)
}
chatbox.Autoscroll = true
return nil
}
func initKeybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
// if err := g.SetKeybinding("button", gocui.MouseLeft, gocui.ModNone, sendToServer); err != nil {
// log.Panicln(err)
// }
if err := g.SetKeybinding("textarea", gocui.KeyCtrlA, gocui.ModNone, autoscroll); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("textarea", gocui.KeyEnter, gocui.ModNone, sendToServer); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("chatbox", gocui.MouseWheelUp, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
scrollView(v, -1)
return nil
}); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("chatbox", gocui.MouseWheelDown, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
scrollView(v, 1)
return nil
}); err != nil {
log.Panicln(err)
}
return nil
}
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if chatbox, err := g.SetView("chatbox", 2, 1, maxX-28, maxY-5); err != nil {
if err != gocui.ErrUnknownView {
return err
}
chatbox.Autoscroll = true
chatbox.Title = "Chat Box (Find source at https://git.bulgariu.xyz/raul/mini-chat!)"
}
// if button, err := g.SetView("button", maxX/2+32, maxY-4, maxX-28, maxY-2); err != nil {
// if err != gocui.ErrUnknownView {
// return err
// }
//
// button.Wrap = true
// button.Frame = true
// fmt.Fprintln(button, "Send it")
// }
if textarea, err := g.SetView("textarea", 2, maxY-4, maxX-28, maxY-2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
if _, err := g.SetCurrentView("textarea"); err != nil {
log.Panicln(err)
}
textarea.Title = "(" + Profile.Username + ") " + "Send message" + " (Ctrl+A: Autoscroll)"
textarea.Wrap = true
textarea.Editable = true
}
return nil
}