From 5edb80e54adb36e7fbd417418653aa2679f28822 Mon Sep 17 00:00:00 2001 From: raul Date: Thu, 4 Apr 2024 09:39:37 +0200 Subject: [PATCH] Add gocui library practice exercise --- gocui-practice/go.mod | 10 ++++++++++ gocui-practice/go.sum | 10 ++++++++++ gocui-practice/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 gocui-practice/go.mod create mode 100644 gocui-practice/go.sum create mode 100644 gocui-practice/main.go diff --git a/gocui-practice/go.mod b/gocui-practice/go.mod new file mode 100644 index 0000000..79fa642 --- /dev/null +++ b/gocui-practice/go.mod @@ -0,0 +1,10 @@ +module gocui-practice + +go 1.22.1 + +require ( + github.com/jroimartin/gocui v0.5.0 // indirect + 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/gocui-practice/go.sum b/gocui-practice/go.sum new file mode 100644 index 0000000..4a4ecbd --- /dev/null +++ b/gocui-practice/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/gocui-practice/main.go b/gocui-practice/main.go new file mode 100644 index 0000000..eda67f1 --- /dev/null +++ b/gocui-practice/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" + "log" + + "github.com/jroimartin/gocui" +) + +func main() { + g, err := gocui.NewGui(gocui.OutputNormal) + if err != nil { + log.Panicln(err) + } + defer g.Close() + + g.SetManagerFunc(layout) + + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { + log.Panicln(err) + } + + if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { + log.Panicln(err) + } +} + +func layout(g *gocui.Gui) error { + maxX, maxY := g.Size() + if v, err := g.SetView("hello", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { + if err != gocui.ErrUnknownView { + return err + } + fmt.Fprintln(v, "Hello world!") + } + return nil +} + +func quit(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit +}