Finally figured out how to read view contents

I think I'm finally getting the hang of Gocui, it's pretty interesting
how it separates each view into a different section for the layout
function, also went ahead and separated the keybindings into their own
function, and yes I'm stealing from the _examples folder of the official
project, it's really helpful okay?
This commit is contained in:
raul 2024-04-08 08:23:18 +02:00
parent 8acdc82081
commit 01e9e7e2db
1 changed files with 57 additions and 25 deletions

View File

@ -1,14 +1,18 @@
package main
import (
"bytes"
"fmt"
"io"
"log"
"os/exec"
"strings"
"time"
"github.com/jroimartin/gocui"
)
var input chan int
var input chan string
func main() {
g, err := gocui.NewGui(gocui.OutputNormal)
@ -24,17 +28,12 @@ func main() {
g.Mouse = true
g.Cursor = true
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("WOW", gocui.MouseLeft, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
initKeybindings(g)
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
var numToIncrease int
@ -48,7 +47,7 @@ func counter() {
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if v, err := g.SetView("hello", maxX/2-20, maxY/2-18, maxX/2+20, maxY/2-15); err != nil {
if v, err := g.SetView("test", maxX/2-20, maxY/2-18, maxX/2+20, maxY/2-15); err != nil {
if err != gocui.ErrUnknownView {
return err
}
@ -56,39 +55,72 @@ func layout(g *gocui.Gui) error {
fmt.Fprintln(v, "Hello!")
}
if v, err := g.SetView("anotherone", maxX/2-20, maxY/2-13, maxX/2+20, maxY/2-6); err != nil {
if v, err := g.SetView("editor", maxX/2-20, maxY/2-13, maxX/2+20, maxY/2-6); err != nil {
if err != gocui.ErrUnknownView {
return err
}
if _, err := g.SetCurrentView("anotherone"); err != nil {
if _, err := g.SetCurrentView("editor"); err != nil {
return err
}
v.Title = "Editing"
v.Editable = true
v.Wrap = true
fmt.Fprintln(v, "Holaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!")
fmt.Fprintln(v, "")
}
if v, err := g.SetView("WOW", maxX/2-20, maxY/2, maxX/2+20, maxY/2+5); err != nil {
if v, err := g.SetView("quit", maxX/2-20, maxY/2, maxX/2+20, maxY/2+5); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// var number int
// go func() {
// number = <-input
// }()
// fmt.Fprintln(v, number)
v.Title = "Quit button"
fmt.Fprintln(v, "You should quit... NOW!", numToIncrease)
go func() {
message := <-input
formattedMsg := bytes.NewBufferString(message)
fmt.Fprintln(formattedMsg)
}()
}
// if err := g.SetKeybinding("WOW", gocui.MouseLeft, gocui.ModNone, fcn); err != nil {
// fmt.Fprintln(v, "Clicked")
// }
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
func quitter(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func initKeybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("editor", gocui.KeyCtrlA, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
v.Mask ^= '*'
return nil
}); err != nil {
return err
}
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quitter); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("quit", gocui.MouseLeft, gocui.ModNone, quitter); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("editor", gocui.KeyEnter, gocui.ModNone,
func(g *gocui.Gui, v *gocui.View) error {
vb := v.ViewBuffer()
stringer := strings.NewReader(vb)
buf := new(strings.Builder)
_, err := io.Copy(buf, stringer)
if err != nil {
return err
}
strings.TrimRight(buf.String(), "\n")
cmd := exec.Command("notify-send", strings.TrimRight(buf.String(), "\n"))
cmd.Run()
return nil
}); err != nil {
return err
}
return nil
}