Skip to content

Commit

Permalink
hof/tui: rip out router business, no need for this extra complexity (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
verdverm authored Sep 10, 2023
1 parent ad61850 commit ca071ed
Show file tree
Hide file tree
Showing 32 changed files with 264 additions and 4,858 deletions.
24 changes: 15 additions & 9 deletions lib/tui/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"sync"

"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 @@ -61,20 +63,16 @@ func (app *App) SetLastFocus(last tview.Primitive) {
app.lastFocus = last
}

//func (A *App) AddPage(name string, item tview.Primitive, resize, visible bool ) {
// A.Pages.AddPage(name, item, resize, visible)
//}

// blocking call
func (app *App) Start() error {

func (app *App) Start(context map[string]any) error {
// stuff to ensure we don't mess up the user's terminal
// catch panics, clean up, format error
defer func() {
e := recover()
if e != nil {
app.stop()
// Print a formatted panic output
fmt.Fprintf(os.Stderr, "Captured a panic(value=%v) lib.Start()... Exit vermui and clean terminal...\nPrint stack trace:\n\n", e)
fmt.Fprintf(os.Stderr, "Captured a panic(value=%v) lib.Start()... Exiting and cleaning terminal...\nPrint stack trace:\n\n", e)
//debug.PrintStack()
//gs, err := stack.ParseDump(bytes.NewReader(debug.Stack()), os.Stderr)
//if err != nil {
Expand All @@ -92,16 +90,24 @@ func (app *App) Start() error {
}
}()

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

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

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

// blocking
app.SetRoot(app.rootView, true)

// blocking
return app.Run()
}

Expand Down
13 changes: 2 additions & 11 deletions lib/tui/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"strconv"
"time"

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

"github.com/hofstadter-io/hof/cmd/hof/flags"
"github.com/hofstadter-io/hof/lib/tui"
"github.com/hofstadter-io/hof/lib/tui/app"
Expand All @@ -20,12 +18,6 @@ import (
)

func Cmd(args []string, rflags flags.RootPflagpole) error {
// stuff to ensure we don't mess up the user's terminal
//oldState, err := terminal.MakeRaw(0)
//if err != nil {
// return err
//}
//defer terminal.Restore(0, oldState)

// setup new app
App, err := app.NewApp()
Expand Down Expand Up @@ -89,7 +81,7 @@ func Cmd(args []string, rflags flags.RootPflagpole) error {
}

context := map[string]any{
"path": path,
"page": path,
"args": args,
}

Expand All @@ -98,12 +90,11 @@ func Cmd(args []string, rflags flags.RootPflagpole) error {
go func() {
// some latent locksups occur randomly
time.Sleep(time.Millisecond * 23)
tui.SendCustomEvent("/router/dispatch", context)
tui.SendCustomEvent("/status/message", "[dodgerblue::b]Welcome to [gold::bi]_[ivory]Hofstadter[-::-]")
}()

// Start the Main (Blocking) Loop
return App.Start()
return App.Start(context)
}

func logKeys() {
Expand Down
6 changes: 2 additions & 4 deletions lib/tui/components/cue/helpers/source-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"time"

"cuelang.org/go/cue"

"github.com/hofstadter-io/hof/lib/tui"
)

type EvalSource string
Expand Down Expand Up @@ -45,7 +43,7 @@ func (sc SourceConfig) Encode() (map[string]any, error) {


func (sc SourceConfig) GetValue() (cue.Value, error) {
tui.Log("debug", fmt.Sprintf("SCFG.GetValue %# v", sc))
// tui.Log("debug", fmt.Sprintf("SCFG.GetValue %# v", sc))

switch sc.Source {
case EvalNone:
Expand Down Expand Up @@ -85,7 +83,7 @@ func (sc SourceConfig) GetValue() (cue.Value, error) {
}

func (sc SourceConfig) GetText() (string, error) {
tui.Log("debug", fmt.Sprintf("SCFG.GetText %# v", sc))
// tui.Log("debug", fmt.Sprintf("SCFG.GetText %# v", sc))
switch sc.Source {
case EvalNone:
return sc.Text, nil
Expand Down
18 changes: 5 additions & 13 deletions lib/tui/components/cue/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ func (V *Playground) Encode() (map[string]any, error) {
}


func New(initialText string, scopeSourceConfig helpers.SourceConfig) (*Playground) {
func New(initialText string) (*Playground) {

C := &Playground{
Flex: tview.NewFlex(),
text: initialText,
scope: &valPack{
config: scopeSourceConfig,
},
scope: &valPack{},
final: &valPack{},
}
// our wrapper around the CUE widgets
Expand All @@ -83,7 +81,7 @@ func New(initialText string, scopeSourceConfig helpers.SourceConfig) (*Playgroun
// TODO, options form

// scope viewer
C.scope.viewer = browser.New(C.scope.config, "cue")
C.scope.viewer = browser.New(helpers.SourceConfig{}, "cue")
C.scope.viewer.SetName("scope")
C.scope.viewer.SetBorder(true)

Expand All @@ -101,14 +99,8 @@ func New(initialText string, scopeSourceConfig helpers.SourceConfig) (*Playgroun
C.final.viewer.SetBorder(true)

// usingScope?
C.final.viewer.SetUsingScope(true)
C.useScope = true
//if sourceConfig.Source != helpers.EvalNone {

//} else {
// // add empty scope box
// C.Flex.AddItem(nil, 0, 0, false)
//}
C.final.viewer.SetUsingScope(false)
C.useScope = false

// layout
C.Flex.
Expand Down
72 changes: 69 additions & 3 deletions lib/tui/components/cue/playground/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func (C *Playground) HandleAction(action string, args []string, context map[string]any) (bool, error) {
tui.Log("warn", fmt.Sprintf("Playground.HandleAction: %v %v", action, args))
tui.Log("warn", fmt.Sprintf("Playground.HandleAction: %v %v %v", action, args, context))
var err error
handled := true

Expand Down Expand Up @@ -60,6 +60,8 @@ func (C *Playground) HandleAction(action string, args []string, context map[stri
}
}

case "update":
err = C.updateFromArgsAndContext(args, context)

default:
err = fmt.Errorf("unknown command %q", action)
Expand All @@ -68,8 +70,73 @@ func (C *Playground) HandleAction(action string, args []string, context map[stri
return handled, err
}

func (C *Playground) updateFromArgsAndContext(args[] string, context map[string]any) error {
// get source, defaults to empty, new runtime?
source := ""
if _source, ok := context["source"]; ok {
source = _source.(string)
}

target := "value"
if _target, ok := context["target"]; ok {
target = _target.(string)
}

// setup our source config
srcCfg := helpers.SourceConfig{
Args: args,
}

// special case, source will be empty when the args are all cue entrypoints
// we want to...
// (1) catch special empty case for new play
// (2) we want different defaults for empty when there are args, based on the target
// for (1), we need temporary <new-play> to know we are in new play mode
if len(args) == 0 || (len(args) == 1 && args[0] == "new") {
source = "<new-play>" // very temporary setting
target = "value"
}

rebuildScope := false
switch target {
case "value":
// local source default, assume it was a filename
if source == "" {
source = "file"
} else if source == "<new-play>" {
source = ""
}
srcCfg.Source = helpers.EvalSource(source)

// tui.Log("extra", fmt.Sprintf("Eval.playItem.value: %v", srcCfg ))
C.UseScope(false)
// need to get the text once at startup
txt, err := srcCfg.GetText()
if err != nil {
tui.Log("error", err)
return err
}
// tui.Log("extra", fmt.Sprintf("Eval.playItem.value.text: %v", txt ))
C.SetText(txt)

case "scope":
if source == "" {
source = "runtime"
}
srcCfg.Source = helpers.EvalSource(source)

C.SetScopeConfig(srcCfg)

rebuildScope = true
C.UseScope(true)
}

return C.Rebuild(rebuildScope)
}


func (C *Playground) Rebuild(rebuildScope bool) error {
// tui.Log("info", fmt.Sprintf("Play.rebuildScope %v %v %v", rebuildScope, C.useScope, C.scope.config))
tui.Log("info", fmt.Sprintf("Play.rebuildScope %v %v %v", rebuildScope, C.useScope, C.scope.config))
var (
v cue.Value
err error
Expand Down Expand Up @@ -130,7 +197,6 @@ func (C *Playground) Rebuild(rebuildScope bool) error {
C.SetItem(0, nil, 0, 0, false)
}


// tui.Draw()
return nil
}
Expand Down
4 changes: 0 additions & 4 deletions lib/tui/components/panel/panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ func (P *Panel) Refresh(context map[string]any) error {
P.SetTitle(" "+P.Name()+" ")
}

//case "conn":
// P.makeItemConnection(args, context)


default:
}

Expand Down
19 changes: 14 additions & 5 deletions lib/tui/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func eventMods(mods tcell.ModMask) string {
}

type EventStream struct {
sync.RWMutex
mutex sync.RWMutex
srcMap map[string]chan Event
stream chan Event
wg sync.WaitGroup
Expand All @@ -245,11 +245,12 @@ func (es *EventStream) Init() {
}

func (es *EventStream) Merge(name string, ec chan Event) {
es.Lock()
defer es.Unlock()

es.wg.Add(1)

es.mutex.Lock()
es.srcMap[name] = ec
es.mutex.Unlock()

go func(a chan Event) {
for n := range a {
Expand All @@ -261,22 +262,30 @@ func (es *EventStream) Merge(name string, ec chan Event) {
}

func (es *EventStream) Handle(path string, handler func(Event)) {
es.mutex.Lock()
defer es.mutex.Unlock()
es.Handlers[cleanPath(path)] = handler
}

func (es *EventStream) RemoveHandle(path string) {
es.mutex.Lock()
defer es.mutex.Unlock()
delete(es.Handlers, cleanPath(path))
}

// Remove all existing defined Handlers from the map
func (es *EventStream) ResetHandlers() {
es.mutex.Lock()
defer es.mutex.Unlock()
for Path := range es.Handlers {
delete(es.Handlers, Path)
}
return
}

func (es *EventStream) match(path string) string {
es.mutex.RLock()
defer es.mutex.RUnlock()
return findMatch(es.Handlers, path)
}

Expand All @@ -291,8 +300,8 @@ func (es *EventStream) Loop() {
return
}
func(a Event) {
es.RLock()
defer es.RUnlock()
es.mutex.RLock()
defer es.mutex.RUnlock()
if pattern := es.match(a.Path); pattern != "" {
es.Handlers[pattern](a)
}
Expand Down
4 changes: 4 additions & 0 deletions lib/tui/events/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ type WgtInfo struct {
Handlers map[string]func(Event)
WgtRef tview.Primitive
Id string
Lock *sync.RWMutex
}

func NewWgtInfo(wgt tview.Primitive) WgtInfo {
return WgtInfo{
Handlers: make(map[string]func(Event)),
WgtRef: wgt,
Id: wgt.Id(),
Lock: new(sync.RWMutex),
}
}

Expand Down Expand Up @@ -80,6 +82,8 @@ func GenId() string {
func (wm WgtMgr) WgtHandlersHook() func(Event) {
return func(e Event) {
for _, v := range wm {
v.Lock.RLock()
defer v.Lock.RUnlock()
if k := findMatch(v.Handlers, e.Path); k != "" {
// v.WgtRef.Lock()
v.Handlers[k](e)
Expand Down
Loading

0 comments on commit ca071ed

Please sign in to comment.