commit 2b34149aa9e9c91bdf46d66dc3c0f118b292d74f
parent a7add69f1840a7737618ba76c1989dd45c44b671
Author: lash <dev@holbrook.no>
Date: Sat, 8 Apr 2023 11:46:30 +0100
WIP Add indexed condition for browse menu items display
Diffstat:
4 files changed, 49 insertions(+), 11 deletions(-)
diff --git a/go/engine/engine.go b/go/engine/engine.go
@@ -114,7 +114,7 @@ func(en *Engine) WriteResult(w io.Writer) error {
if err != nil {
return err
}
- m, err := en.rs.RenderMenu()
+ m, err := en.rs.RenderMenu(idx)
if err != nil {
return err
}
diff --git a/go/resource/resource.go b/go/resource/resource.go
@@ -20,7 +20,7 @@ type Resource interface {
PutMenu(string, string) error // Add a menu item.
SetMenuBrowse(string, string, bool) error // Set menu browser display details.
RenderTemplate(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) // Render the given data map using the template of the symbol.
- RenderMenu() (string, error) // Render the current state of menu
+ RenderMenu(idx uint16) (string, error) // Render the current state of menu
Render(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) // Render full output.
FuncFor(sym string) (EntryFunc, error) // Resolve symbol content point for.
}
@@ -104,7 +104,17 @@ func(m *MenuResource) shiftMenu() (string, string, error) {
}
// add available browse options.
-func(m *MenuResource) applyPage() error {
+func(m *MenuResource) applyPage(idx uint16) error {
+ l := uint16(len(m.sinkValues))
+ if l == 0 {
+ return nil
+ }
+ if idx == l - 1 {
+ m.canNext = false
+ }
+ if idx == 0 {
+ m.canPrev = false
+ }
if m.canNext {
err := m.PutMenu(m.next[0], m.next[1])
if err != nil {
@@ -123,8 +133,8 @@ func(m *MenuResource) applyPage() error {
// RenderMenu returns the full current state of the menu as a string.
//
// After this has been executed, the state of the menu will be empty.
-func(m *MenuResource) RenderMenu() (string, error) {
- err := m.applyPage()
+func(m *MenuResource) RenderMenu(idx uint16) (string, error) {
+ err := m.applyPage(idx)
if err != nil {
return "", err
}
@@ -252,7 +262,7 @@ func(m *MenuResource) render(sym string, values map[string]string, idx uint16, s
return "", fmt.Errorf("limit exceeded: %v", sizer)
}
}
- s, err = m.RenderMenu()
+ s, err = m.RenderMenu(idx)
if err != nil {
return "", err
}
diff --git a/go/resource/state_test.go b/go/resource/state_test.go
@@ -13,7 +13,7 @@ func TestStateResourceInit(t *testing.T) {
_ = NewStateResource(&st)
}
-func TestStateBrowse(t *testing.T) {
+func TestStateBrowseNoSink(t *testing.T) {
st := state.NewState(0)
st.Down("root")
@@ -28,7 +28,35 @@ func TestStateBrowse(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- s, err := rs.RenderMenu()
+ s, err := rs.RenderMenu(0)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ expect := `1:foo
+2:bar`
+ if s != expect {
+ t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, s)
+ }
+}
+
+
+func TestStateBrowseSink(t *testing.T) {
+ st := state.NewState(0)
+ st.Down("root")
+
+ rs := NewStateResource(&st)
+ rs.PutMenu("1", "foo")
+ rs.PutMenu("2", "bar")
+ err := rs.SetMenuBrowse("11", "next", false)
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = rs.SetMenuBrowse("22", "prev", true)
+ if err != nil {
+ t.Fatal(err)
+ }
+ s, err := rs.RenderMenu(0)
if err != nil {
t.Fatal(err)
}
@@ -58,7 +86,7 @@ func TestStateBrowse(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- s, err = rs.RenderMenu()
+ s, err = rs.RenderMenu(idx)
if err != nil {
t.Fatal(err)
}
diff --git a/go/vm/runner_test.go b/go/vm/runner_test.go
@@ -319,7 +319,7 @@ func TestRunMenu(t *testing.T) {
t.Errorf("expected empty remainder, got length %v: %v", l, b)
}
- r, err := rs.RenderMenu()
+ r, err := rs.RenderMenu(0)
if err != nil {
t.Fatal(err)
}
@@ -351,7 +351,7 @@ func TestRunMenuBrowse(t *testing.T) {
t.Errorf("expected empty remainder, got length %v: %v", l, b)
}
- r, err := rs.RenderMenu()
+ r, err := rs.RenderMenu(0)
if err != nil {
t.Fatal(err)
}