From 66c70d5b3199c9853a90d298e26c197860490ba0 Mon Sep 17 00:00:00 2001 From: surdeus Date: Tue, 23 Jul 2024 17:41:31 +0500 Subject: [PATCH] feat: delete exceed gowitharg shit. --- command.go | 2 +- context.go | 29 +++++++++++++++-------------- go.go | 4 ++-- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/command.go b/command.go index 9f3b5a9..213c52e 100644 --- a/command.go +++ b/command.go @@ -161,7 +161,7 @@ func (compo *CommandCompo) Serve(c Context) { // Closing current widget cmdUpdates.Close() // And running the other one. - cmdUpdates, _ = c.runWidget(cmd.Widget, cmd.WidgetArg) + cmdUpdates, _ = c.WithArg(cmd.WidgetArg).RunWidget(cmd.Widget) } continue } diff --git a/context.go b/context.go index 23a392f..39af4d9 100644 --- a/context.go +++ b/context.go @@ -220,7 +220,7 @@ func (c Context) ReadFile(fileID FileID) ([]byte, string, error) { return bts, pth, nil } -func (c Context) runCompo(compo Component, arg any) (*UpdateChan, error) { +func (c Context) RunCompo(compo Component) (*UpdateChan, error) { if compo == nil { return nil, nil } @@ -235,8 +235,7 @@ func (c Context) runCompo(compo Component, arg any) (*UpdateChan, error) { updates := NewUpdateChan() go func() { compo.Serve( - c.WithInput(updates). - WithArg(arg), + c.WithInput(updates), ) // To let widgets finish themselves before // the channel is closed and close it by themselves. @@ -246,20 +245,20 @@ func (c Context) runCompo(compo Component, arg any) (*UpdateChan, error) { } // Run widget in background returning the new input channel for it. -func (c Context) runWidget(widget Widget, arg any) (*UpdateChan, error) { +func (c Context) RunWidget(widget Widget) (*UpdateChan, error) { var err error if widget == nil { return nil, EmptyWidgetErr } - compos := widget.Render(c.WithArg(arg)) + compos := widget.Render(c) // Leave if changed path or components are empty. if compos == nil { return nil, EmptyCompoErr } chns := make([]*UpdateChan, len(compos)) for i, compo := range compos { - chns[i], err = c.runCompo(compo, arg) + chns[i], err = c.RunCompo(compo) if err != nil { for _, chn := range chns { chn.Close() @@ -299,14 +298,16 @@ func (c Context) runWidget(widget Widget, arg any) (*UpdateChan, error) { return ret, nil } -// Simple go without an argument for context. -func (c Context) Go(pth Widget) error { - return c.GoWithArg(pth, nil) +func (c Context) GoRet(pth Widget) WidgetGo { + return WidgetGo{ + Path: pth, + Arg: c.Arg(), + } } -// Go to the specified widget with the -// specificed argument. -func (c Context) GoWithArg(pth Widget, arg any) error { +// Go to the specified widget +// using context values. +func (c Context) Go(pth Widget) error { var err error if pth == nil { c.session.pathHistory = []Widget{} @@ -316,7 +317,7 @@ func (c Context) GoWithArg(pth Widget, arg any) error { var back bool if pth == Back { if len(c.session.pathHistory) < 2 { - return c.GoWithArg(nil, arg) + return c.Go(nil) } pth = c.session.pathHistory[len(c.session.pathHistory)-2] c.session.pathHistory = @@ -332,7 +333,7 @@ func (c Context) GoWithArg(pth Widget, arg any) error { c.session.skippedUpdates.Close() // Running the new one. - c.session.skippedUpdates, err = c.runWidget(pth, arg) + c.session.skippedUpdates, err = c.RunWidget(pth) if err != nil { return err } diff --git a/go.go b/go.go index d38b96d..c2354a4 100644 --- a/go.go +++ b/go.go @@ -14,8 +14,8 @@ type WidgetGo struct { Arg any } -func (sc WidgetGo) Act(c Context) { - c.GoWithArg(sc.Path, sc.Arg) +func (w WidgetGo) Act(c Context) { + c.WithArg(w.Arg).Go(w.Path) } // Implementing the Server interface.