golang-exercises/gocui-practice/main.go

90 lines
1.6 KiB
Go
Raw Normal View History

2024-04-04 09:39:37 +02:00
package main
import (
"fmt"
"log"
2024-04-06 10:14:33 +02:00
"time"
2024-04-04 09:39:37 +02:00
"github.com/jroimartin/gocui"
)
2024-04-06 10:14:33 +02:00
var input chan int
2024-04-04 09:39:37 +02:00
func main() {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.SetManagerFunc(layout)
2024-04-06 10:14:33 +02:00
go counter()
g.Mouse = true
2024-04-04 09:39:37 +02:00
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
2024-04-06 10:14:33 +02:00
if err := g.SetKeybinding("WOW", gocui.MouseLeft, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
2024-04-04 09:39:37 +02:00
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
2024-04-06 10:14:33 +02:00
var numToIncrease int
func counter() {
for {
numToIncrease++
time.Sleep(time.Second)
}
}
2024-04-04 09:39:37 +02:00
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
2024-04-06 10:14:33 +02:00
if v, err := g.SetView("hello", maxX/2-20, maxY/2-18, maxX/2+20, maxY/2-15); err != nil {
2024-04-04 09:39:37 +02:00
if err != gocui.ErrUnknownView {
return err
}
fmt.Fprintln(v, "This is a reaaaaaaaally long message")
fmt.Fprintln(v, "Hello!")
2024-04-04 09:39:37 +02:00
}
2024-04-06 10:14:33 +02:00
if v, err := g.SetView("anotherone", maxX/2-20, maxY/2-13, maxX/2+20, maxY/2-6); err != nil {
if err != gocui.ErrUnknownView {
return err
}
2024-04-06 10:14:33 +02:00
for i := 0; i < 5; i++ {
fmt.Fprintln(v, "Hola!")
}
}
2024-04-06 10:14:33 +02:00
if v, err := g.SetView("WOW", 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)
fmt.Fprintln(v, "You should quit... NOW!", numToIncrease)
}
// if err := g.SetKeybinding("WOW", gocui.MouseLeft, gocui.ModNone, fcn); err != nil {
// fmt.Fprintln(v, "Clicked")
// }
2024-04-04 09:39:37 +02:00
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}