You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lntop/ui/ui.go

111 lines
1.9 KiB
Go

5 years ago
package ui
import (
5 years ago
"context"
5 years ago
"github.com/jroimartin/gocui"
5 years ago
"github.com/edouardparis/lntop/app"
"github.com/edouardparis/lntop/ui/views"
5 years ago
)
type Ui struct {
5 years ago
app *app.App
channels *views.Channels
5 years ago
}
func (u *Ui) Run() error {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return err
}
defer g.Close()
5 years ago
g.Cursor = true
g.SetManagerFunc(u.layout)
5 years ago
5 years ago
err = u.setKeyBinding(g)
if err != nil {
5 years ago
return err
}
5 years ago
g.Update(u.Refresh)
5 years ago
err = g.MainLoop()
if err != nil && err != gocui.ErrQuit {
5 years ago
return err
}
5 years ago
return err
}
5 years ago
func (u *Ui) setKeyBinding(g *gocui.Gui) error {
err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit)
if err != nil {
return err
}
err = g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, cursorUp)
if err != nil {
return err
}
err = g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, cursorDown)
if err != nil {
return err
}
return nil
}
func cursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy+1); err != nil {
ox, oy := v.Origin()
if err := v.SetOrigin(ox, oy+1); err != nil {
return err
}
}
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
cx, cy := v.Cursor()
if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 {
if err := v.SetOrigin(ox, oy-1); err != nil {
return err
}
}
}
return nil
}
5 years ago
func (u *Ui) Refresh(g *gocui.Gui) error {
channels, err := u.app.Network.ListChannels(context.Background())
if err != nil {
return err
}
u.channels.Update(channels)
return nil
}
func (u *Ui) layout(g *gocui.Gui) error {
5 years ago
maxX, maxY := g.Size()
5 years ago
return u.channels.Set(g, 0, maxY/8, maxX-1, maxY-1)
5 years ago
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
5 years ago
func New(app *app.App) *Ui {
return &Ui{
5 years ago
app: app,
channels: views.NewChannels(),
}
5 years ago
}