vise/go/resource/resource.go

149 lines
4.2 KiB
Go
Raw Normal View History

2023-03-31 11:52:04 +02:00
package resource
2023-03-31 11:59:55 +02:00
import (
"context"
"fmt"
"log"
2023-03-31 11:59:55 +02:00
)
// EntryFunc is a function signature for retrieving value for a key
type EntryFunc func(ctx context.Context) (string, error)
type CodeFunc func(sym string) ([]byte, error)
type TemplateFunc func(sym string, sizer *Sizer) (string, error)
type FuncForFunc func(sym string) (EntryFunc, error)
2023-03-31 11:52:04 +02:00
// Resource implementation are responsible for retrieving values and templates for symbols, and can render templates from value dictionaries.
type Resource interface {
GetTemplate(sym string, sizer *Sizer) (string, error) // Get the template for a given symbol.
2023-04-01 11:58:02 +02:00
GetCode(sym string) ([]byte, error) // Get the bytecode for the given symbol.
2023-04-03 10:11:44 +02:00
PutMenu(string, string) error // Add a menu item.
ShiftMenu() (string, string, error) // Remove and return the first menu item in list.
SetMenuBrowse(string, string, bool) error // Set menu browser display details.
2023-04-01 11:58:02 +02:00
RenderTemplate(sym string, values map[string]string) (string, error) // Render the given data map using the template of the symbol.
2023-04-06 13:08:30 +02:00
RenderMenu() (string, error) // Render the current state of menu
Render(sym string, values map[string]string, sizer *Sizer) (string, error) // Render full output.
2023-04-06 13:08:30 +02:00
FuncFor(sym string) (EntryFunc, error) // Resolve symbol content point for.
2023-03-31 11:52:04 +02:00
}
type MenuResource struct {
menu [][2]string
2023-04-03 10:11:44 +02:00
next [2]string
prev [2]string
codeFunc CodeFunc
templateFunc TemplateFunc
funcFunc FuncForFunc
}
func NewMenuResource() *MenuResource {
return &MenuResource{}
}
func(m *MenuResource) WithCodeGetter(codeGetter CodeFunc) *MenuResource {
m.codeFunc = codeGetter
return m
}
func(m *MenuResource) WithEntryFuncGetter(entryFuncGetter FuncForFunc) *MenuResource {
m.funcFunc = entryFuncGetter
return m
}
func(m *MenuResource) WithTemplateGetter(templateGetter TemplateFunc) *MenuResource {
m.templateFunc = templateGetter
return m
2023-04-03 10:11:44 +02:00
}
2023-04-06 13:08:30 +02:00
// SetMenuBrowse defines the how pagination menu options should be displayed.
//
// The selector is the expected user input, and the title is the description string.
//
// If back is set, the option will be defined for returning to a previous page.
2023-04-03 10:11:44 +02:00
func(m *MenuResource) SetMenuBrowse(selector string, title string, back bool) error {
entry := [2]string{selector, title}
if back {
m.prev = entry
} else {
m.next = entry
}
return nil
}
2023-04-06 13:08:30 +02:00
// PutMenu adds a menu option to the menu rendering.
func(m *MenuResource) PutMenu(selector string, title string) error {
m.menu = append(m.menu, [2]string{selector, title})
log.Printf("menu %v", m.menu)
return nil
}
2023-04-06 13:08:30 +02:00
// PutMenu removes and returns the first of remaining menu options.
//
// Fails if menu is empty.
func(m *MenuResource) ShiftMenu() (string, string, error) {
if len(m.menu) == 0 {
return "", "", fmt.Errorf("menu is empty")
}
r := m.menu[0]
m.menu = m.menu[1:]
return r[0], r[1], nil
}
2023-04-06 13:08:30 +02:00
// RenderMenu returns the full current state of the menu as a string.
//
// After this has been executed, the state of the menu will be empty.
func(m *MenuResource) RenderMenu() (string, error) {
r := ""
for true {
l := len(r)
choice, title, err := m.ShiftMenu()
if err != nil {
break
}
if l > 0 {
r += "\n"
}
r += fmt.Sprintf("%s:%s", choice, title)
}
return r, nil
}
func(m *MenuResource) RenderTemplate(sym string, values map[string]string) (string, error) {
return DefaultRenderTemplate(m, sym, values)
}
func(m *MenuResource) FuncFor(sym string) (EntryFunc, error) {
return m.funcFunc(sym)
}
func(m *MenuResource) GetCode(sym string) ([]byte, error) {
return m.codeFunc(sym)
}
func(m *MenuResource) GetTemplate(sym string, sizer *Sizer) (string, error) {
return m.templateFunc(sym, sizer)
}
func(m *MenuResource) Render(sym string, values map[string]string, sizer *Sizer) (string, error) {
r := ""
s, err := m.RenderTemplate(sym, values)
if err != nil {
return "", err
}
log.Printf("rendered %v bytes for template", len(s))
r += s
l, ok := sizer.Check(r)
if !ok {
return "", fmt.Errorf("rendered size %v exceeds limits: %v", l, sizer)
}
s, err = m.RenderMenu()
if err != nil {
return "", err
}
log.Printf("rendered %v bytes for menu", len(s))
r += s
l, ok = sizer.Check(r)
if !ok {
return "", fmt.Errorf("rendered size %v exceeds limits: %v", l, sizer)
}
return r, nil
}