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:
parent
8acdc82081
commit
01e9e7e2db
|
@ -1,14 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jroimartin/gocui"
|
"github.com/jroimartin/gocui"
|
||||||
)
|
)
|
||||||
|
|
||||||
var input chan int
|
var input chan string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
g, err := gocui.NewGui(gocui.OutputNormal)
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
||||||
|
@ -24,17 +28,12 @@ func main() {
|
||||||
g.Mouse = true
|
g.Mouse = true
|
||||||
g.Cursor = true
|
g.Cursor = true
|
||||||
|
|
||||||
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
initKeybindings(g)
|
||||||
log.Panicln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := g.SetKeybinding("WOW", gocui.MouseLeft, gocui.ModNone, quit); err != nil {
|
|
||||||
log.Panicln(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var numToIncrease int
|
var numToIncrease int
|
||||||
|
@ -48,7 +47,7 @@ func counter() {
|
||||||
|
|
||||||
func layout(g *gocui.Gui) error {
|
func layout(g *gocui.Gui) error {
|
||||||
maxX, maxY := g.Size()
|
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 {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -56,39 +55,72 @@ func layout(g *gocui.Gui) error {
|
||||||
fmt.Fprintln(v, "Hello!")
|
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 {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := g.SetCurrentView("anotherone"); err != nil {
|
if _, err := g.SetCurrentView("editor"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
v.Title = "Editing"
|
v.Title = "Editing"
|
||||||
v.Editable = true
|
v.Editable = true
|
||||||
v.Wrap = 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 {
|
if err != gocui.ErrUnknownView {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// var number int
|
v.Title = "Quit button"
|
||||||
// go func() {
|
|
||||||
// number = <-input
|
|
||||||
// }()
|
|
||||||
// fmt.Fprintln(v, number)
|
|
||||||
|
|
||||||
fmt.Fprintln(v, "You should quit... NOW!", numToIncrease)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func quit(g *gocui.Gui, v *gocui.View) error {
|
func quitter(g *gocui.Gui, v *gocui.View) error {
|
||||||
return gocui.ErrQuit
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue