package cmd import ( "fmt" "github.com/jroimartin/gocui" "github.com/nsf/termbox-go" "log" "net" "time" ) func ui(conn net.Conn) { 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) go listener(g, conn) if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } 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 } 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" } 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" } return nil }