From dff54a8d0b1a9c06f304c562e3a5c5d53c6c730f Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 23 Apr 2024 09:58:07 +0200 Subject: [PATCH] Add interface to be refactored --- chat-tests/cmd/clientFunc.go | 187 ++++++++++++++++++++++++++++++++++- 1 file changed, 184 insertions(+), 3 deletions(-) diff --git a/chat-tests/cmd/clientFunc.go b/chat-tests/cmd/clientFunc.go index f17f158..2020e0e 100644 --- a/chat-tests/cmd/clientFunc.go +++ b/chat-tests/cmd/clientFunc.go @@ -1,16 +1,197 @@ package cmd import ( -// "fmt" -// "log" -// "net" + "bufio" + "fmt" + "log" + "net" + "os" + "time" + + "github.com/jroimartin/gocui" + //"time" ) +type Message struct { + Contents string + //Date time.Time + Server net.Conn +} + +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 + fmt.Print(receiveMessage(conn)) + sendName(conn) + GUI() +} + +//////////////////////////////////////////////////// +// REFACTORING BELOW // +//////////////////////////////////////////////////// + +func listenMessages(g *gocui.Gui) { + time.Sleep(time.Second) + chatbox, err := g.View("chatbox") + if err != nil { + log.Panicln(err) + } + for { + messageFromServer := receiveMessage(data.Server) + fmt.Fprint(chatbox, messageFromServer) + } +} + +func receiveMessage(conn net.Conn) string { + serverMessage := make([]byte, 2048) + if _, err := conn.Read(serverMessage); err != nil { + log.Fatalf("Error occurred reading from server: %v\n", err) + } + finalMessage := string(serverMessage) + return finalMessage +} + +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) + } + + 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 listener(g *gocui.Gui, conn net.Conn) { +// time.Sleep(time.Second) +// chatbox, err := g.View("chatbox") +// if err != nil { +// log.Panicln(err) +// } +// for { +// reply := make([]byte, 2048) +// _, err := conn.Read(reply) +// if err != nil { +// g.Close() +// log.Fatalf("Server closed connection\n") +// } +// +// fmt.Fprintln(chatbox, string(reply)) +// termbox.Interrupt() +// } +// } + +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 + } + //////////////7 + + //////////////7 + + data.Contents = msg + data.toSend() + + textarea.Clear() + textarea.SetCursor(0, 0) + 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.KeyEnter, gocui.ModNone, sendToServer); 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/2+40, maxY-5); err != nil { + if err != gocui.ErrUnknownView { + return err + } + chatbox.Autoscroll = true + chatbox.Title = "Chat Box" + } + + if button, err := g.SetView("button", maxX/2+32, maxY-4, maxX/2+40, 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/2+28, maxY-2); err != nil { + if err != gocui.ErrUnknownView { + return err + } + if _, err := g.SetCurrentView("textarea"); err != nil { + log.Panicln(err) + } + textarea.Title = "Send message" + textarea.Wrap = true + textarea.Editable = true + } + + return nil }