WIP add menu render to sizer context

This commit is contained in:
lash 2023-04-10 17:38:23 +01:00
parent 5f04698d0a
commit ca4e9e963a
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 59 additions and 17 deletions

View File

@ -52,6 +52,11 @@ func(m *Menu) WithOutputSize(outputSize uint16) *Menu {
return m return m
} }
// GetOutputSize returns the defined heuristic menu size.
func(m *Menu) GetOutputSize() uint32 {
return uint32(m.outputSize)
}
// WithBrowseConfig defines the criteria for page browsing. // WithBrowseConfig defines the criteria for page browsing.
func(m *Menu) WithBrowseConfig(cfg BrowseConfig) *Menu { func(m *Menu) WithBrowseConfig(cfg BrowseConfig) *Menu {
m.browse = cfg m.browse = cfg

View File

@ -95,7 +95,7 @@ func(pg *Page) Map(key string) error {
return err return err
} }
} }
log.Printf("page map is now: %v", pg.cacheMap) log.Printf("mapped %s", key)
return nil return nil
} }
@ -192,6 +192,15 @@ func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[st
return nil, err return nil, err
} }
if pg.menu != nil {
r, err := pg.menu.Render(idx)
if err != nil {
return nil, err
}
log.Printf("appending %s for menu", r)
s += r
}
remaining, ok := pg.sizer.Check(s) remaining, ok := pg.sizer.Check(s)
if !ok { if !ok {
return nil, fmt.Errorf("capacity exceeded") return nil, fmt.Errorf("capacity exceeded")
@ -255,12 +264,6 @@ func(pg *Page) render(sym string, values map[string]string, idx uint16) (string,
} }
log.Printf("rendered %v bytes for template", len(s)) log.Printf("rendered %v bytes for template", len(s))
r += s r += s
if pg.sizer != nil {
_, ok = pg.sizer.Check(r)
if !ok {
return "", fmt.Errorf("limit exceeded: %v", pg.sizer)
}
}
s, err = pg.menu.Render(idx) s, err = pg.menu.Render(idx)
if err != nil { if err != nil {
return "", err return "", err
@ -275,6 +278,20 @@ func(pg *Page) render(sym string, values map[string]string, idx uint16) (string,
return "", fmt.Errorf("limit exceeded: %v", pg.sizer) return "", fmt.Errorf("limit exceeded: %v", pg.sizer)
} }
} }
// s, err = pg.menu.Render(idx)
// if err != nil {
// return "", err
// }
// log.Printf("rendered %v bytes for menu", len(s))
// if len(s) > 0 {
// r += "\n" + s
// }
// if pg.sizer != nil {
// _, ok = pg.sizer.Check(r)
// if !ok {
// return "", fmt.Errorf("limit exceeded: %v", pg.sizer)
// }
// }
return r, nil return r, nil
} }

View File

@ -88,17 +88,34 @@ func TestSizeLimit(t *testing.T) {
pg := NewPage(ca, rs).WithMenu(mn).WithSizer(szr) pg := NewPage(ca, rs).WithMenu(mn).WithSizer(szr)
ca.Push() ca.Push()
st.Down("test") st.Down("test")
ca.Add("foo", "inky", 4) err := ca.Add("foo", "inky", 4)
ca.Add("bar", "pinky", 10) if err != nil {
ca.Add("baz", "blinky", 0) t.Fatal(err)
pg.Map("foo") }
pg.Map("bar") err = ca.Add("bar", "pinky", 10)
pg.Map("baz") if err != nil {
t.Fatal(err)
}
err = ca.Add("baz", "blinky", 0)
if err != nil {
t.Fatal(err)
}
err = pg.Map("foo")
if err != nil {
t.Fatal(err)
}
err = pg.Map("bar")
if err != nil {
t.Fatal(err)
}
err = pg.Map("baz")
if err != nil {
t.Fatal(err)
}
mn.Put("1", "foo the foo") mn.Put("1", "foo the foo")
mn.Put("2", "go to bar") mn.Put("2", "go to bar")
var err error
_, err = pg.Render("small", 0) _, err = pg.Render("small", 0)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -110,11 +110,11 @@ func baz() error {
func long() error { func long() error {
b := []byte{} b := []byte{}
b = vm.NewLine(b, vm.LOAD, []string{"longdata"}, []byte{0x00}, nil)
b = vm.NewLine(b, vm.MAP, []string{"longdata"}, nil, nil)
b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil) b = vm.NewLine(b, vm.MOUT, []string{"0", "back"}, nil, nil)
b = vm.NewLine(b, vm.MNEXT, []string{"00", "nexxt"}, nil, nil) b = vm.NewLine(b, vm.MNEXT, []string{"00", "nexxt"}, nil, nil)
b = vm.NewLine(b, vm.MPREV, []string{"11", "prevv"}, nil, nil) b = vm.NewLine(b, vm.MPREV, []string{"11", "prevv"}, nil, nil)
b = vm.NewLine(b, vm.LOAD, []string{"longdata"}, []byte{0x00}, nil)
b = vm.NewLine(b, vm.MAP, []string{"longdata"}, nil, nil)
b = vm.NewLine(b, vm.HALT, nil, nil, nil) b = vm.NewLine(b, vm.HALT, nil, nil, nil)
b = vm.NewLine(b, vm.INCMP, []string{"0", "_"}, nil, nil) b = vm.NewLine(b, vm.INCMP, []string{"0", "_"}, nil, nil)

View File

@ -178,6 +178,7 @@ func(vm *Vm) RunCatch(b []byte, ctx context.Context) ([]byte, error) {
if r { if r {
log.Printf("catch at flag %v, moving to %v", sig, sym) //bitField, d) log.Printf("catch at flag %v, moving to %v", sig, sym) //bitField, d)
vm.st.Down(sym) vm.st.Down(sym)
vm.Reset()
b = []byte{} b = []byte{}
} }
return b, nil return b, nil
@ -195,6 +196,7 @@ func(vm *Vm) RunCroak(b []byte, ctx context.Context) ([]byte, error) {
} }
if r { if r {
log.Printf("croak at flag %v, purging and moving to top", sig) log.Printf("croak at flag %v, purging and moving to top", sig)
vm.Reset()
vm.st.Reset() vm.st.Reset()
vm.pg.Reset() vm.pg.Reset()
vm.ca.Reset() vm.ca.Reset()
@ -258,6 +260,7 @@ func(vm *Vm) RunMove(b []byte, ctx context.Context) ([]byte, error) {
} }
log.Printf("loaded additional code: %x", code) log.Printf("loaded additional code: %x", code)
b = append(b, code...) b = append(b, code...)
vm.Reset()
return b, nil return b, nil
} }
@ -313,6 +316,7 @@ func(vm *Vm) RunInCmp(b []byte, ctx context.Context) ([]byte, error) {
if err != nil { if err != nil {
return b, err return b, err
} }
vm.Reset()
code, err := vm.rs.GetCode(target) code, err := vm.rs.GetCode(target)
if err != nil { if err != nil {
@ -392,7 +396,6 @@ func(vm *Vm) Render() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
vm.Reset()
return r, nil return r, nil
} }