Documentation
¶
Overview ¶
Package linebuf keeps the lines of text a viewport shows, and works out what it draws of them: how wide each line is, the rows it wraps to, and the cells of a row as a string that draws them, as fast as a line of a log a million lines long needs. It makes the edits to the lines, follows positions in them through the edits, and finds the positions that the cells of the rows show.
Index ¶
- Constants
- func PlainText(s string, method ansi.Method) string
- func TextLen(s string) int
- func Wrap(s string, method ansi.Method, w int) string
- func ZeroStyle(s *uv.Style) bool
- type Change
- type ChangeLog
- type Index
- func (r Index) After(row, cell int, scratch *[]int) Pos
- func (r Index) AtColumn(row, column int, scratch *[]int) (Pos, bool)
- func (r Index) BlockText(x0, y0, x1, y1 int) string
- func (r Index) CaretIn(p Pos, end bool, scratch *[]int) (row, cell int)
- func (r Index) CellOf(p Pos, end bool, scratch *[]int) (row, cell int)
- func (r Index) CharAt(row, cell int, scratch *[]int) (Pos, bool)
- func (r Index) Covered(row RowSpan, from, to Pos) (lo, hi int, ok bool)
- func (r Index) EndsRow(p Pos, row int, scratch *[]int) bool
- func (r Index) InSight(line, sub, h, col int, out []RowSpan) []RowSpan
- func (x Index) LineAt(row int) (line, sub int)
- func (r Index) PosAt(row, cell int, scratch *[]int) Pos
- func (r Index) RowAt(y int, scratch *[]int) (line, a, end int)
- func (r Index) RowCells(y int, m *RowMemo) string
- func (r Index) RowEnd(row int, scratch *[]int) (p Pos, wraps bool)
- func (x Index) RowsBefore(i int) int
- func (x Index) RowsOf(i int) int
- func (x Index) Total() int
- func (x Index) Uniform() bool
- type Measure
- type Mode
- type Overlay
- type Pos
- type RowMemo
- type RowSpan
- type RowStyle
- type SGRs
- type Snapshot
- type Store
- func (b *Store) AppendLines(texts ...string)
- func (b *Store) AppendText(s string)
- func (t *Store) ByteAt(i int, method ansi.Method, x int) int
- func (t *Store) CellOf(i int, method ansi.Method, col int) int
- func (b *Store) CellText(i int, method ansi.Method, a, end int) string
- func (t *Store) Clamp(p Pos) Pos
- func (b *Store) Clear()
- func (b *Store) CountRows(method ansi.Method, mode Mode, w, most int) int
- func (b *Store) CutRow(i int, method ansi.Method, a, end int, st *RowStyle) string
- func (t *Store) Edit(edits ...TextEdit) (Change, bool)
- func (t *Store) End() Pos
- func (b *Store) Index(method ansi.Method, mode Mode, width int) Index
- func (b *Store) Kept(method ansi.Method, mode Mode, width int) bool
- func (b *Store) Len() int
- func (b *Store) Line(i int) string
- func (b *Store) Measure(method ansi.Method) Measure
- func (t *Store) Offset(p Pos) int
- func (t *Store) PosOf(off int) Pos
- func (t *Store) Replace(edits ...TextEdit)
- func (t *Store) Restore(s Snapshot)
- func (b *Store) Rev() uint64
- func (b *Store) RowStarts(i int, method ansi.Method, w int, mode Mode, scratch *[]int) []int
- func (t *Store) Snapshot() Snapshot
- func (b *Store) String() string
- func (t *Store) Text(i int) string
- type TextEdit
Constants ¶
const CheckpointEvery = 1024
CheckpointEvery is how many cells apart the checkpoints of a long line are, so that cutting a row out of a line megabytes long scans little more than the row.
const LongLineBytes = 8 << 10
LongLineBytes is how long a line must be for a viewport to keep checkpoints of it, and the rows it wraps to, rather than scan it from its start for each row it shows of it, every frame.
Variables ¶
This section is empty.
Functions ¶
func TextLen ¶
TextLen returns how many bytes the text of s is, as PlainText gives it with ansi.GraphemeWidth.
func Wrap ¶
Wrap returns text s with its lines wrapped at words to w cells, measured with method, into the rows a viewport wrapping with Words shows them in: a newline starts each row past a line's first, in place of the spaces that would hang off the row before it, and the spaces that end a line past w are dropped. A tab counts as a space a cell wide rather than up to the next stop, as the canvas draws none for it in a text, where a viewport draws spaces. A w of zero or less leaves s as it is, as does a text with no line longer than w in bytes, which is returned without a copy.
Types ¶
type Change ¶
type Change struct {
// contains filtered or unexported fields
}
Change is what Store.Edit did to a store's text, to follow positions through it with. Its methods take positions in the text as it was before the change, and give them in the text as it is after. The zero value is a change that moves no position.
func (Change) End ¶
End returns where the text put in by the change's last edit ends, in the text after it, which is where a caret goes after typing.
func (Change) Map ¶
Map returns where position p of the text before the change is after it: unchanged before the text changed, moved with the text after it, and at the end of the text put in when it was within the text replaced. A position where text was put in stays before that text, or with after, goes after it, as a caret where text is typed does.
type ChangeLog ¶
type ChangeLog struct {
// contains filtered or unexported fields
}
ChangeLog is the last few changes made to a store by its edits, each with the Store.Rev it made, to follow positions through from one revision to a later one. The zero value is ready to use.
func (*ChangeLog) Reset ¶
Reset notes a change at revision rev that positions cannot be followed through.
type Index ¶
type Index struct {
// contains filtered or unexported fields
}
Index is how many rows the lines of a store take, wrapped to a width with a mode, as they were when it was made, and where in them the text is: the positions in the text that cells of the rows show, and the cells that positions are drawn at. Rows are counted over all of the lines, wrapped, from 0, and the cells of a row from its first.
func (Index) After ¶
After returns the position after the character drawn at cell of row, as PosAt finds it: where the next character starts, or the end of the row, or PosAt's own for a cell past the text of the row.
func (Index) AtColumn ¶
AtColumn returns the position on row at the character boundary nearest column of the row, never inside a wide character or a tab, or the end of the row, as RowEnd has it, for a column at or past the row's cells, and whether that is the place the row's line wraps after it.
func (Index) BlockText ¶
BlockText returns the text of the cells from x0 up to x1, inclusive, of each row from y0 up to y1, inclusive, as laid out, a line to a row, with the spaces at the end of each left out. The rows up to y1 must be rows of r.
func (Index) CaretIn ¶
CaretIn is CellOf with a cell past the width of a wrapped row, among the spaces that hang off it or past them, at the column past the width, as a caret is drawn past the row's text.
func (Index) CellOf ¶
CellOf returns the row and the cell of it that position p, clamped to the text, is drawn at. A position where a line wraps is at the start of the row after, or with end, as a caret at the end of a row is, past the cells of the row before.
func (Index) CharAt ¶
CharAt returns the position of the character drawn at cell of row, and whether there is one: none for a cell past the text of its row.
func (Index) Covered ¶
Covered returns the cells of row, one of those in sight, that the text from from up to to takes, from lo up to hi, cells of its line, and whether it takes any. A row the text runs on past has math.MaxInt for hi, for the line break or the wrap after it.
func (Index) EndsRow ¶
EndsRow reports whether p, clamped, is the end of row, a row its line wraps after: the place it wraps, which a caret at the end of the row is at.
func (Index) InSight ¶
InSight appends to out the rows in sight of a viewport h rows tall whose top row is row sub of line, and returns it: of a line the rows leave whole, the cells from column col on, and of one they wrap, each of its rows, as many cells of each as the rows are wide at most.
func (Index) LineAt ¶
LineAt returns the line that row is in, and how far into the line's rows it is, for a row of the lines.
func (Index) PosAt ¶
PosAt returns the position in the text of the character drawn at cell of row: the end of the row for a cell past its text, which for a row a line wraps at is where the next row starts, the start of the text for a row before the first, and its end for one past the last.
func (Index) RowAt ¶
RowAt returns the line that row y shows, and the cells of it the row takes, from a up to end, which for its last row is past the end of the line. Wrapped rows are worked out in scratch.
func (Index) RowCells ¶
RowCells returns the cells of row y, all of it from its first cell, kept in m for the next call, as reading the cells of a selection asks for each cell of a row in turn.
func (Index) RowEnd ¶
RowEnd returns the end of row, as the end key moves a caret to it, and whether it is the place its line wraps after it: the end of the line on its last row, and on a row the line wraps after, the place it wraps, where the next row starts, after any spaces that hang off a row wrapped at words. A tab the line wraps inside has no place there, and the row ends before it.
func (Index) RowsBefore ¶
RowsBefore returns how many rows the lines before line i take.
type Measure ¶
type Measure struct {
Widest int
// contains filtered or unexported fields
}
Measure is how wide the lines of a store are, measured with a width method, as they were when it was made.
type Overlay ¶
type Overlay struct {
Start, End int // cells of the line, End exclusive; bytes as given to [MarkCells]
SGR string
}
Overlay is a style drawn over part of a row, such as a mark or the style of its line, as the escape sequence that sets it.
type Pos ¶
type Pos struct{ Line, Col int }
Pos is a place in the text of a store's lines: a line, and a byte of the line's text as PlainText gives it with ansi.GraphemeWidth, without its styles, a tab one byte. It is between two glyphs, or at an end.
type RowMemo ¶
type RowMemo struct {
// contains filtered or unexported fields
}
RowMemo is the row whose cells [Rows.RowCells] last cut.
type RowSpan ¶
type RowSpan struct{ Line, Sub, A, B int }
RowSpan is a row of the lines in sight: the cells from A up to B of line Line, and which of the line's rows, Sub, it is.
type RowStyle ¶
type RowStyle struct {
Line string
Marks []Overlay
Fill int
Pens *SGRs
Pass bool // keep the commands that [escape.PassThrough] keeps
}
RowStyle is what is drawn over a row of a line: Line under the text's own styles, across all of the row, which it pads with spaces to fill Fill cells, and Marks over the cells they cover. Pens keeps the escape sequences of the text's styles, which are drawn again over Line after each of its own.
type SGRs ¶
type SGRs struct {
// contains filtered or unexported fields
}
SGRs keeps the escape sequences of the styles that text was drawn in, as making one allocates, and the same few styles come up row after row, such as the colors of a log's levels. A nil SGRs keeps none.
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
Snapshot is the lines of a store as they were when Store.Snapshot took it, which they stay as the store goes on changing. Its methods read only what the nodes hold of their lines, which is never written again once a snapshot shares them, so that they may be called from any goroutine while the store is changed. The zero value holds no lines.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store holds the lines of a buffer, and what it works out about them, such as how wide each is, so that appending a line to a store of a million costs about as much as appending one to a store of ten. A style left open at the end of a line carries on into the next.
The lines are kept in a balanced tree, whose nodes keep what they sum up about their lines, such as how many rows they wrap to, so that finding a line or a row costs a node a level, and keep what is worked out about their lines, so that a change works out again only the lines it changed.
The zero value is an empty store, ready to use. A Store is not safe for use by more than one goroutine at a time.
func (*Store) AppendLines ¶
AppendLines adds the lines of each of texts in turn, as Store.AppendText does: a few a line at a time, and many made into a tree of their own, joined to the end of the lines.
func (*Store) AppendText ¶
AppendText adds the lines of s, split at its newlines, which is one empty line for "". A newline at the end of s ends its last line.
func (*Store) ByteAt ¶
ByteAt returns the byte of the text of line i, as PlainText gives it, of the glyph drawn at cell x of the line, measured with method: its first byte for each of the cells of a wide glyph or a tab, and the end of the text for a cell past the line's last glyph. A long line is scanned from the checkpoint before x.
func (*Store) CellOf ¶
CellOf returns the cell that byte col of the text of line i, as PlainText gives it, is drawn at, measured with method: the first cell of the glyph it is in, the cell of the glyph before it for a combining mark drawn on that, and the cell after the line's last glyph for the end of the text. A long line is scanned from the checkpoint before col.
func (*Store) CellText ¶
CellText returns the text of the glyphs of line i that start in the cells from a to end, inclusive, as [cellText] does.
func (*Store) Clamp ¶
Clamp returns p moved to the nearest place in the store's text: the start for a line before the first, the end for one past the last, the end of the line for a byte past it, and the start of the glyph, such as a character of many bytes or a grapheme cluster, that a byte falls in.
func (*Store) CountRows ¶
CountRows returns how many rows the lines of the store take wrapped w cells wide with mode, measured with method, counting no further than most, for whether they fill a height without counting all of them.
func (*Store) CutRow ¶
CutRow returns the cells from a up to end of line i, measured with method, as a string that draws them on its own, as [cutRow] does.
func (*Store) Edit ¶
Edit makes edits at once, as one change, and returns it: each clamped, with its ends swapped when From comes after To, and with the positions of the store before any of them, in any order, and sorted in place. Of two that overlap, rather than touch, the one given later is dropped, and text put in at the same place by more than one goes in in the order given. It reports whether the text changed: not when every edit puts nothing in place of nothing, in a store with lines, which takes the change as though it held one empty line.
func (*Store) Index ¶
Index returns how many rows the store's lines take, wrapped to width with mode and measured with method, working out only the lines not worked out yet for them: all of them the first time, in parallel.
func (*Store) Kept ¶
Kept reports whether the store keeps how wide its lines are, measured with method, and how many rows they take, wrapped to width with mode, so that Store.Measure and Store.Index cost a step.
func (*Store) Measure ¶
Measure returns how wide the store's lines are with method, measuring only the lines not measured yet: all of them the first time, in parallel.
func (*Store) Offset ¶
Offset returns the byte of Store.String that p, as Store.Clamp returns it, is at.
func (*Store) PosOf ¶
PosOf returns the place in the text that byte off of Store.String is at: that of the glyph it falls in, or of the next one when it falls in an escape sequence, the end of a line for its newline, and the end of the text past it.
func (*Store) Replace ¶
Replace makes edits to the store's text, all at once, as one change: each replaces the text from its From up to its To with its Text, split into lines at its newlines, a newline at its end starting an empty line, without the carriage return before a newline. The places of the edits must be as Store.Clamp returns them, for the text before any of them, each From at or before its To, and the edits in order, none overlapping the next, though they may touch. A store with no lines takes them as though it held one empty line.
The text after an edit is drawn in the style it was drawn in before: the escape sequences of the text removed, and of the lines between, are left in its place as one that sets the style they leave, when it is not the style already in force there. Text is put in as it is given, and may hold escape sequences of its own, as an appended line may.
Each edit works out again only the lines it adds, and an edit within the lines of one leaf of the tree changes the nodes on the path to it in place, where no snapshot shares them, and what they keep of the lines with them, so that the store is measured again in a step a level.
func (*Store) Restore ¶
Restore puts the store's lines back to those of snapshot s, in a step: the store shares the nodes of s, and what they keep of their lines, such as how wide each is, so that nothing is measured again. A snapshot of another store may be restored too, the two then sharing what they keep, which is safe while both are changed on one goroutine.
func (*Store) Rev ¶
Rev counts the changes to the store, for those that keep what they worked out from its lines.
func (*Store) RowStarts ¶
RowStarts returns the cells the rows of line i start at, wrapped to w with mode and measured with method: worked out in scratch, or kept for a long line, which must not be changed.
func (*Store) Snapshot ¶
Snapshot returns the store's lines as they are, in a step: the nodes of the tree are shared with it, and a change copies those it touches rather than change them in place.