From ca10bc42844c614cb14db0d03d18f6cf0c21043b Mon Sep 17 00:00:00 2001 From: raul Date: Sat, 13 Apr 2024 13:11:36 +0200 Subject: [PATCH] Implement Gocui UI into mini-chat client --- cmd/client.go | 61 ++++++++++++++++++++++++++++----------------------- cmd/ui.go | 40 +++++++++++++++------------------ 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/cmd/client.go b/cmd/client.go index 1977808..05cce97 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -7,10 +7,11 @@ package cmd import ( "bufio" "fmt" + "github.com/jroimartin/gocui" + "github.com/spf13/cobra" + "log" "net" "os" - - "github.com/spf13/cobra" ) // clientCmd represents the client command @@ -25,6 +26,9 @@ var clientCmd = &cobra.Command{ }, } +var err error +var conn net.Conn + func init() { rootCmd.AddCommand(clientCmd) @@ -41,13 +45,6 @@ func init() { // clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } -func readMsg(conn net.Conn) error { - reply := make([]byte, 1024) - conn.Read(reply) - fmt.Println(string(reply)) - return nil -} - func client(cmd *cobra.Command) { port, _ := cmd.Flags().GetString("port") ip, _ := cmd.Flags().GetString("ip") @@ -59,31 +56,39 @@ func client(cmd *cobra.Command) { port = "1302" } socket := ip + ":" + port - conn, err := net.Dial("tcp", socket) + conn, err = net.Dial("tcp", socket) cobra.CheckErr(err) defer conn.Close() - // go func() { - // for { - // reply := make([]byte, 1024) - // conn.Read(reply) - // fmt.Println(string(reply)) - // } - // }() + reply := make([]byte, 1024) + conn.Read(reply) + fmt.Print(string(reply)) - // TODO: GET READING DATA FROM SERVER WORKING + msg, err := bufio.NewReader(os.Stdin).ReadString('\n') + cobra.CheckErr(err) + conn.Write([]byte(msg)) - for { - msg, err := bufio.NewReader(os.Stdin).ReadString('\n') - cobra.CheckErr(err) - conn.Write([]byte(msg)) - go readMsg(conn) + ui(conn) + +} + +func sendmsg(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 } - // msg, err := bufio.NewReader(os.Stdin).ReadString('\n') - // fmt.Println(msg) + if msg == "" { + return nil + } - //fmt.Println(string(data)) - - //ui() + fmt.Fprint(conn, msg) + textarea.Clear() + textarea.SetCursor(0, 0) + return nil } diff --git a/cmd/ui.go b/cmd/ui.go index 41260db..d3988f6 100644 --- a/cmd/ui.go +++ b/cmd/ui.go @@ -3,10 +3,13 @@ package cmd import ( "fmt" "github.com/jroimartin/gocui" + "github.com/nsf/termbox-go" "log" + "net" + "time" ) -func ui() { +func ui(conn net.Conn) { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { log.Panicln(err) @@ -17,31 +20,30 @@ func ui() { g.Cursor = true initKeybindings(g) + go listener(g, conn) + if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } -func quit(*gocui.Gui, *gocui.View) error { - return gocui.ErrQuit -} - -func sendmsg(g *gocui.Gui, v *gocui.View) error { +func listener(g *gocui.Gui, conn net.Conn) { + time.Sleep(time.Second) chatbox, err := g.View("chatbox") - textarea, err := g.View("textarea") if err != nil { log.Panicln(err) } - message := textarea.Buffer() - msg := string(message) - if msg == "" { - return nil - } + for { + reply := make([]byte, 2048) + conn.Read(reply) - fmt.Fprint(chatbox, msg) - textarea.Clear() - textarea.SetCursor(0, 0) - return nil + fmt.Fprintln(chatbox, string(reply)) + termbox.Interrupt() + } +} + +func quit(*gocui.Gui, *gocui.View) error { + return gocui.ErrQuit } func initKeybindings(g *gocui.Gui) error { @@ -67,7 +69,6 @@ func layout(g *gocui.Gui) error { return err } chatbox.Title = "Chat Box" - fmt.Fprintln(chatbox, "Hello world!") } if button, err := g.SetView("button", maxX/2+32, maxY-4, maxX/2+40, maxY-2); err != nil { @@ -99,10 +100,5 @@ func layout(g *gocui.Gui) error { currentUsers.Title = "Connected users" } - // TODO: Add "Users currently online" view - // if currentUsers, err := g.SetView("currentUsers", maxX/2+34, 1, maxX/2+40, maxY/2+11); err != nil { - // - // } - return nil }