linebuf

package
v0.0.0-alpha.18 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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 PlainText

func PlainText(s string, method ansi.Method) string

PlainText returns the text of line s without its styles, with its tabs.

func TextLen

func TextLen(s string) int

TextLen returns how many bytes the text of s is, as PlainText gives it with ansi.GraphemeWidth.

func Wrap

func Wrap(s string, method ansi.Method, w int) string

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.

func ZeroStyle

func ZeroStyle(s *uv.Style) bool

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

func (c Change) End() Pos

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

func (c Change) Map(p Pos, after bool) Pos

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.

func (Change) MapSpan

func (c Change) MapSpan(a, b Pos) (from, to Pos)

MapSpan returns where the run of text from a to b, either way round, is after the change, its ends mapped so that text put in at either end is left out of it, and an empty one mapped as Map maps a position.

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

func (l *ChangeLog) Add(rev uint64, c Change)

Add notes change c, which made revision rev.

func (*ChangeLog) Reset

func (l *ChangeLog) Reset(rev uint64)

Reset notes a change at revision rev that positions cannot be followed through.

func (*ChangeLog) Since

func (l *ChangeLog) Since(rev, now uint64) ([]Change, bool)

Since returns the changes made after revision rev, up to revision now, in order, and true, or false when positions from then cannot be followed to now: the log was reset since, or more changes were made than it keeps. Lines appended move no position, and need no change.

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

func (r Index) After(row, cell int, scratch *[]int) Pos

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

func (r Index) AtColumn(row, column int, scratch *[]int) (Pos, bool)

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

func (r Index) BlockText(x0, y0, x1, y1 int) string

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

func (r Index) CaretIn(p Pos, end bool, scratch *[]int) (row, cell int)

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

func (r Index) CellOf(p Pos, end bool, scratch *[]int) (row, cell int)

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

func (r Index) CharAt(row, cell int, scratch *[]int) (Pos, bool)

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

func (r Index) Covered(row RowSpan, from, to Pos) (lo, hi int, ok bool)

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

func (r Index) EndsRow(p Pos, row int, scratch *[]int) bool

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

func (r Index) InSight(line, sub, h, col int, out []RowSpan) []RowSpan

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

func (x Index) LineAt(row int) (line, sub int)

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

func (r Index) PosAt(row, cell int, scratch *[]int) Pos

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

func (r Index) RowAt(y int, scratch *[]int) (line, a, end int)

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

func (r Index) RowCells(y int, m *RowMemo) string

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

func (r Index) RowEnd(row int, scratch *[]int) (p Pos, wraps bool)

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

func (x Index) RowsBefore(i int) int

RowsBefore returns how many rows the lines before line i take.

func (Index) RowsOf

func (x Index) RowsOf(i int) int

RowsOf returns how many rows line i takes.

func (Index) Total

func (x Index) Total() int

Total returns how many rows all the lines take.

func (Index) Uniform

func (x Index) Uniform() bool

Uniform reports whether every line takes one row.

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.

func (Measure) Width

func (m Measure) Width(i int) int

Width returns how many cells line i takes.

type Mode

type Mode uint8

Mode is how lines wider than a viewport are fitted to it.

const (
	// None shows lines whole, a row each.
	None Mode = iota
	// Words wraps lines at the spaces between words, and after hyphens,
	// breaking a word only when it is wider than the viewport.
	Words
	// Chars wraps lines at the viewport's width, wherever it falls.
	Chars
)

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.

func MarkCells

func MarkCells(s string, method ansi.Method, marks, dst []Overlay) []Overlay

MarkCells appends to dst marks, given in bytes of the text of line s as PlainText returns it, as the cells of the line they cover, in order, dropping those that cover none or set no style, and those overlapping one before them.

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.

func EndOf

func EndOf(at Pos, text string) Pos

EndOf returns where text ends, put in at at.

func (Pos) Before

func (p Pos) Before(q Pos) bool

Before reports whether p comes before q.

func (Pos) Compare

func (p Pos) Compare(q Pos) int

Compare returns -1 when p comes before q, 1 when it comes after, and 0 when they are the same.

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.

func (*SGRs) Of

func (c *SGRs) Of(s *uv.Style) string

Of returns the escape sequence of style s.

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.

func (Snapshot) Len

func (s Snapshot) Len() int

Len returns the number of lines.

func (Snapshot) Line

func (s Snapshot) Line(i int) string

Line returns line i, which must be one of the snapshot's.

func (Snapshot) Lines

func (s Snapshot) Lines(from int) iter.Seq2[int, string]

Lines returns an iterator over the lines from line from on, and their numbers.

func (Snapshot) String

func (s Snapshot) String() string

String returns the lines, joined with newlines.

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

func (b *Store) AppendLines(texts ...string)

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

func (b *Store) AppendText(s string)

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

func (t *Store) ByteAt(i int, method ansi.Method, x int) int

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

func (t *Store) CellOf(i int, method ansi.Method, col int) int

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

func (b *Store) CellText(i int, method ansi.Method, a, end int) string

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

func (t *Store) Clamp(p Pos) Pos

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

func (b *Store) Clear()

Clear removes every line.

func (*Store) CountRows

func (b *Store) CountRows(method ansi.Method, mode Mode, w, most int) int

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

func (b *Store) CutRow(i int, method ansi.Method, a, end int, st *RowStyle) string

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

func (t *Store) Edit(edits ...TextEdit) (Change, bool)

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

func (t *Store) End() Pos

End returns the end of the store's text.

func (*Store) Index

func (b *Store) Index(method ansi.Method, mode Mode, width int) 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

func (b *Store) Kept(method ansi.Method, mode Mode, width int) bool

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

func (b *Store) Len() int

Len returns the number of lines.

func (*Store) Line

func (b *Store) Line(i int) string

Line returns line i as it was given, with its styles.

func (*Store) Measure

func (b *Store) Measure(method ansi.Method) 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

func (t *Store) Offset(p Pos) int

Offset returns the byte of Store.String that p, as Store.Clamp returns it, is at.

func (*Store) PosOf

func (t *Store) PosOf(off int) Pos

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

func (t *Store) Replace(edits ...TextEdit)

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

func (t *Store) Restore(s Snapshot)

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

func (b *Store) Rev() uint64

Rev counts the changes to the store, for those that keep what they worked out from its lines.

func (*Store) RowStarts

func (b *Store) RowStarts(i int, method ansi.Method, w int, mode Mode, scratch *[]int) []int

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

func (t *Store) Snapshot() 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.

func (*Store) String

func (b *Store) String() string

String returns the lines, joined with newlines.

func (*Store) Text

func (t *Store) Text(i int) string

Text returns the text of line i without its styles, with its tabs, as PlainText gives it with ansi.GraphemeWidth: the line itself when it is only printable ASCII, which for a long line is known from measuring it, without reading it again.

type TextEdit

type TextEdit struct {
	From, To Pos
	Text     string
}

TextEdit is a change to the text of a store: the text from From up to To replaced with Text.

Jump to

Keyboard shortcuts

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