package main import ( "fmt" "log" "time" "github.com/jroimartin/gocui" ) var ( viewArr = []string{"exercise1", "exercise2"} active = 0 ) func main() { g, err := gocui.NewGui(gocui.OutputNormal) if err != nil { log.Panicln(err) } defer g.Close() g.Highlight = true g.SelFgColor = gocui.ColorRed g.SetManagerFunc(layout) 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 initKeybindings(g *gocui.Gui) error { if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { log.Panicln(err) } if err := g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, nextView); err != nil { log.Panicln(err) } if err := g.SetKeybinding("", gocui.KeyEnter, gocui.ModNone, buttonSelect); err != nil { log.Panicln(err) } return nil } func buttonSelect(g *gocui.Gui, v *gocui.View) error { exercise1, err := g.View("exercise1") if err != nil { return err } exercise2, err := g.View("exercise2") if err != nil { return err } switch v { case exercise1: g.Close() exer1() time.Sleep(time.Second) main() case exercise2: fmt.Fprintln(v, "BAR") } return nil } func layout(g *gocui.Gui) error { //maxX, maxY := g.Size() if exercise1, err := g.SetView("exercise1", 2, 1, 12, 4); err != nil { if err != gocui.ErrUnknownView { return err } exercise1.Title = "Ex. 1" g.SetCurrentView("exercise1") fmt.Fprintln(exercise1, "Methods") } if exercise2, err := g.SetView("exercise2", 14, 1, 24, 4); err != nil { if err != gocui.ErrUnknownView { return err } exercise2.Title = "Ex. 2" } return nil } func nextView(g *gocui.Gui, v *gocui.View) error { nextIndex := (active + 1) % len(viewArr) name := viewArr[nextIndex] if _, err := setCurrentViewOnTop(g, name); err != nil { return err } active = nextIndex return nil } func setCurrentViewOnTop(g *gocui.Gui, name string) (*gocui.View, error) { if _, err := g.SetCurrentView(name); err != nil { return nil, err } return g.SetViewOnTop(name) }