diff --git a/chat-tests/cmd/client.go b/chat-tests/cmd/client.go index ca10399..0a52479 100644 --- a/chat-tests/cmd/client.go +++ b/chat-tests/cmd/client.go @@ -7,27 +7,26 @@ package cmd import ( "fmt" "github.com/spf13/cobra" - "log" - //"os" + "os" ) // clientCmd represents the client command var clientCmd = &cobra.Command{ Use: "client", - Short: "Client interface for tiny-chat", + Short: "Client interface for mini-chat", Long: `Refactored mini-chat client that properly interfaces with its server. Example: - ./tiny-chat client --ip 192.168.0.100 --port 1337`, + ./mini-chat client --ip 192.168.0.100 --port 1337`, Run: func(cmd *cobra.Command, args []string) { - // if len(args) == 0 { - // cmd.Help() - // os.Exit(0) - // } if err := setClientParameters(cmd); err != nil { - log.Fatalf("Error happened trying to set parameters: %v\n", err) + cmd.Help() + fmt.Printf("\n-----------------------\n") + fmt.Println(err) + fmt.Printf("-----------------------\n") + os.Exit(0) } Client() diff --git a/chat-tests/cmd/clientFunc.go b/chat-tests/cmd/clientFunc.go index d8219a6..cb98a58 100644 --- a/chat-tests/cmd/clientFunc.go +++ b/chat-tests/cmd/clientFunc.go @@ -1,17 +1,19 @@ +/* +Copyright © 2024 Raul +*/ + package cmd import ( "bufio" "fmt" + "github.com/jroimartin/gocui" + "github.com/nsf/termbox-go" "log" "net" "os" "strings" "time" - - "github.com/jroimartin/gocui" - "github.com/nsf/termbox-go" - //"time" ) type Message struct { @@ -60,8 +62,13 @@ func listenMessages(g *gocui.Gui) { for { messageFromServer, err := receiveMessage(data.Server) if err != nil { - g.Close() - log.Fatalf("Error occurred reading from server: %v\n", err) + // 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") @@ -109,25 +116,6 @@ func GUI() { } } -// 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 } @@ -146,9 +134,6 @@ func sendToServer(g *gocui.Gui, v *gocui.View) error { if msg == "" { return nil } - //////////////7 - - //////////////7 data.Contents = msg data.toSend()