WIP Add indexed condition for browse menu items display

This commit is contained in:
lash 2023-04-08 11:46:30 +01:00
parent a7add69f18
commit 2b34149aa9
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 49 additions and 11 deletions

View File

@ -114,7 +114,7 @@ func(en *Engine) WriteResult(w io.Writer) error {
if err != nil { if err != nil {
return err return err
} }
m, err := en.rs.RenderMenu() m, err := en.rs.RenderMenu(idx)
if err != nil { if err != nil {
return err return err
} }

View File

@ -20,7 +20,7 @@ type Resource interface {
PutMenu(string, string) error // Add a menu item. PutMenu(string, string) error // Add a menu item.
SetMenuBrowse(string, string, bool) error // Set menu browser display details. 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. 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. 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. 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. // 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 { if m.canNext {
err := m.PutMenu(m.next[0], m.next[1]) err := m.PutMenu(m.next[0], m.next[1])
if err != nil { if err != nil {
@ -123,8 +133,8 @@ func(m *MenuResource) applyPage() error {
// RenderMenu returns the full current state of the menu as a string. // 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. // After this has been executed, the state of the menu will be empty.
func(m *MenuResource) RenderMenu() (string, error) { func(m *MenuResource) RenderMenu(idx uint16) (string, error) {
err := m.applyPage() err := m.applyPage(idx)
if err != nil { if err != nil {
return "", err 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) return "", fmt.Errorf("limit exceeded: %v", sizer)
} }
} }
s, err = m.RenderMenu() s, err = m.RenderMenu(idx)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -13,7 +13,7 @@ func TestStateResourceInit(t *testing.T) {
_ = NewStateResource(&st) _ = NewStateResource(&st)
} }
func TestStateBrowse(t *testing.T) { func TestStateBrowseNoSink(t *testing.T) {
st := state.NewState(0) st := state.NewState(0)
st.Down("root") st.Down("root")
@ -28,7 +28,35 @@ func TestStateBrowse(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) 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 { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -58,7 +86,7 @@ func TestStateBrowse(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
s, err = rs.RenderMenu() s, err = rs.RenderMenu(idx)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -319,7 +319,7 @@ func TestRunMenu(t *testing.T) {
t.Errorf("expected empty remainder, got length %v: %v", l, b) t.Errorf("expected empty remainder, got length %v: %v", l, b)
} }
r, err := rs.RenderMenu() r, err := rs.RenderMenu(0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -351,7 +351,7 @@ func TestRunMenuBrowse(t *testing.T) {
t.Errorf("expected empty remainder, got length %v: %v", l, b) t.Errorf("expected empty remainder, got length %v: %v", l, b)
} }
r, err := rs.RenderMenu() r, err := rs.RenderMenu(0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }