Implement Gocui UI into mini-chat client

This commit is contained in:
raul 2024-04-13 13:11:36 +02:00
parent 75dac0ccef
commit ca10bc4284
2 changed files with 51 additions and 50 deletions

View File

@ -7,10 +7,11 @@ package cmd
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/jroimartin/gocui"
"github.com/spf13/cobra"
"log"
"net" "net"
"os" "os"
"github.com/spf13/cobra"
) )
// clientCmd represents the client command // clientCmd represents the client command
@ -25,6 +26,9 @@ var clientCmd = &cobra.Command{
}, },
} }
var err error
var conn net.Conn
func init() { func init() {
rootCmd.AddCommand(clientCmd) rootCmd.AddCommand(clientCmd)
@ -41,13 +45,6 @@ func init() {
// clientCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") // 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) { func client(cmd *cobra.Command) {
port, _ := cmd.Flags().GetString("port") port, _ := cmd.Flags().GetString("port")
ip, _ := cmd.Flags().GetString("ip") ip, _ := cmd.Flags().GetString("ip")
@ -59,31 +56,39 @@ func client(cmd *cobra.Command) {
port = "1302" port = "1302"
} }
socket := ip + ":" + port socket := ip + ":" + port
conn, err := net.Dial("tcp", socket) conn, err = net.Dial("tcp", socket)
cobra.CheckErr(err) cobra.CheckErr(err)
defer conn.Close() defer conn.Close()
// go func() { reply := make([]byte, 1024)
// for { conn.Read(reply)
// reply := make([]byte, 1024) fmt.Print(string(reply))
// conn.Read(reply)
// fmt.Println(string(reply))
// }
// }()
// TODO: GET READING DATA FROM SERVER WORKING
for {
msg, err := bufio.NewReader(os.Stdin).ReadString('\n') msg, err := bufio.NewReader(os.Stdin).ReadString('\n')
cobra.CheckErr(err) cobra.CheckErr(err)
conn.Write([]byte(msg)) conn.Write([]byte(msg))
go readMsg(conn)
ui(conn)
} }
// msg, err := bufio.NewReader(os.Stdin).ReadString('\n') func sendmsg(g *gocui.Gui, v *gocui.View) error {
// fmt.Println(msg) textarea, err := g.View("textarea")
if err != nil {
//fmt.Println(string(data)) log.Panicln(err)
}
//ui() message := textarea.Buffer()
msg := string(message)
if msg == "" {
return nil
}
if msg == "" {
return nil
}
fmt.Fprint(conn, msg)
textarea.Clear()
textarea.SetCursor(0, 0)
return nil
} }

View File

@ -3,10 +3,13 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
"github.com/nsf/termbox-go"
"log" "log"
"net"
"time"
) )
func ui() { func ui(conn net.Conn) {
g, err := gocui.NewGui(gocui.OutputNormal) g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
@ -17,31 +20,30 @@ func ui() {
g.Cursor = true g.Cursor = true
initKeybindings(g) initKeybindings(g)
go listener(g, conn)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err) log.Panicln(err)
} }
} }
func quit(*gocui.Gui, *gocui.View) error { func listener(g *gocui.Gui, conn net.Conn) {
return gocui.ErrQuit time.Sleep(time.Second)
}
func sendmsg(g *gocui.Gui, v *gocui.View) error {
chatbox, err := g.View("chatbox") chatbox, err := g.View("chatbox")
textarea, err := g.View("textarea")
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
message := textarea.Buffer() for {
msg := string(message) reply := make([]byte, 2048)
if msg == "" { conn.Read(reply)
return nil
fmt.Fprintln(chatbox, string(reply))
termbox.Interrupt()
}
} }
fmt.Fprint(chatbox, msg) func quit(*gocui.Gui, *gocui.View) error {
textarea.Clear() return gocui.ErrQuit
textarea.SetCursor(0, 0)
return nil
} }
func initKeybindings(g *gocui.Gui) error { func initKeybindings(g *gocui.Gui) error {
@ -67,7 +69,6 @@ func layout(g *gocui.Gui) error {
return err return err
} }
chatbox.Title = "Chat Box" 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 { 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" 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 return nil
} }