Final PR #1

Merged
raul merged 9 commits from testing into main 2024-04-14 10:05:00 +02:00
2 changed files with 51 additions and 50 deletions
Showing only changes of commit ca10bc4284 - Show all commits

View File

@ -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
for {
msg, err := bufio.NewReader(os.Stdin).ReadString('\n')
cobra.CheckErr(err)
conn.Write([]byte(msg))
go readMsg(conn)
ui(conn)
}
// msg, err := bufio.NewReader(os.Stdin).ReadString('\n')
// fmt.Println(msg)
//fmt.Println(string(data))
//ui()
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
}
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 (
"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.Fprintln(chatbox, string(reply))
termbox.Interrupt()
}
}
fmt.Fprint(chatbox, msg)
textarea.Clear()
textarea.SetCursor(0, 0)
return nil
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
}