view

package
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: BSD-2-Clause Imports: 4 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(w io.Writer, tmplText string, data any) error

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.

func (*PropertyList) Render

func (p *PropertyList) Render(w io.Writer) error

Render writes Lines to w, one per line. It returns the first write error encountered, if any.

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 NewTable

func NewTable(headers ...string) *Table

NewTable returns a Table with the given column headers.

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) AddRow

func (t *Table) AddRow(cells ...string)

AddRow appends one row of cell values.

func (*Table) Header

func (t *Table) Header() string

Header returns the ASCII-formatted, padded header line.

func (*Table) Lines

func (t *Table) Lines() [][]string

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) OrgHeader

func (t *Table) OrgHeader() string

OrgHeader returns the org-mode pipe-delimited, padded header line.

func (*Table) OrgLines

func (t *Table) OrgLines() [][]string

OrgLines returns the org-mode pipe-delimited, padded rows, grouped by AddGroup boundaries — one inner slice per group.

func (*Table) OrgRule

func (t *Table) OrgRule() string

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

func (t *Table) RenderASCII(w io.Writer) error

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

func (t *Table) RenderOrg(w io.Writer) error

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.

func (*Table) Rule

func (t *Table) Rule() string

Rule returns an ASCII underline line (dashes sized to each header's own text, then padded to column width like any other row).

func (*Table) SetRight

func (t *Table) SetRight(cols ...int)

SetRight marks the given zero-based column indexes as right-justified.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL