From 91ee0568cab19f319f507e8b84f28d93fca55187 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 12 Apr 2023 14:20:09 +0100 Subject: [PATCH] Write root menu to writer (not stdout) in engine loop --- go/engine/loop.go | 12 +++++++----- go/engine/loop_test.go | 33 +++++++++++++++++++++++++++++++++ go/render/page.go | 3 +-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/go/engine/loop.go b/go/engine/loop.go index 88eb137..c3ba79e 100644 --- a/go/engine/loop.go +++ b/go/engine/loop.go @@ -2,7 +2,7 @@ package engine import ( "bufio" - "bytes" +// "bytes" "context" "fmt" "io" @@ -16,9 +16,11 @@ func Loop(en *Engine, startSym string, ctx context.Context, reader io.Reader, wr return fmt.Errorf("cannot init: %v\n", err) } - b := bytes.NewBuffer(nil) - en.WriteResult(b, ctx) - fmt.Println(b.String()) + //b := bytes.NewBuffer(nil) + //en.WriteResult(b, ctx) + en.WriteResult(writer, ctx) + writer.Write([]byte{0x0a}) + //fmt.Println(b.String()) running := true bufReader := bufio.NewReader(reader) @@ -36,12 +38,12 @@ func Loop(en *Engine, startSym string, ctx context.Context, reader io.Reader, wr if err != nil { return fmt.Errorf("unexpected termination: %v\n", err) } - //b := bytes.NewBuffer(nil) err = en.WriteResult(writer, ctx) if err != nil { return err } writer.Write([]byte{0x0a}) + } return nil } diff --git a/go/engine/loop_test.go b/go/engine/loop_test.go index 682c2ab..fd8c21a 100644 --- a/go/engine/loop_test.go +++ b/go/engine/loop_test.go @@ -13,6 +13,39 @@ import ( "git.defalsify.org/festive/state" ) +func TestLoopTop(t *testing.T) { + generateTestData(t) + ctx := context.TODO() + st := state.NewState(0) + rs := resource.NewFsResource(dataDir) + ca := cache.NewCache().WithCacheSize(1024) + + en := NewEngine(Config{}, &st, &rs, ca) + err := en.Init("root", ctx) + if err != nil { + t.Fatal(err) + } + + input := []string{ + "2", + "j", + "1", + } + inputStr := strings.Join(input, "\n") + inputBuf := bytes.NewBuffer(append([]byte(inputStr), 0x0a)) + outputBuf := bytes.NewBuffer(nil) + log.Printf("running with input: %s", inputBuf.Bytes()) + + err = Loop(&en, "root", ctx, inputBuf, outputBuf) + if err != nil { + t.Fatal(err) + } + location, _ := st.Where() + if location != "foo" { + fmt.Errorf("expected location 'foo', got %s", location) + } +} + func TestLoopBackForth(t *testing.T) { generateTestData(t) ctx := context.TODO() diff --git a/go/render/page.go b/go/render/page.go index 9c0396a..37e2675 100644 --- a/go/render/page.go +++ b/go/render/page.go @@ -60,11 +60,9 @@ func(pg *Page) Usage() (uint32, uint32, error) { } r := uint32(l) rsv := uint32(c)-r - log.Printf("size before %v %v", r, c) if pg.menu != nil { r += uint32(pg.menu.ReservedSize()) } - log.Printf("size after %v %v", r, c) return r, rsv, nil } @@ -149,6 +147,7 @@ func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16) return "", err } + b := bytes.NewBuffer([]byte{}) err = tp.Execute(b, values) if err != nil {