2023-04-09 00:35:13 +02:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"git.defalsify.org/festive/cache"
|
|
|
|
"git.defalsify.org/festive/resource"
|
|
|
|
)
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Page exectues output rendering into pages constrained by size.
|
2023-04-09 00:35:13 +02:00
|
|
|
type Page struct {
|
2023-04-12 19:04:36 +02:00
|
|
|
cacheMap map[string]string // Mapped content symbols
|
|
|
|
cache cache.Memory // Content store.
|
|
|
|
resource resource.Resource // Symbol resolver.
|
|
|
|
menu *Menu // Menu rendererer.
|
|
|
|
sink *string // Content symbol rendered by dynamic size.
|
|
|
|
sizer *Sizer // Process size constraints.
|
2023-04-09 00:35:13 +02:00
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// NewPage creates a new Page object.
|
2023-04-09 11:16:41 +02:00
|
|
|
func NewPage(cache cache.Memory, rs resource.Resource) *Page {
|
2023-04-09 00:35:13 +02:00
|
|
|
return &Page{
|
|
|
|
cache: cache,
|
|
|
|
cacheMap: make(map[string]string),
|
|
|
|
resource: rs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// WithMenu sets a menu renderer for the page.
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) WithMenu(menu *Menu) *Page {
|
|
|
|
pg.menu = menu
|
|
|
|
if pg.sizer != nil {
|
|
|
|
pg.sizer = pg.sizer.WithMenuSize(pg.menu.ReservedSize())
|
|
|
|
}
|
|
|
|
return pg
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// WithSizer sets a size constraints definition for the page.
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) WithSizer(sizer *Sizer) *Page {
|
|
|
|
pg.sizer = sizer
|
|
|
|
if pg.menu != nil {
|
|
|
|
pg.sizer = pg.sizer.WithMenuSize(pg.menu.ReservedSize())
|
|
|
|
}
|
|
|
|
return pg
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Usage returns size used by values and menu, and remaining size available
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) Usage() (uint32, uint32, error) {
|
|
|
|
var l int
|
|
|
|
var c uint16
|
|
|
|
for k, v := range pg.cacheMap {
|
|
|
|
l += len(v)
|
|
|
|
sz, err := pg.cache.ReservedSize(k)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
c += sz
|
2023-04-10 16:26:18 +02:00
|
|
|
log.Printf("v %x %v %v %v %v", []byte(v), len(v), l, sz, c)
|
2023-04-09 00:35:13 +02:00
|
|
|
}
|
|
|
|
r := uint32(l)
|
2023-04-10 16:26:18 +02:00
|
|
|
rsv := uint32(c)-r
|
2023-04-09 00:35:13 +02:00
|
|
|
if pg.menu != nil {
|
|
|
|
r += uint32(pg.menu.ReservedSize())
|
|
|
|
}
|
2023-04-10 16:26:18 +02:00
|
|
|
return r, rsv, nil
|
2023-04-09 00:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Map marks the given key for retrieval.
|
|
|
|
//
|
|
|
|
// After this, Val() will return the value for the key, and Size() will include the value size and limitations in its calculations.
|
|
|
|
//
|
|
|
|
// Only one symbol with no size limitation may be mapped at the current level.
|
|
|
|
func(pg *Page) Map(key string) error {
|
2023-04-10 16:26:18 +02:00
|
|
|
v, err := pg.cache.Get(key)
|
2023-04-09 00:35:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l, err := pg.cache.ReservedSize(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if l == 0 {
|
2023-04-09 10:44:32 +02:00
|
|
|
if pg.sink != nil && *pg.sink != key {
|
2023-04-09 00:35:13 +02:00
|
|
|
return fmt.Errorf("sink already set to symbol '%v'", *pg.sink)
|
|
|
|
}
|
|
|
|
pg.sink = &key
|
|
|
|
}
|
2023-04-10 16:26:18 +02:00
|
|
|
pg.cacheMap[key] = v
|
2023-04-09 00:35:13 +02:00
|
|
|
if pg.sizer != nil {
|
|
|
|
err := pg.sizer.Set(key, l)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 18:38:23 +02:00
|
|
|
log.Printf("mapped %s", key)
|
2023-04-09 00:35:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Val gets the mapped content for the given symbol.
|
|
|
|
//
|
2023-04-09 00:35:13 +02:00
|
|
|
// Fails if key is not mapped.
|
|
|
|
func(pg *Page) Val(key string) (string, error) {
|
|
|
|
r := pg.cacheMap[key]
|
|
|
|
if len(r) == 0 {
|
|
|
|
return "", fmt.Errorf("key %v not mapped", key)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Sizes returned the actual used bytes by each mapped symbol.
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) Sizes() (map[string]uint16, error) {
|
|
|
|
sizes := make(map[string]uint16)
|
|
|
|
var haveSink bool
|
|
|
|
for k, _ := range pg.cacheMap {
|
|
|
|
l, err := pg.cache.ReservedSize(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if l == 0 {
|
|
|
|
if haveSink {
|
|
|
|
panic(fmt.Sprintf("duplicate sink for %v", k))
|
|
|
|
}
|
|
|
|
haveSink = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sizes, nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// RenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate.
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) RenderTemplate(sym string, values map[string]string, idx uint16) (string, error) {
|
|
|
|
tpl, err := pg.resource.GetTemplate(sym)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if pg.sizer != nil {
|
|
|
|
values, err = pg.sizer.GetAt(values, idx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if idx > 0 {
|
|
|
|
return "", fmt.Errorf("sizer needed for indexed render")
|
|
|
|
}
|
|
|
|
log.Printf("render for index: %v", idx)
|
|
|
|
|
|
|
|
tp, err := template.New("tester").Option("missingkey=error").Parse(tpl)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-04-12 15:20:09 +02:00
|
|
|
|
2023-04-09 00:35:13 +02:00
|
|
|
b := bytes.NewBuffer([]byte{})
|
|
|
|
err = tp.Execute(b, values)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), err
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// Render renders the current mapped content and menu state against the template associated with the symbol.
|
|
|
|
func(pg *Page) Render(sym string, idx uint16) (string, error) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
values, err := pg.prepare(sym, pg.cacheMap, idx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pg.render(sym, values, idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset prepared the Page object for re-use.
|
|
|
|
//
|
|
|
|
// It clears mappings and removes the sink definition.
|
|
|
|
func(pg *Page) Reset() {
|
|
|
|
pg.sink = nil
|
|
|
|
pg.cacheMap = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2023-04-09 00:35:13 +02:00
|
|
|
// render menu and all syms except sink, split sink into display chunks
|
2023-04-11 09:35:00 +02:00
|
|
|
// TODO: Function too long, split up
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) prepare(sym string, values map[string]string, idx uint16) (map[string]string, error) {
|
|
|
|
var sink string
|
2023-04-10 06:30:57 +02:00
|
|
|
|
|
|
|
if pg.sizer == nil {
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
2023-04-09 00:35:13 +02:00
|
|
|
var sinkValues []string
|
|
|
|
noSinkValues := make(map[string]string)
|
|
|
|
for k, v := range values {
|
|
|
|
sz, err := pg.cache.ReservedSize(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if sz == 0 {
|
|
|
|
sink = k
|
|
|
|
sinkValues = strings.Split(v, "\n")
|
|
|
|
v = ""
|
|
|
|
log.Printf("found sink %s with field count %v", k, len(sinkValues))
|
|
|
|
}
|
|
|
|
noSinkValues[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if sink == "" {
|
|
|
|
log.Printf("no sink found for sym %s", sym)
|
|
|
|
return values, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pg.sizer.AddCursor(0)
|
|
|
|
s, err := pg.render(sym, noSinkValues, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 09:35:00 +02:00
|
|
|
// remaining includes core menu
|
|
|
|
remaining, ok := pg.sizer.Check(s)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("capacity exceeded")
|
|
|
|
}
|
2023-04-09 00:35:13 +02:00
|
|
|
|
2023-04-11 09:35:00 +02:00
|
|
|
var menuSizes [4]uint32 // mainSize, prevsize, nextsize, nextsize+prevsize
|
2023-04-10 18:38:23 +02:00
|
|
|
if pg.menu != nil {
|
2023-04-11 09:35:00 +02:00
|
|
|
cfg := pg.menu.GetBrowseConfig()
|
|
|
|
tmpm := NewMenu().WithBrowseConfig(cfg)
|
|
|
|
v, err := tmpm.Render(0)
|
2023-04-10 18:38:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-11 09:35:00 +02:00
|
|
|
menuSizes[0] = uint32(len(v))
|
|
|
|
tmpm = tmpm.WithPageCount(2)
|
|
|
|
v, err = tmpm.Render(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
menuSizes[1] = uint32(len(v)) - menuSizes[0]
|
|
|
|
v, err = tmpm.Render(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
menuSizes[2] = uint32(len(v)) - menuSizes[0]
|
|
|
|
menuSizes[3] = menuSizes[1] + menuSizes[2]
|
2023-04-09 00:35:13 +02:00
|
|
|
}
|
|
|
|
|
2023-04-11 09:35:00 +02:00
|
|
|
log.Printf("%v bytes available for sink split before navigation", remaining)
|
2023-04-09 00:35:13 +02:00
|
|
|
|
|
|
|
l := 0
|
|
|
|
var count uint16
|
|
|
|
tb := strings.Builder{}
|
|
|
|
rb := strings.Builder{}
|
|
|
|
|
2023-04-11 09:35:00 +02:00
|
|
|
netRemaining := remaining
|
|
|
|
if len(sinkValues) > 1 {
|
2023-04-12 08:56:02 +02:00
|
|
|
log.Printf("menusizes %v", menuSizes)
|
|
|
|
netRemaining -= menuSizes[1] - 1
|
2023-04-11 09:35:00 +02:00
|
|
|
}
|
|
|
|
|
2023-04-09 00:35:13 +02:00
|
|
|
for i, v := range sinkValues {
|
|
|
|
log.Printf("processing sinkvalue %v: %s", i, v)
|
|
|
|
l += len(v)
|
2023-04-11 09:35:00 +02:00
|
|
|
if uint32(l) > netRemaining {
|
2023-04-09 00:35:13 +02:00
|
|
|
if tb.Len() == 0 {
|
|
|
|
return nil, fmt.Errorf("capacity insufficient for sink field %v", i)
|
|
|
|
}
|
|
|
|
rb.WriteString(tb.String())
|
|
|
|
rb.WriteRune('\n')
|
|
|
|
c := uint32(rb.Len())
|
|
|
|
pg.sizer.AddCursor(c)
|
|
|
|
tb.Reset()
|
|
|
|
l = 0
|
2023-04-11 09:35:00 +02:00
|
|
|
if count == 0 {
|
|
|
|
netRemaining -= menuSizes[2]
|
|
|
|
}
|
2023-04-09 00:35:13 +02:00
|
|
|
count += 1
|
|
|
|
}
|
|
|
|
if tb.Len() > 0 {
|
|
|
|
tb.WriteByte(byte(0x00))
|
2023-04-12 08:56:02 +02:00
|
|
|
l += 1
|
2023-04-09 00:35:13 +02:00
|
|
|
}
|
|
|
|
tb.WriteString(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tb.Len() > 0 {
|
|
|
|
rb.WriteString(tb.String())
|
|
|
|
count += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
r := rb.String()
|
|
|
|
r = strings.TrimRight(r, "\n")
|
|
|
|
|
|
|
|
noSinkValues[sink] = r
|
|
|
|
|
|
|
|
if pg.menu != nil {
|
|
|
|
pg.menu = pg.menu.WithPageCount(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range strings.Split(r, "\n") {
|
|
|
|
log.Printf("nosinkvalue %v: %s", i, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return noSinkValues, nil
|
|
|
|
}
|
|
|
|
|
2023-04-12 19:04:36 +02:00
|
|
|
// render template, menu (if it exists), and audit size constraint (if it exists).
|
2023-04-09 00:35:13 +02:00
|
|
|
func(pg *Page) render(sym string, values map[string]string, idx uint16) (string, error) {
|
|
|
|
var ok bool
|
|
|
|
r := ""
|
|
|
|
s, err := pg.RenderTemplate(sym, values, idx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
log.Printf("rendered %v bytes for template", len(s))
|
|
|
|
r += s
|
2023-04-11 09:35:00 +02:00
|
|
|
|
|
|
|
if pg.menu != nil {
|
|
|
|
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
|
|
|
|
}
|
2023-04-09 16:35:26 +02:00
|
|
|
}
|
2023-04-11 09:35:00 +02:00
|
|
|
|
2023-04-09 00:35:13 +02:00
|
|
|
if pg.sizer != nil {
|
|
|
|
_, ok = pg.sizer.Check(r)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("limit exceeded: %v", pg.sizer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|