mirror of
https://github.com/edouardparis/lntop
synced 2024-11-16 00:12:44 +00:00
49 lines
777 B
Go
49 lines
777 B
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/jroimartin/gocui"
|
||
|
)
|
||
|
|
||
|
type Ui struct {
|
||
|
}
|
||
|
|
||
|
func (u *Ui) Run() error {
|
||
|
g, err := gocui.NewGui(gocui.OutputNormal)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer g.Close()
|
||
|
|
||
|
g.SetManagerFunc(layout)
|
||
|
|
||
|
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
|
||
|
return err
|
||
|
}
|
||
|
return 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
|
||
|
}
|
||
|
|
||
|
func New() *Ui {
|
||
|
return &Ui{}
|
||
|
}
|