1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-01-07 04:49:17 +00:00
ctrl/stew/stew.go

92 lines
2.1 KiB
Go
Raw Normal View History

2021-06-30 05:14:04 +00:00
package stew
2021-06-14 04:49:47 +00:00
import (
"flag"
"fmt"
"os"
2021-06-30 06:30:15 +00:00
"github.com/gdamore/tcell/v2"
2021-06-14 04:49:47 +00:00
"github.com/rivo/tview"
)
type Stew struct {
stewardSocket string
}
func NewStew() (*Stew, error) {
stewardSocket := flag.String("stewardSocket", "/usr/local/steward/tmp/steward.sock", "specify the full path of the steward socket file")
flag.Parse()
_, err := os.Stat(*stewardSocket)
if err != nil {
return nil, fmt.Errorf("error: specify the full path to the steward.sock file: %v", err)
}
s := Stew{
stewardSocket: *stewardSocket,
}
return &s, nil
}
type slide struct {
name string
key tcell.Key
primitive tview.Primitive
}
2021-06-14 04:49:47 +00:00
func (s *Stew) Start() error {
2021-06-30 06:30:15 +00:00
pages := tview.NewPages()
2021-06-30 05:14:04 +00:00
app := tview.NewApplication()
2021-06-30 06:30:15 +00:00
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
2021-06-30 10:21:10 +00:00
if event.Key() == tcell.KeyF1 {
pages.SwitchToPage("info")
2021-06-30 06:30:15 +00:00
return nil
2021-06-30 10:21:10 +00:00
} else if event.Key() == tcell.KeyF2 {
2021-06-30 06:30:15 +00:00
pages.SwitchToPage("message")
return nil
}
return event
})
2021-06-30 09:05:10 +00:00
info := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWrap(false)
// The slides to draw, and their name.
// NB: This slice is being looped over further below, to create the menu
// elements. If adding a new slide, make sure that slides are ordererin
// chronological order, so we can auto generate the info menu with it's
// corresponding F key based on the slice index+1.
slides := []slide{
{name: "info", key: tcell.KeyF1, primitive: infoSlide(app)},
{name: "message", key: tcell.KeyF2, primitive: messageSlide(app)},
}
2021-06-23 12:55:39 +00:00
// Add on page for each slide.
for i, v := range slides {
if i == 0 {
pages.AddPage(v.name, v.primitive, true, true)
fmt.Fprintf(info, " F%v:%v ", i+1, v.name)
continue
}
pages.AddPage(v.name, v.primitive, true, false)
fmt.Fprintf(info, " F%v:%v ", i+1, v.name)
}
2021-06-30 05:19:36 +00:00
2021-06-30 06:30:15 +00:00
// Create the main layout.
2021-06-30 09:05:10 +00:00
layout := tview.NewFlex()
//layout.SetBorder(true)
2021-06-30 09:05:10 +00:00
layout.SetDirection(tview.FlexRow).
AddItem(pages, 0, 10, true).
AddItem(info, 1, 1, false)
2021-06-30 06:30:15 +00:00
if err := app.SetRoot(layout, true).EnableMouse(true).Run(); err != nil {
panic(err)
2021-06-14 15:07:31 +00:00
}
2021-06-14 04:49:47 +00:00
return nil
}