Documentation
¶
Overview ¶
Package render has the text rendering that every card shares: markdown, code, diffs, tables and small formatters tuned to the theme.
Index ¶
- Constants
- func Axis(width int, ticks ...Tick) string
- func Bad(text string, t theme.Theme) string
- func Beside(boxes [][]string, width, gap int) []string
- func Blocks(blocks ...[]string) []string
- func Bytes(n int64) string
- func Carry(lines []string) []string
- func ChartInk(recency float64) float64
- func Clock(d time.Duration) string
- func CodeStyle(t theme.Theme) string
- func Cool(text string, t theme.Theme) string
- func Dim(text string, t theme.Theme) string
- func Divider(width int, t theme.Theme) string
- func Elapsed(d time.Duration) string
- func Facts(facts []Fact, width int, t theme.Theme) []string
- func Fit(s string, width int) string
- func Good(text string, t theme.Theme) string
- func Group(text string, t theme.Theme) string
- func Heading(text string, t theme.Theme) string
- func Height(cells []string, sizes, widths []int) int
- func Item(mark string, lines []string, width int) []string
- func Lay(rows, stack []string, corner Corner, width int) []string
- func LeftCells(cols []Column, left []bool, cells []string, width int, t theme.Theme) []string
- func Money(f float64) string
- func Natural(cols []Column, rows [][]string) []int
- func Nested(depth int, text string) string
- func On(at time.Time) string
- func Paint(st lipgloss.Style, text string) string
- func PaintTimeline(rows [][]ChartRun, colors []color.Color, t theme.Theme, selected int) []string
- func Percent(f float64) string
- func Relative(at time.Time) string
- func Section(title string, lines []string, t theme.Theme) []string
- func Settled(text string) int
- func ShareBar(n, top int64, width int, t theme.Theme) string
- func Spark(counts []int64, t theme.Theme) string
- func Spinner(tick int, t theme.Theme) string
- func Stretches(moments []Moment, series, rows int, location *time.Location) ([]Stretch, Grain)
- func StyleConfig(t theme.Theme) ansi.StyleConfig
- func Table(cols []Column, rows [][]string, width int, t theme.Theme) string
- func Timeline(stretches []Stretch, width, inset int, glyphs ChartGlyphs) [][]ChartRun
- func Titled(cols []Column) bool
- func Tokens(n int) string
- func ToolGlyph(icon string, t theme.Theme) string
- func Unmarked(markdown string) string
- func Untab(text string) string
- func Warm(text string, t theme.Theme) string
- func When(at time.Time) string
- func Widest(cols []Column, cells [][]int) []int
- func Widths(cells []string) []int
- func Wrap(s string, width int) string
- type ChartGlyphs
- type ChartPart
- type ChartRun
- type Column
- type Corner
- type Edit
- type Fact
- type Grain
- type Grid
- type Grip
- func (g *Grip) Drag(s Scrollbar, y int, pressed bool) (int, bool)
- func (g *Grip) Drop() bool
- func (g Grip) Holding() bool
- func (g *Grip) Hover(s Scrollbar, y int, on bool) bool
- func (g Grip) On(s Scrollbar) Scrollbar
- func (g *Grip) Press(s Scrollbar, y int) (ScrollHit, int)
- func (g *Grip) Release() bool
- type Layout
- type Line
- type Mark
- type Markdown
- type Moment
- type Op
- type ScrollHit
- type ScrollPart
- type Scrollbar
- func (s Scrollbar) Column(t theme.Theme) []string
- func (s Scrollbar) DragTo(y, held int) int
- func (s Scrollbar) Grab(y int) int
- func (s Scrollbar) Hit(y int) (ScrollHit, int)
- func (s Scrollbar) Lit() ScrollPart
- func (s Scrollbar) OnTrack(y int) bool
- func (s Scrollbar) Part(y int) ScrollPart
- func (s Scrollbar) Same(o Scrollbar) bool
- func (s Scrollbar) Scrolls() bool
- type Scrolled
- type Stream
- type Stretch
- type Tick
Constants ¶
const BarWidth = 1
BarWidth is the number of columns that the scrollbar itself takes, in the last column inside a pane's rail. The rail's other columns are always blank, so a highlighted row's background never reaches the bar.
const ChartLeadIn = 0.06
ChartLeadIn is the oldest stretch's recency, just above zero, so the stretch is still drawn.
const ColumnGap = 3
ColumnGap is the number of blank columns between two cells in a row.
const MaxDiffLines = 6000
MaxDiffLines is the line count above which a diff is summarized instead of computed.
const ScrollbarWidth = 40
ScrollbarWidth is the width that a view needs before it gives the scrollbar's rail its last space.Rail columns.
const Stays = math.MaxInt
Stays is the rank for a column that never leaves, such as a list's name column after a mark column. A staying column wider than its share of the width wraps its cells over more lines. The first column always stays.
const TabWidth = 4
TabWidth is the number of cells between two tab stops when Untab expands the tabs in text that a card shows.
Variables ¶
This section is empty.
Functions ¶
func Axis ¶
Axis returns a row up to width cells wide that sets each tick's label at its column, measured in cells, under one-cell marks such as a spark line. A label that would run into the previous label or past the width is left out.
func Beside ¶
Beside returns boxes side by side at the right of rows at the width, with gap blank columns between each two boxes, the inset that windows keep between each other. A box shorter than the tallest box is padded with blank rows, and a row narrower than its box's widest row is padded with blanks, so the next box lines up.
func Blocks ¶
Blocks returns the blocks in order with space.Gap blank rows between each two, the way a pane sets its own blocks apart. Empty blocks are left out, so a missing block never doubles a gap.
func Bytes ¶
Bytes returns a byte count in the largest unit that fits, the way every screen shows a size: 512 bytes, 1.5 KB, 5 MB.
func Carry ¶
Carry carries the styling of lines cut from one text over the breaks between them, in place. Each line opens with the style sequences in force at the previous line's end, and a line that leaves a style in force ends with a reset, so every line keeps the style it had in the text.
func ChartInk ¶
ChartInk returns the ink for a stretch at the recency, from quiet for the oldest stretch to bright for the newest, along an eased gradient.
func Clock ¶
Clock returns a running time in whole seconds with every part it has reached: seconds, then minutes and seconds, then hours, minutes and seconds. A part never leaves once it shows, so the text only grows as the time runs, and a time that has run for hours shows to the second as it did in its first minute.
func Divider ¶
Divider returns a dashed rule that is width cells wide, with the theme's info mark in its middle by the same rule that centers text: an odd dash goes to the right of the mark. A rule too narrow for the mark has only dashes.
func Facts ¶
Facts returns the facts in two columns that fit in width cells, laid out like a form's labels and values: each label dim in a column as wide as the widest label, ColumnGap blank columns, then its value. A value wraps in its own column, and a label too wide for its column wraps in it. A width too narrow for both columns puts each value under its label. A fact with a value that shows nothing, such as an empty or only styled value, is left out.
func Fit ¶
Fit pads a line with blanks to width columns. It never cuts: the caller wraps its text to the width first (Wrap), so a line wider than the width comes back unchanged.
func Height ¶
Height returns the number of lines that a row takes with its columns at the widths: one, and more where a cell is wider than its column and wraps in it. Sizes has the cells' widths measured beforehand, so only a cell wider than its column is wrapped to count its lines. It counts the lines exactly the way Grid.Lines paints them.
func Item ¶
Item returns a list item's lines that fit in width cells: the mark, such as a number or a bullet, a blank, then the first line, with every other line and every wrapped part set under the text after the mark. Without a mark the lines are only wrapped.
func Lay ¶
Lay returns the rows at the width with the stack over the corner. Each covered row splits at the stack's edge: the stack's row takes the columns it covers, and the row keeps the rest. This composes the two without cutting any text out of the stack. The caller sizes the stack to fit the rows, and stack rows past the last row only guard against a taller stack. It leaves the given rows unchanged.
func LeftCells ¶
LeftCells returns a row's cells in the left columns, laid out in width cells: first the untitled columns' cells, each wrapped on its own lines, then the other cells as labeled values under their column titles (Facts). Empty cells are left out.
func Natural ¶
Natural returns each column's natural width over the rows: its title's width in capitals or its widest cell's width, whichever is larger.
func Nested ¶
Nested returns the text set in by depth levels of space.Lead cells, for a row in a tree such as a report's rows under a row.
func On ¶
On returns the moment the way a sentence puts it after a verb, in the user's own zone: at the time for today, on the day for this year, otherwise on the day and the year. It returns empty text for a zero time.
func Paint ¶
Renders text in the style, restarting the style after every reset inside the text so its background runs unbroken
func PaintTimeline ¶
PaintTimeline returns a chart's rows painted in the theme: each series in its color, faded toward the background by its stretch's age, the selected row's label bright, and the axis and the pluses between counts quiet.
func Relative ¶
Relative returns a future or past time in words: now, in 5 minutes, 3 hours ago, or the day and time beyond a day either way. It returns empty text for a zero time.
func Section ¶
Section returns a block under a heading in a list's group heading style: the heading's row, then the lines. It returns nil for empty lines, so an empty section never shows a bare heading.
func Settled ¶
Settled returns the offset where the text's block that later text can still change starts, so everything before it can print unchanged: the start of the last paragraph or heading after a blank line, outside fenced code and raw HTML. It returns 0 while no block is closed yet.
func ShareBar ¶
ShareBar returns a bar for n's share of top, where top fills width cells, and at least one cell for a count above zero.
func Spark ¶
Spark returns the counts as one row of bars in eight heights, the highest count the tallest and a zero count blank.
func Stretches ¶
Stretches returns the stretches around the moments, oldest first, at the finest grain with stretches that fit the rows, and in days for a month of days regardless of the rows. Empty stretches are left out, and so are moments without a time.
func StyleConfig ¶
func StyleConfig(t theme.Theme) ansi.StyleConfig
StyleConfig returns a glamour style made from the theme, starting from the dark, light or ASCII set.
func Table ¶
Table renders a read-only table, such as a report: the titles, then the rows, each starting at column zero. Every cell shows whole: a cell wider than its staying column wraps over more lines, and each left column's cells show under their row, set in by space.Lead (LeftCells).
func Timeline ¶
func Timeline(stretches []Stretch, width, inset int, glyphs ChartGlyphs) [][]ChartRun
Timeline returns a chart's rows for the stretches at the width: one row per stretch, then the running total's trail. Each row has the stretch's label, the axis, a bar per series as long as its share of the busiest stretch, at least one cell for every series in the stretch, the count per series, now beside the newest stretch and peak beside the busiest stretch. The axis is a vertical line, so inset blank columns sit on each side of it: the inset that a frame keeps inside its border. The bars, the counts and the notes are ColumnGap apart, like a table's columns. Every row fits the width, and a width too small for all of it gives way in this order, never by giving up a blank: the notes leave first, then the counts while the bars stay at least barLeast wide, then the axis and the bars leave with the trail and the labels keep their counts beside them, then the counts leave too, and a width too small for the labels leaves the rows empty.
func Titled ¶
Titled reports whether any column has a title, so a table with these columns has a row of titles above its rows.
func ToolGlyph ¶
ToolGlyph returns the glyph for a tool icon that a tool registers, such as "read" or "shell", and the plain tool glyph for an unknown icon.
func Unmarked ¶
Unmarked returns the markdown's text without its markup, with the words and the lines kept. Emphasis next to emphasis comes out on a second pass, because one pass leaves the second of two marks that share the space between them.
func Untab ¶
Untab returns the text with each tab expanded to blanks up to the next tab stop, counted in cells from the start of the tab's line. A card measures its text in cells, and a tab has no width until the terminal draws it, so text with tabs would not line up.
func When ¶
When returns the moment in the user's own zone: the time for today, the day for this year, otherwise the day and the year. It returns empty text for a zero time.
func Widest ¶
Widest returns each column's natural width from cell widths measured beforehand row by row: its title's width in capitals or its widest cell's width, whichever is larger. A long table measures each cell once and lays its columns out again from those widths. The title is measured in capitals because the titles row shows it that way, and a capital can be wider than its small letter.
Types ¶
type ChartGlyphs ¶
type ChartGlyphs struct {
// Fill is a bar's cell, Axis is the axis, and Dot and Star are the trail's marks
Fill, Axis, Dot, Star string
// Now and Peak are the notes beside the newest stretch and the busiest stretch
Now, Peak string
}
ChartGlyphs has a chart's marks.
func ChartGlyphsOf ¶
func ChartGlyphsOf(t theme.Theme) ChartGlyphs
ChartGlyphsOf returns a chart's marks in the theme: its bar fill and separator, with ASCII marks when the theme draws only in ASCII.
type ChartPart ¶
type ChartPart int
ChartPart is what a run in a chart row draws.
const ( // PartLabel is a stretch's label PartLabel ChartPart = iota // PartAxis is the axis between the labels and the bars PartAxis // PartBar is one series' bar PartBar // PartCount is one series' count, or the plus between two counts PartCount // PartNote is now beside the newest stretch, or peak beside the busiest stretch PartNote // PartTrail is the running total's trail under the bars PartTrail // PartGap is blank cells PartGap )
type ChartRun ¶
type ChartRun struct {
Text string
Part ChartPart
// Series is the series that the run draws or counts, and -1 when it has no series
Series int
// Recency is the stretch's recency, from ChartLeadIn for the oldest stretch to 1 for the newest stretch. A painter fades older
// stretches by it
Recency float64
}
ChartRun is one run of cells in a chart row.
type Column ¶
type Column struct {
Title string
// Flex is the column's share of the width that the shown columns leave spare. A column without a share is as wide as its widest cell
// or its title, whichever is wider.
Flex int
// Right aligns the cells to the right, for numbers.
Right bool
// Rank orders the columns that leave when the width cannot fit every column at its natural width. The column with the lowest rank
// leaves first, and of two columns with one rank the later column leaves first, so a table without ranks loses its columns from the
// last toward the first. Zero is the default rank. A column that should leave before the default, such as a long free-text column, has
// a negative rank, and a column that should outlast the default has a positive rank. The first column, and every column ranked Stays,
// never leaves. A left column's cells are not lost: a list shows them first in the selected row's detail pane, and a static table shows
// them under each row (LeftCells).
Rank int
}
Column is a table column: its title, how its cells align, its share of the spare width, and its rank among the columns that leave a width too narrow for every column. A table never cuts a cell. Every column it shows is as wide as its widest cell and its title, and a column that does not fit leaves whole.
type Edit ¶
Edit is the selected row's cell that is being typed into in place: its text with the caret at a rune offset, shown selected until the first keystroke.
type Grid ¶
type Grid struct {
// contains filtered or unexported fields
}
Grid is a table at column widths laid out beforehand, which paints each row on its own in styles made once. A long table paints only its visible rows, and its columns, measured over every row, stay still while it scrolls. Each line is width columns: lead columns before the first cell, with the selected row's mark in their first column on the row's first line, then the cells. A cell wider than its column wraps in it, so its row takes more lines. A lit row's background runs the full width on every line of the row.
func NewGrid ¶
NewGrid returns a grid for the columns at the widths from Columns or Spread. Lead is the number of columns before the first cell, and width is every line's width with the lead.
func (*Grid) EditSpot ¶
EditSpot returns where the edit's caret shows in a row that takes tall lines: its column, counted from the line's first column, and its line in the row. The typed text shows in the edit's column, or across every shown column when that column left the width.
func (*Grid) Height ¶
Height returns the number of lines that Lines paints a row's cells in at the grid's column widths.
func (*Grid) Lines ¶
Lines returns a row's lines painted as marked, the cell that the edit names shown as it is being typed into on the lit row. A row takes one line, and more where a cell is wider than its column and wraps in it (Height counts them). Every line of the row has the row's look, and only the first line has the row's mark.
type Grip ¶
type Grip struct {
// contains filtered or unexported fields
}
Grip is a scrollbar's pointer state, which every scrolling pane keeps beside its bar: the bar row under the pointer, which lights the part there, and the thumb row held through a drag. A pane dresses its bar with the grip (On) whenever it builds the bar.
func (*Grip) Drag ¶
Drag follows the pointer at bar row y while the thumb is held, wherever the pointer is, and returns the offset that keeps the held thumb row under the pointer. pressed is false when the pointer moved with no button down: the release was missed, so the thumb is released. It reports false while the thumb is not held.
func (*Grip) Drop ¶
Drop forgets the pointer and releases the thumb, for a bar that is no longer drawn, and reports whether either was set.
func (*Grip) Hover ¶
Hover puts the pointer on bar row y while on is true, and off the bar otherwise, and reports whether the bar's lit part changed, which is the only case in which the pane draws again.
func (Grip) On ¶
On returns the bar with the grip's pointer and hold on it, so the bar lights the part the pointer is on.
func (*Grip) Press ¶
Press takes a left press on bar row y. A cap returns its page request, which the pane carries out with its own paging. A press on the track grabs the thumb and returns ScrollJump with the offset to show: the same offset on the thumb, and beside it the offset that puts the thumb's middle under the pointer. The drag then follows the pointer until the button comes up (Drag).
type Layout ¶
type Layout struct {
// Xs has each column's left edge, counted from the first cell, and Widths has its width. A column without width takes no room in a row,
// not even the gap before it, so the next column starts where it would have started. A column that left has no width, and neither has
// a column without a title, cells or a share of the spare width.
Xs, Widths []int
// Left marks the columns that left because the width cannot fit them. A column without a title or cells never counts as left.
Left []bool
}
Layout is a table's columns fitted to a width.
func Columns ¶
Columns returns the columns fitted to cells width columns wide over the rows (Spread).
func Spread ¶
Spread fits the columns, at the natural widths that Natural or Widest gave them, into cells width columns wide. While the columns do not fit, they leave whole one by one in rank order (Column.Rank). If the columns that stay are still wider than the width, their widest column narrows until they fit and wraps its cells over more lines. The flex columns share the width that the columns leave spare. Which columns leave depends on the width and the natural widths only, so it is the same for every row.
type Markdown ¶
type Markdown struct {
// contains filtered or unexported fields
}
Markdown is a markdown renderer cached per theme and width. It lays out the tables in the markdown itself, and glamour renders the rest.
func NewMarkdown ¶
func NewMarkdown() *Markdown
NewMarkdown returns a markdown renderer with an empty cache.
func (*Markdown) Lines ¶
Lines returns the markdown's lines at width in the theme, without the blank lines around them, except the space.Gap blank rows that set a horizontal rule at either end apart from whatever a card shows beside the lines. A tab in the source takes the columns up to its next stop, no line is wider than the width, so a pane's text never runs on past its last text column (see fitted), and a rule keeps space.Gap rows from the text around it (see ruledApart and layTable).
type Moment ¶
Moment is an event that a timeline draws: its time, its series, and its index in the owner's list.
type ScrollHit ¶
type ScrollHit int
ScrollHit is the request that a click on a scrollbar makes.
const ( // ScrollNone is a click that misses the scrollbar, or a click while nothing scrolls. ScrollNone ScrollHit = iota // ScrollPageUp is a page toward the start, from the top cap. ScrollPageUp // ScrollPageDown is a page toward the end, from the bottom cap. ScrollPageDown // ScrollJump is a press on the track, which grabs the thumb: on the thumb it keeps the offset, and beside it the thumb's middle jumps // to the pressed place. ScrollJump )
type ScrollPart ¶
type ScrollPart int
ScrollPart is a part of a scrollbar, which lights while the pointer is on it.
const ( // PartNone means that the pointer is off the bar, or that nothing scrolls. PartNone ScrollPart = iota // PartUp is the top cap, which pages toward the start. PartUp // PartDown is the bottom cap, which pages toward the end. PartDown // PartTrack is the track beside the thumb. PartTrack // PartThumb is the thumb. PartThumb )
type Scrollbar ¶
type Scrollbar struct {
// Height in rows, the total content lines, the first visible line and the visible line count
Height, Total, Offset, Visible int
// Lines that start a turn, ticked along the track, and lines holding a search match
Marks, Hits []int
// Chords that page up and down, shown on the caps
Up, Down string
// Point is one past the bar row under the pointer, and zero while the pointer is off the bar. Held reports whether the thumb is held
// through a drag. Together they light the part of the bar that the pointer is on.
Point int
Held bool
}
Scrollbar is the scrollbar beside scrolling content: a column with one cell per row, caps that show the paging keys, and a track that marks turns and matches, with a thumb as long as the content's visible share. Every scrolling pane draws it in its rail's last column, and a Grip keeps its pointer state.
The bar shows only while the content runs past the visible rows. Three rows or more show both caps and a track between them, two rows show only the caps, and one row shows the one cap that pages toward the content left to see: down until the end, then up.
func (Scrollbar) Column ¶
Column returns the scrollbar's cells from the top, one per row, blank while nothing scrolls. It draws from the theme's colors and glyphs, so the bar reads the same way in the ASCII glyph set and the plain style.
func (Scrollbar) DragTo ¶
DragTo returns the offset that puts the thumb's held row under bar row y. The pointer may be anywhere, above or under the bar included: the thumb stops at its track's ends. An offset that keeps the thumb in place stays unchanged, so grabbing the thumb never moves the content, and the thumb at either end of its track shows the content's start or its end exactly. Between the ends, it returns the offset that puts the thumb nearest the pointer.
func (Scrollbar) Grab ¶
Grab returns the thumb row that a press on row y grabbed, or the thumb's middle for a press on the track beside it, where the thumb then jumps.
func (Scrollbar) Hit ¶
Hit returns the meaning of a press on scrollbar row y, and for a press on the track the offset it moves to: the offset unchanged on the thumb, and beside the thumb the offset that puts the thumb's middle on the row pressed.
func (Scrollbar) Lit ¶
func (s Scrollbar) Lit() ScrollPart
Lit returns the part of the bar that lights: the thumb while it is held, otherwise the part under the pointer.
func (Scrollbar) OnTrack ¶
OnTrack reports whether row y lies on the track between the caps while the content scrolls, where a press grabs the thumb.
func (Scrollbar) Part ¶
func (s Scrollbar) Part(y int) ScrollPart
Part returns the part of the bar on row y, PartNone off the bar or while nothing scrolls.
type Scrolled ¶
type Scrolled struct {
// contains filtered or unexported fields
}
Scrolled keeps a body's rows laid beside a scrollbar at the last paint. The scrollbar's column is drawn again only when the scrollbar changed, and each row is composed again only when its text or its scrollbar cell changed, so a view that only moved a spinner composes one row again.
func (*Scrolled) Lay ¶
Lay returns the body's rows beside the scrollbar in a pane w columns wide, one row for each scrollbar row: each row fitted to the pane's width less the bar, then the scrollbar's cell in the pane's last column. A row, a highlighted row's background included, ends before the rail, so the rail's blank columns stay blank and set every row and every highlight apart from the bar.
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream renders the markdown for a text that grows at its end, such as a streaming reply. Each run of blocks that no later text can change is rendered once and kept. Only the open block after them is rendered again as the text grows, so a long reply costs the same per word as a short one. The lines equal the full text rendered at once.
type Stretch ¶
type Stretch struct {
// Start is its first moment, and End is the next stretch's first moment
Start, End time.Time
Label string
// Counts has each series' moment count in the stretch, and Moments has the moments, oldest first
Counts []int
Moments []Moment
}
Stretch is a stretch in a timeline, with the moments in it.