go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit 83a3c6e4eb67f4da6b8d15d55db95790397161f0
parent a1f69bc1058915abcb7752c98721c79acff36d77
Author: lash <dev@holbrook.no>
Date:   Thu, 26 Sep 2024 03:46:09 +0100

Preserve current item length when changing page in render

Diffstat:
Minternal/resourcetest/util.go | 5+++++
Mrender/page.go | 7+++++--
Mrender/size_test.go | 38++++++++++++++++++++++++++++++++++++++
3 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/internal/resourcetest/util.go b/internal/resourcetest/util.go @@ -72,6 +72,11 @@ func(tr *TestResource) AddFunc(ctx context.Context, key string, fn resource.Entr tr.AddLocalFunc(key, fn) } +func(tr *TestResource) AddStatic(ctx context.Context, key string, val string) error { + tr.db.SetPrefix(db.DATATYPE_STATICLOAD) + return tr.db.Put(ctx, []byte(key), []byte(val)) +} + func(tr *TestResource) Lock() { tr.db.SetLock(0, true) } diff --git a/render/page.go b/render/page.go @@ -77,7 +77,10 @@ func(pg *Page) Usage() (uint32, uint32, error) { c += sz } r := uint32(l) - rsv := uint32(c)-r + rsv := uint32(0) + if uint32(c) > r { + rsv = uint32(c)-r + } //if pg.menu != nil { // r += uint32(pg.menu.ReservedSize()) //} @@ -271,7 +274,7 @@ func(pg *Page) joinSink(sinkValues []string, remaining uint32, menuSizes [4]uint c := uint32(rb.Len()) pg.sizer.AddCursor(c) tb.Reset() - l = 0 + l = len(v) if count == 0 { netRemaining -= menuSizes[2] } diff --git a/render/size_test.go b/render/size_test.go @@ -3,6 +3,7 @@ package render import ( "context" "fmt" + "math/rand" "testing" "git.defalsify.org/vise.git/state" @@ -24,6 +25,7 @@ func newTestSizeResource() *testSizeResource { rs.AddTemplate(ctx, "small", "one {{.foo}} two {{.bar}} three {{.baz}}") rs.AddTemplate(ctx, "toobug", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus.") rs.AddTemplate(ctx, "pages", "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}") + rs.AddTemplate(ctx, "transparent", "{{.out}}") rs.AddLocalFunc("foo", get) rs.AddLocalFunc("bar", get) rs.AddLocalFunc("baz", get) @@ -337,4 +339,40 @@ func TestMenuSink(t *testing.T) { } } +func TestMiddlePage(t *testing.T) { + ctx := context.Background() + st := state.NewState(0) + ca := cache.NewCache() + mn := NewMenu().WithBrowseConfig(DefaultBrowseConfig()) + rs := newTestSizeResource() + rs.Lock() + content := "" + for i := 0; i < 42; i++ { + v := rand.Intn(26) + b := make([]byte, 3+(v%3)) + for ii := 0; ii < len(b); ii++ { + b[ii] = uint8(0x41 + v) + v = rand.Intn(26) + } + content += fmt.Sprintf("%d:%s\n", i, string(b)) + } + content = content[:len(content)-1] + + st.Down("test") + + ca.Push() + ca.Add("out", content, 0) + szr := NewSizer(160) + + mn.Put("x", "exit") + mn.Put("q", "quit") + pg := NewPage(ca, rs).WithMenu(mn).WithSizer(szr) + pg.Map("out") + + r, err := pg.Render(ctx, "transparent", 2) + if err != nil { + t.Fatal(err) + } + fmt.Printf("%s\n", r) +}