From 313d22fd649a3e54af3ba039174c5e52b079357a Mon Sep 17 00:00:00 2001 From: raul Date: Mon, 15 Apr 2024 09:00:48 +0200 Subject: [PATCH] Add select-buttons exercise --- select-buttons/go.mod | 11 ++++++ select-buttons/go.sum | 10 ++++++ select-buttons/main.go | 81 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 select-buttons/go.mod create mode 100644 select-buttons/go.sum create mode 100644 select-buttons/main.go diff --git a/select-buttons/go.mod b/select-buttons/go.mod new file mode 100644 index 0000000..ef6468f --- /dev/null +++ b/select-buttons/go.mod @@ -0,0 +1,11 @@ +module select-buttons + +go 1.22.2 + +require github.com/jroimartin/gocui v0.5.0 + +require ( + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/nsf/termbox-go v1.1.1 // indirect + github.com/rivo/uniseg v0.4.7 // indirect +) diff --git a/select-buttons/go.sum b/select-buttons/go.sum new file mode 100644 index 0000000..4a4ecbd --- /dev/null +++ b/select-buttons/go.sum @@ -0,0 +1,10 @@ +github.com/jroimartin/gocui v0.5.0 h1:DCZc97zY9dMnHXJSJLLmx9VqiEnAj0yh0eTNpuEtG/4= +github.com/jroimartin/gocui v0.5.0/go.mod h1:l7Hz8DoYoL6NoYnlnaX6XCNR62G7J5FfSW5jEogzaxE= +github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY= +github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= diff --git a/select-buttons/main.go b/select-buttons/main.go new file mode 100644 index 0000000..51c112a --- /dev/null +++ b/select-buttons/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "fmt" + "log" + + "github.com/jroimartin/gocui" +) + +var ( + viewArr = []string{"exercise1", "exercise2"} + active = 0 +) + +func main() { + fmt.Println("test") + 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) + } + 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" + 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) +}