Documentation
¶
Overview ¶
Package view renders arbitrary display data through text/template. The package itself has no notion of output format (ASCII table, org-mode, or anything else) — that decision lives entirely in the template text a caller supplies. Table and PropertyList are convenience helpers layered on top for the common cases of tabular and key/value data: they do the Go-side width computation and padding (text/template has no good way to do either) and hand the result to Render as pre-formatted lines.
Index ¶
- func Render(w io.Writer, tmplText string, data any) error
- type PropertyList
- type Table
- func (t *Table) AddGroup()
- func (t *Table) AddRow(cells ...string)
- func (t *Table) Header() string
- func (t *Table) Lines() [][]string
- func (t *Table) OrgHeader() string
- func (t *Table) OrgLines() [][]string
- func (t *Table) OrgRule() string
- func (t *Table) RenderASCII(w io.Writer) error
- func (t *Table) RenderOrg(w io.Writer) error
- func (t *Table) Rule() string
- func (t *Table) SetRight(cols ...int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Render ¶
Render parses tmplText and executes it against data, writing the result to w. Callers that render the same template repeatedly should parse it once via template.Must(template.New(...).Parse(...)) at package scope instead of calling Render, which reparses tmplText on every call.
tmplText must be trusted input: Go templates can read all exported fields and call all exported methods of data, which may expose sensitive values or trigger side effects if the template text is user-controlled.
Missing keys use the default text/template behavior and render as "<no value>". To turn missing keys into hard errors, parse the template explicitly with template.New("…").Option("missingkey=error").Parse(…).
Types ¶
type PropertyList ¶
type PropertyList struct {
// contains filtered or unexported fields
}
PropertyList is a data-prep helper for single-record key/value output (e.g. "trader bot detail"'s field dump): a key-width-aligned block with no header row, unlike Table. There's no org-mode variant today — nothing in the repo emits property lists as org — but Lines stays available for callers composing a larger document template later.
func NewPropertyList ¶
func NewPropertyList() *PropertyList
NewPropertyList returns an empty PropertyList.
func (*PropertyList) Add ¶
func (p *PropertyList) Add(key, value string)
Add appends a key/value line.
func (*PropertyList) AddIf ¶
func (p *PropertyList) AddIf(cond bool, key, value string)
AddIf appends a key/value line only when cond is true, for optional fields that shouldn't print at all rather than print empty.
func (*PropertyList) Lines ¶
func (p *PropertyList) Lines() []string
Lines returns the key-width-padded "key value" lines, ready to embed in a larger template.
type Table ¶
type Table struct {
Headers []string
Rows [][]string
Right map[int]bool // per-column right-justify; default left
Groups []int // row-count boundaries where a new group starts; nil/empty = one group
}
Table is a data-prep helper for row/column content: it computes per-column widths and padding in Go (text/template has no good way to do either) and exposes the result both as ready-to-embed formatted lines (Lines/OrgLines, for callers composing a larger document template) and via convenience standalone renderers (RenderASCII/RenderOrg) for the common case of a table being the entire output.
func (*Table) AddGroup ¶
func (t *Table) AddGroup()
AddGroup marks a group boundary at the current row count, so a blank line (ASCII) or hline (org) separates the rows added before this call from the rows added after it.
func (*Table) Lines ¶
Lines returns the ASCII-formatted, padded rows, grouped by AddGroup boundaries — one inner slice per group, ready to embed in a larger template or feed to RenderASCII's built-in one.
func (*Table) OrgLines ¶
OrgLines returns the org-mode pipe-delimited, padded rows, grouped by AddGroup boundaries — one inner slice per group.
func (*Table) OrgRule ¶
OrgRule returns an org-mode hline ("|---+---|") sized to each column's width, valid org-table syntax for separating a header from its body or one group of rows from the next.
func (*Table) RenderASCII ¶
RenderASCII writes the table as a human-readable, column-aligned block: header, dash underline, rows, with a blank line between AddGroup groups. Column widths are computed once per call.
func (*Table) RenderOrg ¶
RenderOrg writes the table as a valid org-mode table: header, hline, rows, with an hline between AddGroup groups (org tables don't tolerate blank lines mid-table). Column widths are computed once per call.