Compare commits
2 Commits
39c2dde016
...
c4fb5606f6
Author | SHA1 | Date |
---|---|---|
raul | c4fb5606f6 | |
raul | 6599c66abb |
|
@ -5,6 +5,7 @@ Copyright © 2024 Raul
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
|
@ -15,13 +16,10 @@ import (
|
|||
// clientCmd represents the client command
|
||||
var clientCmd = &cobra.Command{
|
||||
Use: "client",
|
||||
Short: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Short: "Connect to a mini-chat server",
|
||||
Long: `Connect to a mini-chat server.
|
||||
Example:
|
||||
./mini-chat client --ip 192.168.0.50 --port 1337`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
client(cmd)
|
||||
},
|
||||
|
@ -43,13 +41,49 @@ 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")
|
||||
if ip == "" {
|
||||
fmt.Println("Not enough arguments, run \"-h\" parameter to see all the parameters!")
|
||||
os.Exit(1)
|
||||
os.Exit(0)
|
||||
}
|
||||
if port == "" {
|
||||
port = "1302"
|
||||
}
|
||||
socket := ip + ":" + port
|
||||
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))
|
||||
// }
|
||||
// }()
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// msg, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
||||
// fmt.Println(msg)
|
||||
|
||||
//fmt.Println(string(data))
|
||||
|
||||
//ui()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/jroimartin/gocui"
|
||||
"log"
|
||||
)
|
||||
|
||||
func ui() {
|
||||
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)
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
fmt.Fprint(chatbox, msg)
|
||||
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, sendmsg); err != nil {
|
||||
log.Panicln(err)
|
||||
}
|
||||
|
||||
if err := g.SetKeybinding("textarea", gocui.KeyEnter, gocui.ModNone, sendmsg); 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-6); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
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 {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
//button.BgColor = gocui.ColorRed
|
||||
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
|
||||
}
|
||||
|
||||
if currentUsers, err := g.SetView("currentUsers", maxX/2+42, 1, maxX-6, maxY-6); err != nil {
|
||||
if err != gocui.ErrUnknownView {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue