package main import ( "github.com/jroimartin/gocui" ) func main() { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { panic(err) } defer g.Close() g.SetManagerFunc(layout) if err := keybindings(g); err != nil { panic(err) } if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { panic(err) } } func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("status", maxX-20, maxY-3, maxX-1, maxY-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Frame = false v.BgColor = gocui.ColorGreen v.FgColor = gocui.ColorBlack v.Write([]byte("Status Bar")) } if v, err := g.SetView("main", 0, 0, maxX-21, maxY-1); err != nil { if err != gocui.ErrUnknownView { return err } v.Editable = true v.Wrap = true v.Autoscroll = true v.Frame = false if _, err := g.SetCurrentView("main"); err != nil { return err } } return nil } func keybindings(g *gocui.Gui) error { if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { return err } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit }
package main import ( "fmt" "github.com/jroimartin/gocui" ) func main() { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { panic(err) } defer g.Close() g.SetManagerFunc(layout) if err := keybindings(g); err != nil { panic(err) } if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { panic(err) } } func layout(g *gocui.Gui) error { maxX, maxY := g.Size() if v, err := g.SetView("menu", maxX/2-10, maxY/2-2, maxX/2+10, maxY/2+2); err != nil { if err != gocui.ErrUnknownView { return err } v.Frame = true v.Title = "Menu" v.Highlight = true v.SelBgColor = gocui.ColorGreen v.SelFgColor = gocui.ColorBlack fmt.Fprintln(v, "Option 1") fmt.Fprintln(v, "Option 2") fmt.Fprintln(v, "Option 3") if _, err := g.SetCurrentView("menu"); err != nil { return err } } return nil } func keybindings(g *gocui.Gui) error { if err := g.SetKeybinding("menu", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil { return err } if err := g.SetKeybinding("menu", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, selectOption); err != nil { return err } if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { return err } return nil } func cursorDown(g *gocui.Gui, v *gocui.View) error { cx, cy := v.Cursor() if err := v.SetCursor(cx, cy+1); err != nil { ox, oy := v.Origin() if err := v.SetOrigin(ox, oy+1); err != nil { return err } } return nil } func cursorUp(g *gocui.Gui, v *gocui.View) error { cx, cy := v.Cursor() if err := v.SetCursor(cx, cy-1); err != nil && cy > 0 { ox, oy := v.Origin() if err := v.SetOrigin(ox, oy-1); err != nil { return err } } return nil } func selectOption(g *gocui.Gui, v *gocui.View) error { _, cy := v.Cursor() switch cy { case 0: fmt.Println("Option 1 selected") case 1: fmt.Println("Option 2 selected") case 2: fmt.Println("Option 3 selected") } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit }In this example, we define the "menu" view with three options and highlight the currently selected option using the v.SelBgColor and v.SelFgColor properties. We also set the keybindings to allow the user to navigate the menu with the arrow keys and select an option using Enter. Overall, the go github.com.jroimartin.gocui library provides a simple and intuitive interface for developing terminal-based applications. Its small size and minimalistic design make it a great package for building lightweight applications.