Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hof/tui: cleanup, better exit, prep for save/load of dashboards #334

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions lib/tui/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"sync"

"golang.org/x/crypto/ssh/terminal"
// "golang.org/x/crypto/ssh/terminal"

"github.com/hofstadter-io/hof/lib/tui/events"
"github.com/hofstadter-io/hof/lib/tui/tview"
Expand Down Expand Up @@ -90,19 +90,19 @@ func (app *App) Start(context map[string]any) error {
}
}()

oldState, err := terminal.MakeRaw(0)
if err != nil {
return err
}
defer terminal.Restore(0, oldState)
//oldState, err := terminal.MakeRaw(0)
//if err != nil {
// return err
//}
//defer terminal.Restore(0, oldState)

// start the event engine
go app.EventBus.Start()

// set the initial view
err = app.rootView.Mount(context)
err := app.rootView.Mount(context)
if err != nil {
panic(err)
return err
}

app.SetRoot(app.rootView, true)
Expand All @@ -122,6 +122,10 @@ func (app *App) stop() error {
return nil
}

func (app *App) DoStop() {
app.stop()
}

func (app *App) Clear() {
screen := app.Screen()
if screen != nil {
Expand Down
4 changes: 1 addition & 3 deletions lib/tui/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Clear() {
}

func Stop() {
globalApp.Stop()
globalApp.DoStop()
}

func GetRootView() tview.Primitive {
Expand Down Expand Up @@ -63,8 +63,6 @@ func SetFocus(p tview.Primitive) {
}

func Unfocus() {
//appLock.Lock()
//defer appLock.Unlock()

if globalApp == nil {
// really shouldn't get here, but the event stream is still running
Expand Down
1 change: 1 addition & 0 deletions lib/tui/modules/eval/argparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func enrichContext(context map[string]any) (map[string]any) {
// top-level eval commands
//
case
"preview",
"save",
"list",
"show",
Expand Down
87 changes: 55 additions & 32 deletions lib/tui/modules/eval/persist.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import (
"os"
"path/filepath"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/format"
"gopkg.in/yaml.v3"

"github.com/hofstadter-io/hof/lib/tui"
"github.com/hofstadter-io/hof/lib/tui/components/cue/browser"
"github.com/hofstadter-io/hof/lib/tui/components/cue/helpers"
"github.com/hofstadter-io/hof/lib/tui/components/panel"
"github.com/hofstadter-io/hof/lib/tui/components/widget"
)

var persist_debug = true

const evalSaveDirSubdir = "tui/saves/eval"

func evalSavePath(filename string) string {
configDir, _ := os.UserConfigDir()
return filepath.Join(configDir,"hof",evalSaveDirSubdir, filename)
}

func (M *Eval) Save(filename string) error {
func (M *Eval) Save(destination string, preview bool) error {

//
// encode and marshal the dashboard
Expand All @@ -31,43 +34,63 @@ func (M *Eval) Save(filename string) error {
return err
}

b, err := yaml.Marshal(m)
if err != nil {
return err
}
ctx := cuecontext.New()
v := ctx.CompileString("{}")
v = v.FillPath(cue.ParsePath(""), m)

// save location
savename := evalSavePath(filename)
//b, err := yaml.Marshal(m)
//if err != nil {
// return err
//}

// ensure the dir exists
dir := filepath.Dir(savename)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}

// write our dashboard out
err = os.WriteFile(savename, b, 0644)
if err != nil {
return err
}

//
// alert the user in several ways
//
if preview {

info := fmt.Sprintf("%s saved to ... %s", M.Name(), savename)
tui.Tell("info", info)
tui.Log("info", info)
cfg := &helpers.SourceConfig{Value: v, Source: helpers.EvalNone}
t := browser.New(cfg, "cue")
t.Rebuild()

// extra to display the save info
if persist_debug {
t := widget.NewTextView()
t.SetDynamicColors(false)
fmt.Fprint(t, string(b))
//t := widget.NewTextView()
//t.SetDynamicColors(false)
//fmt.Fprint(t, string(b))
I := panel.NewBaseItem(nil, M.Panel)
I.SetWidget(t)
M.AddItem(I, 0, 1, true)

} else {
// save location
savename := evalSavePath(destination)

opts := []cue.Option{
cue.Final(),
}
syn := v.Syntax(opts...)

b, err := format.Node(syn)
if err != nil {
return err
}

// ensure the dir exists
dir := filepath.Dir(savename)
err = os.MkdirAll(dir, 0755)
if err != nil {
return err
}

// write our dashboard out
err = os.WriteFile(savename, b, 0644)
if err != nil {
return err
}

//
// alert the user in several ways
//

info := fmt.Sprintf("%s saved to ... %s", M.Name(), savename)
tui.Tell("info", info)
tui.Log("info", info)
}

return nil
Expand Down
27 changes: 21 additions & 6 deletions lib/tui/modules/eval/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eval
import (
"fmt"
"reflect"
"strings"

"github.com/hofstadter-io/hof/lib/tui"
"github.com/hofstadter-io/hof/lib/tui/components/cue/browser"
Expand Down Expand Up @@ -34,6 +35,9 @@ func (M *Eval) Refresh(context map[string]any) error {
if _, ok := context["item"]; ok {
context["action"] = "update"
} else {
err := fmt.Errorf("unknown command: %q {%s}", action, strings.Join(args, ","))
tui.Tell("error", err)
tui.Log("error", err)
return nil
}
}
Expand Down Expand Up @@ -72,13 +76,17 @@ func (M *Eval) Refresh(context map[string]any) error {
return nil
}

if !handled {
err = fmt.Errorf("unhandled inputs: %v %v", action, args)
tui.Tell("crit", err)
tui.Log("error", err)
}

p.Refresh(context)

// tui.SetFocus(p)
// tui.Draw()

//err = fmt.Errorf("unhandled inputs: %v %v %v", action, args, context)
//tui.Log("warn", err)
return nil

return err
Expand All @@ -89,23 +97,30 @@ func (M *Eval) handleDashActions(p *panel.Panel, action string, args []string, c
handled := true

switch action {
case "preview":
if len(args) < 1 {
err = fmt.Errorf("missing argument to preview")
} else {
err = M.Save(args[0], true)
}

case "save":
if len(args) < 1 {
err = fmt.Errorf("missing filename")
err = fmt.Errorf("missing argument to save")
} else {
err = M.Save(args[0])
err = M.Save(args[0], false)
}

case "load":
if len(args) < 1 {
err = fmt.Errorf("missing filename")
err = fmt.Errorf("missing argument to load")
} else {
_, err = M.LoadEval(args[0])
}

case "show":
if len(args) < 1 {
err = fmt.Errorf("missing filename")
err = fmt.Errorf("missing argument to show")
} else {
_, err = M.ShowEval(args[0])
}
Expand Down
8 changes: 7 additions & 1 deletion lib/tui/modules/root/rootview.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func (V *RootView) Connect(C connector.Connector) {
Usage: "q",
Help: "quit hof",
Callback: func(context map[string]any) {
go tui.Stop()
tui.Log("trace", "got here") // turns out we don't, so we have a havk below and tech debt to figure out here
tui.Stop()
},
})

Expand Down Expand Up @@ -137,6 +138,11 @@ func (V *RootView) tryPageChange(context map[string]any) error {
return fmt.Errorf("missing page parameter in: %v", context)
}

if page == "q" {
tui.Stop()
return nil
}

// don't need to navigate
if page == V.currPage {
return nil
Expand Down