Newline replacement string for portion render

This commit is contained in:
lash 2023-04-07 11:31:30 +01:00
parent 063b786572
commit a8a6adbe6c
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
9 changed files with 197 additions and 59 deletions

View File

@ -119,12 +119,12 @@ func (en *Engine) Exec(input []byte, ctx context.Context) (bool, error) {
// - the template for the given node point is note available for retrieval using the resource.Resource implementer. // - the template for the given node point is note available for retrieval using the resource.Resource implementer.
// - the supplied writer fails to process the writes. // - the supplied writer fails to process the writes.
func(en *Engine) WriteResult(w io.Writer) error { func(en *Engine) WriteResult(w io.Writer) error {
location := en.st.Where() location, idx := en.st.Where()
v, err := en.st.Get() v, err := en.st.Get()
if err != nil { if err != nil {
return err return err
} }
r, err := en.rs.RenderTemplate(location, v) r, err := en.rs.RenderTemplate(location, v, idx, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -31,8 +31,8 @@ func NewFsWrapper(path string, st *state.State) FsWrapper {
} }
} }
func (r FsWrapper) RenderTemplate(sym string, values map[string]string) (string, error) { func (r FsWrapper) RenderTemplate(sym string, values map[string]string, idx uint16, sizer *resource.Sizer) (string, error) {
return resource.DefaultRenderTemplate(r, sym, values) return resource.DefaultRenderTemplate(r, sym, values, idx, sizer)
} }
func(fs FsWrapper) one(ctx context.Context) (string, error) { func(fs FsWrapper) one(ctx context.Context) (string, error) {
@ -101,7 +101,7 @@ func TestEngineInit(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
r := st.Where() r, _ := st.Where()
if r != "foo" { if r != "foo" {
t.Fatalf("expected where-string 'foo', got %s", r) t.Fatalf("expected where-string 'foo', got %s", r)
} }

View File

@ -2,16 +2,30 @@ package resource
import ( import (
"bytes" "bytes"
"fmt"
"log"
"text/template" "text/template"
) )
// DefaultRenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate. // DefaultRenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate.
func DefaultRenderTemplate(r Resource, sym string, values map[string]string) (string, error) { func DefaultRenderTemplate(r Resource, sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
v, err := r.GetTemplate(sym, nil) v, err := r.GetTemplate(sym, nil)
if err != nil { if err != nil {
return "", err return "", err
} }
if sizer != nil {
values, err = sizer.GetAt(values, idx)
} else if idx > 0 {
return "", fmt.Errorf("sizer needed for indexed render")
}
log.Printf("render for index: %v", idx)
if err != nil {
return "", err
}
tp, err := template.New("tester").Option("missingkey=error").Parse(v) tp, err := template.New("tester").Option("missingkey=error").Parse(v)
if err != nil { if err != nil {
return "", err return "", err

View File

@ -20,9 +20,9 @@ type Resource interface {
PutMenu(string, string) error // Add a menu item. PutMenu(string, string) error // Add a menu item.
ShiftMenu() (string, string, error) // Remove and return the first menu item in list. ShiftMenu() (string, string, error) // Remove and return the first menu item in list.
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) (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() (string, error) // Render the current state of menu
Render(sym string, values map[string]string, 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.
} }
@ -30,6 +30,7 @@ type MenuResource struct {
menu [][2]string menu [][2]string
next [2]string next [2]string
prev [2]string prev [2]string
sinkValues []string
codeFunc CodeFunc codeFunc CodeFunc
templateFunc TemplateFunc templateFunc TemplateFunc
funcFunc FuncForFunc funcFunc FuncForFunc
@ -108,8 +109,8 @@ func(m *MenuResource) RenderMenu() (string, error) {
} }
// render menu and all syms except sink, split sink into display chunks // render menu and all syms except sink, split sink into display chunks
func(m *MenuResource) prepare(sym string, values map[string]string, sizer *Sizer) (map[string]string, error) { func(m *MenuResource) prepare(sym string, values map[string]string, idx uint16, sizer *Sizer) (map[string]string, error) {
var sink *string var sink string
var sinkValues []string var sinkValues []string
noSinkValues := make(map[string]string) noSinkValues := make(map[string]string)
for k, v := range values { for k, v := range values {
@ -118,7 +119,7 @@ func(m *MenuResource) prepare(sym string, values map[string]string, sizer *Sizer
return nil, err return nil, err
} }
if sz == 0 { if sz == 0 {
sink = &k sink = k
sinkValues = strings.Split(v, "\n") sinkValues = strings.Split(v, "\n")
v = "" v = ""
log.Printf("found sink %s with field count %v", k, len(sinkValues)) log.Printf("found sink %s with field count %v", k, len(sinkValues))
@ -126,12 +127,12 @@ func(m *MenuResource) prepare(sym string, values map[string]string, sizer *Sizer
noSinkValues[k] = v noSinkValues[k] = v
} }
if sink == nil { if sink == "" {
log.Printf("no sink found for sym %s", sym) log.Printf("no sink found for sym %s", sym)
return values, nil return values, nil
} }
s, err := m.render(sym, noSinkValues, sizer) s, err := m.render(sym, noSinkValues, 0, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -141,35 +142,51 @@ func(m *MenuResource) prepare(sym string, values map[string]string, sizer *Sizer
return nil, fmt.Errorf("capacity exceeded") return nil, fmt.Errorf("capacity exceeded")
} }
l := 0 log.Printf("%v bytes available for sink split", remaining)
r := ""
r_tmp := ""
l := 0
tb := strings.Builder{}
rb := strings.Builder{}
sizer.AddCursor(0)
for i, v := range sinkValues { for i, v := range sinkValues {
log.Printf("processing sinkvalue %v: %s", i, v)
l += len(v) l += len(v)
if uint32(l) > remaining { if uint32(l) > remaining {
if r_tmp == "" { if tb.Len() == 0 {
return nil, fmt.Errorf("capacity insufficient for sink field %v", i) return nil, fmt.Errorf("capacity insufficient for sink field %v", i)
} }
r += r_tmp + "\n" rb.WriteString(tb.String())
r_tmp = "" rb.WriteRune('\n')
c := uint32(rb.Len())
sizer.AddCursor(c)
tb.Reset()
l = 0 l = 0
} }
r_tmp += v if tb.Len() > 0 {
tb.WriteByte(byte(0x00))
}
tb.WriteString(v)
} }
r += r_tmp if tb.Len() > 0 {
rb.WriteString(tb.String())
if r[len(r)-1] != '\n' { }
r += "\n"
r := rb.String()
r = strings.TrimRight(r, "\n")
noSinkValues[sink] = r
for i, v := range strings.Split(r, "\n") {
log.Printf("nosinkvalue %v: %s", i, v)
} }
noSinkValues[*sink] = r
return noSinkValues, nil return noSinkValues, nil
} }
func(m *MenuResource) RenderTemplate(sym string, values map[string]string) (string, error) { func(m *MenuResource) RenderTemplate(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
return DefaultRenderTemplate(m, sym, values) return DefaultRenderTemplate(m, sym, values, idx, sizer)
} }
func(m *MenuResource) FuncFor(sym string) (EntryFunc, error) { func(m *MenuResource) FuncFor(sym string) (EntryFunc, error) {
@ -184,18 +201,20 @@ func(m *MenuResource) GetTemplate(sym string, sizer *Sizer) (string, error) {
return m.templateFunc(sym, sizer) return m.templateFunc(sym, sizer)
} }
func(m *MenuResource) render(sym string, values map[string]string, sizer *Sizer) (string, error) { func(m *MenuResource) render(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
var ok bool var ok bool
r := "" r := ""
s, err := m.RenderTemplate(sym, values) s, err := m.RenderTemplate(sym, values, idx, sizer)
if err != nil { if err != nil {
return "", err return "", err
} }
log.Printf("rendered %v bytes for template", len(s)) log.Printf("rendered %v bytes for template", len(s))
r += s r += s
_, ok = sizer.Check(r) if sizer != nil {
if !ok { _, ok = sizer.Check(r)
return "", fmt.Errorf("limit exceeded: %v", sizer) if !ok {
return "", fmt.Errorf("limit exceeded: %v", sizer)
}
} }
s, err = m.RenderMenu() s, err = m.RenderMenu()
if err != nil { if err != nil {
@ -203,20 +222,22 @@ func(m *MenuResource) render(sym string, values map[string]string, sizer *Sizer)
} }
log.Printf("rendered %v bytes for menu", len(s)) log.Printf("rendered %v bytes for menu", len(s))
r += s r += s
_, ok = sizer.Check(r) if sizer != nil {
if !ok { _, ok = sizer.Check(r)
return "", fmt.Errorf("limit exceeded: %v", sizer) if !ok {
return "", fmt.Errorf("limit exceeded: %v", sizer)
}
} }
return r, nil return r, nil
} }
func(m *MenuResource) Render(sym string, values map[string]string, sizer *Sizer) (string, error) { func(m *MenuResource) Render(sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
var err error var err error
values, err = m.prepare(sym, values, sizer) values, err = m.prepare(sym, values, idx, sizer)
if err != nil { if err != nil {
return "", err return "", err
} }
return m.render(sym, values, sizer) return m.render(sym, values, idx, sizer)
} }

View File

@ -1,8 +1,10 @@
package resource package resource
import ( import (
"bytes"
"fmt" "fmt"
"log" "log"
"strings"
"git.defalsify.org/festive/state" "git.defalsify.org/festive/state"
) )
@ -12,6 +14,7 @@ type Sizer struct {
menuSize uint16 menuSize uint16
memberSizes map[string]uint16 memberSizes map[string]uint16
totalMemberSize uint32 totalMemberSize uint32
crsrs []uint32
sink string sink string
} }
@ -21,6 +24,7 @@ func SizerFromState(st *state.State) (Sizer, error){
menuSize: st.GetMenuSize(), menuSize: st.GetMenuSize(),
memberSizes: make(map[string]uint16), memberSizes: make(map[string]uint16),
} }
sizes, err := st.Sizes() sizes, err := st.Sizes()
if err != nil { if err != nil {
return sz, err return sz, err
@ -56,12 +60,39 @@ func(szr *Sizer) String() string {
} }
func(szr *Sizer) Size(s string) (uint16, error) { func(szr *Sizer) Size(s string) (uint16, error) {
if szr.sink == s {
return 0, nil
}
r, ok := szr.memberSizes[s] r, ok := szr.memberSizes[s]
if !ok { if !ok {
return 0, fmt.Errorf("unknown member: %s", s) return 0, fmt.Errorf("unknown member: %s", s)
} }
return r, nil return r, nil
} }
func(szr *Sizer) AddCursor(c uint32) {
log.Printf("added cursor: %v", c)
szr.crsrs = append(szr.crsrs, c)
}
func(szr *Sizer) GetAt(values map[string]string, idx uint16) (map[string]string, error) {
if szr.sink == "" {
return values, nil
}
outValues := make(map[string]string)
for k, v := range values {
if szr.sink == k {
if idx >= uint16(len(szr.crsrs)) {
return nil, fmt.Errorf("no more values in index")
}
c := szr.crsrs[idx]
v = v[c:]
nl := strings.Index(v, "\n")
log.Printf("k %v v %v c %v nl %v", k, v, c, nl)
if nl > 0 {
v = v[:nl]
}
b := bytes.ReplaceAll([]byte(v), []byte{0x00}, []byte{0x0a})
v = string(b)
}
outValues[k] = v
}
return outValues, nil
}

View File

@ -19,7 +19,8 @@ func getTemplate(sym string, sizer *Sizer) (string, error) {
tpl = "one {{.foo}} two {{.bar}} three {{.baz}}" tpl = "one {{.foo}} two {{.bar}} three {{.baz}}"
case "toobig": case "toobig":
tpl = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus." tpl = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in mattis lorem. Aliquam erat volutpat. Ut vitae metus."
case "pages":
tpl = "one {{.foo}} two {{.bar}} three {{.baz}}\n{{.xyzzy}}"
} }
return tpl, nil return tpl, nil
} }
@ -32,6 +33,8 @@ func funcFor(sym string) (EntryFunc, error) {
return getBar, nil return getBar, nil
case "baz": case "baz":
return getBaz, nil return getBaz, nil
case "xyzzy":
return getXyzzy, nil
} }
return nil, fmt.Errorf("unknown func: %s", sym) return nil, fmt.Errorf("unknown func: %s", sym)
} }
@ -48,6 +51,9 @@ func getBaz(ctx context.Context) (string, error) {
return "blinky", nil return "blinky", nil
} }
func getXyzzy(ctx context.Context) (string, error) {
return "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", nil
}
func TestSizeLimit(t *testing.T) { func TestSizeLimit(t *testing.T) {
st := state.NewState(0).WithOutputSize(128) st := state.NewState(0).WithOutputSize(128)
@ -81,7 +87,7 @@ func TestSizeLimit(t *testing.T) {
} }
_ = tpl _ = tpl
_, err = rs.Render("small", vals, &szr) _, err = rs.Render("small", vals, 0, &szr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -89,8 +95,74 @@ func TestSizeLimit(t *testing.T) {
rs.PutMenu("1", "foo the foo") rs.PutMenu("1", "foo the foo")
rs.PutMenu("2", "go to bar") rs.PutMenu("2", "go to bar")
_, err = rs.Render("toobig", vals, &szr) _, err = rs.Render("toobig", vals, 0, &szr)
if err == nil { if err == nil {
t.Fatalf("expected size exceeded") t.Fatalf("expected size exceeded")
} }
} }
func TestSizePages(t *testing.T) {
st := state.NewState(0).WithOutputSize(128)
mrs := NewMenuResource().WithEntryFuncGetter(funcFor).WithTemplateGetter(getTemplate)
rs := TestSizeResource{
mrs,
}
st.Add("foo", "inky", 4)
st.Add("bar", "pinky", 10)
st.Add("baz", "blinky", 20)
st.Add("xyzzy", "inky pinky\nblinky clyde sue\ntinkywinky dipsy\nlala poo\none two three four five six seven\neight nine ten\neleven twelve", 0)
st.Map("foo")
st.Map("bar")
st.Map("baz")
st.Map("xyzzy")
st.SetMenuSize(32)
szr, err := SizerFromState(&st)
if err != nil {
t.Fatal(err)
}
vals, err := st.Get()
if err != nil {
t.Fatal(err)
}
rs.PutMenu("1", "foo the foo")
rs.PutMenu("2", "go to bar")
r, err := rs.Render("pages", vals, 0, &szr)
if err != nil {
t.Fatal(err)
}
expect := `one inky two pinky three blinky
inky pinky
blinky clyde sue
tinkywinky dipsy
lala poo`
if r != expect {
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
}
rs.PutMenu("1", "foo the foo")
rs.PutMenu("2", "go to bar")
szr, err = SizerFromState(&st)
if err != nil {
t.Fatal(err)
}
r, err = rs.Render("pages", vals, 1, &szr)
if err != nil {
t.Fatal(err)
}
expect = `one inky two pinky three blinky
one two three four five six seven
eight nine ten
eleven twelve`
if r != expect {
t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", expect, r)
}
}

View File

@ -37,7 +37,7 @@ type State struct {
sizes map[string]uint16 // Size limits for all loaded symbols. sizes map[string]uint16 // Size limits for all loaded symbols.
bitSize uint32 // size of (32-bit capacity) bit flag byte array bitSize uint32 // size of (32-bit capacity) bit flag byte array
sink *string sink *string
//sizeIdx uint16 sizeIdx uint16
} }
func toByteSize(bitSize uint32) uint8 { func toByteSize(bitSize uint32) uint8 {
@ -204,12 +204,12 @@ func(st State) WithOutputSize(outputSize uint32) State {
} }
// Where returns the current active rendering symbol. // Where returns the current active rendering symbol.
func(st State) Where() string { func(st State) Where() (string, uint16) {
if len(st.execPath) == 0 { if len(st.execPath) == 0 {
return "" return "", 0
} }
l := len(st.execPath) l := len(st.execPath)
return st.execPath[l-1] return st.execPath[l-1], st.sizeIdx
} }
// Down adds the given symbol to the command stack. // Down adds the given symbol to the command stack.

View File

@ -89,7 +89,7 @@ func RunDeadCheck(b []byte, st *state.State, rs resource.Resource, ctx context.C
if r { if r {
return b, nil return b, nil
} }
location := st.Where() location, _ := st.Where()
if location == "" { if location == "" {
return b, fmt.Errorf("dead runner with no current location") return b, fmt.Errorf("dead runner with no current location")
} }
@ -180,7 +180,7 @@ func RunMove(b []byte, st *state.State, rs resource.Resource, ctx context.Contex
} }
if sym == "_" { if sym == "_" {
st.Up() st.Up()
sym = st.Where() sym, _ = st.Where()
} else { } else {
st.Down(sym) st.Down(sym)
} }

View File

@ -51,8 +51,8 @@ func (r *TestResource) GetTemplate(sym string, sizer *resource.Sizer) (string, e
return "", fmt.Errorf("unknown symbol %s", sym) return "", fmt.Errorf("unknown symbol %s", sym)
} }
func (r *TestResource) RenderTemplate(sym string, values map[string]string) (string, error) { func (r *TestResource) RenderTemplate(sym string, values map[string]string, idx uint16, sizer *resource.Sizer) (string, error) {
return resource.DefaultRenderTemplate(r, sym, values) return resource.DefaultRenderTemplate(r, sym, values, idx, sizer)
} }
func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) { func (r *TestResource) FuncFor(sym string) (resource.EntryFunc, error) {
@ -117,7 +117,7 @@ func TestRunLoadRender(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
r, err := rs.RenderTemplate("foo", m) r, err := rs.RenderTemplate("foo", m, 0, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -126,7 +126,7 @@ func TestRunLoadRender(t *testing.T) {
t.Errorf("Expected %v, got %v", []byte(expect), []byte(r)) t.Errorf("Expected %v, got %v", []byte(expect), []byte(r))
} }
r, err = rs.RenderTemplate("bar", m) r, err = rs.RenderTemplate("bar", m, 0, nil)
if err == nil { if err == nil {
t.Errorf("expected error for render of bar: %v" ,err) t.Errorf("expected error for render of bar: %v" ,err)
} }
@ -147,7 +147,7 @@ func TestRunLoadRender(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
r, err = rs.RenderTemplate("bar", m) r, err = rs.RenderTemplate("bar", m, 0, nil)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
@ -219,7 +219,7 @@ func TestHalt(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
r := st.Where() r, _ := st.Where()
if r == "foo" { if r == "foo" {
t.Fatalf("Expected where-symbol not to be 'foo'") t.Fatalf("Expected where-symbol not to be 'foo'")
} }
@ -245,7 +245,7 @@ func TestRunArg(t *testing.T) {
if l != 0 { if l != 0 {
t.Errorf("expected empty remainder, got length %v: %v", l, b) t.Errorf("expected empty remainder, got length %v: %v", l, b)
} }
r := st.Where() r, _ := st.Where()
if r != "baz" { if r != "baz" {
t.Errorf("expected where-state baz, got %v", r) t.Errorf("expected where-state baz, got %v", r)
} }
@ -270,7 +270,7 @@ func TestRunInputHandler(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
r := st.Where() r, _ := st.Where()
if r != "foo" { if r != "foo" {
t.Fatalf("expected where-sym 'foo', got '%v'", r) t.Fatalf("expected where-sym 'foo', got '%v'", r)
} }
@ -291,7 +291,7 @@ func TestRunArgInvalid(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
r := st.Where() r, _ := st.Where()
if r != "_catch" { if r != "_catch" {
t.Fatalf("expected where-state _catch, got %v", r) t.Fatalf("expected where-state _catch, got %v", r)
} }