segment

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NoLineBreak = LineBreakDecision(iota)
	AllowLineBreakBefore
	RequireLineBreakBefore
	RequireLineBreakAfter
)

Variables

This section is empty.

Functions

func GraphemeClusterIsEmoji

func GraphemeClusterIsEmoji(gc []rune) bool

GraphemeClusterIsEmoji checks whether the grapheme cluster represents an emoji.

func GraphemeClusterIsRegionalIndicator

func GraphemeClusterIsRegionalIndicator(gc []rune) bool

GraphemeClusterIsRegionalIndicator checks whether the grapheme cluster represents a regional indicator (usually rendered as a flag).

Types

type GraphemeClusterBreaker added in v0.3.0

type GraphemeClusterBreaker struct {
	// contains filtered or unexported fields
}

GraphemeClusterBreaker finds breakpoints between grapheme clusters in a sequence of runes.

func (*GraphemeClusterBreaker) ProcessRune added in v0.3.0

func (gb *GraphemeClusterBreaker) ProcessRune(r rune) (canBreakBefore bool)

ProcessRune determines whether the position before the rune is a valid breakpoint (starts a new grapheme cluster).

type GraphemeClusterIter added in v0.3.0

type GraphemeClusterIter struct {
	// contains filtered or unexported fields
}

GraphemeClusterIter segments text into grapheme clusters. A grapheme cluster is a user-perceived character, which can be composed of multiple unicode codepoints. For full details see https://www.unicode.org/reports/tr29/ version 13.0.0, revision 37. Copying the struct produces a new, independent iterator.

func NewGraphemeClusterIter

func NewGraphemeClusterIter(reader text.Reader) GraphemeClusterIter

NewGraphemeClusterIter initializes a new iterator. The iterator assumes that the first character it receives is at a break point (either the start of the text or the beginning of a new grapheme cluster). The input reader MUST produce valid UTF-8 codepoints.

func (*GraphemeClusterIter) NextSegment added in v0.3.0

func (g *GraphemeClusterIter) NextSegment(segment *Segment) error

NextSegment retrieves the next grapheme cluster.

type GraphemeClusterWidthFunc

type GraphemeClusterWidthFunc func(gc []rune, offsetInLine uint64) uint64

GraphemeClusterWidthFunc returns the width in cells for a given grapheme cluster.

type LineBreakDecision added in v0.6.0

type LineBreakDecision byte

type LineBreaker added in v0.6.0

type LineBreaker struct {
	// contains filtered or unexported fields
}

LineBreaker finds possible breakpoints between lines. This uses the Unicode line breaking algorithm from https://www.unicode.org/reports/tr14/

func (*LineBreaker) ProcessRune added in v0.6.0

func (lb *LineBreaker) ProcessRune(r rune) (decision LineBreakDecision)

ProcessRune finds valid breakpoints between lines.

type LineWrapConfig

type LineWrapConfig struct {
	MaxLineWidth    uint64 // Maximum number of cells per line, which must be at least one.
	AllowCharBreaks bool   // Allow breaks at grapheme cluster boundaries.
	WidthFunc       GraphemeClusterWidthFunc
}

LineWrapConfig controls how lines should be soft-wrapped.

type ReverseGraphemeClusterIter added in v0.3.0

type ReverseGraphemeClusterIter struct {
	// contains filtered or unexported fields
}

ReverseGraphemeClusterIter identifies valid breakpoints between grapheme clusters in a reversed-order sequence of runes. Copying the struct produces a new, independent iterator.

func NewReverseGraphemeClusterIter

func NewReverseGraphemeClusterIter(reader text.ReverseReader) ReverseGraphemeClusterIter

NewReverseGraphemeClusterIter constructs a new BreakIter from a runeIter that yields runes in reverse order.

func (*ReverseGraphemeClusterIter) NextSegment added in v0.3.0

func (g *ReverseGraphemeClusterIter) NextSegment(segment *Segment) error

NextSegment retrives the next grapheme cluster reading backwards. The returned locations are relative to the end of the text.

type Segment

type Segment struct {
	// contains filtered or unexported fields
}

Segment represents a sequence of runes from a larger text (for example a grapheme cluster).

func Empty

func Empty() *Segment

Empty returns a new, empty segment.

func (*Segment) Append added in v0.3.0

func (seg *Segment) Append(r rune) *Segment

Append appends a single rune to the end of the segment.

func (*Segment) Clear

func (seg *Segment) Clear() *Segment

Clear removes all runes from the segment.

func (*Segment) HasNewline

func (seg *Segment) HasNewline() bool

HasNewline checks whether a segment contains a line feed rune.

func (*Segment) IsWhitespace

func (seg *Segment) IsWhitespace() bool

IsWhitespace checks whether the segment contains only whitespace runes (spaces, tabs, etc).

func (*Segment) NumRunes

func (seg *Segment) NumRunes() uint64

NumRunes returns the number of runes in the segment.

func (*Segment) ReverseRunes

func (seg *Segment) ReverseRunes() *Segment

ReverseRunes reverses the order of the runes in the segment.

func (*Segment) Runes

func (seg *Segment) Runes() []rune

Runes returns the runes contained in the segment. Callers should not modify the returned slice.

type WrappedLineIter added in v0.3.0

type WrappedLineIter struct {
	// contains filtered or unexported fields
}

WrappedLineIter iterates through soft- and hard-wrapped lines.

func NewWrappedLineIter

func NewWrappedLineIter(wrapConfig LineWrapConfig, textTree *text.Tree, startPos uint64) WrappedLineIter

NewWrappedLineIter constructs a segment iterator for soft- and hard-wrapped lines.

func (*WrappedLineIter) NextSegment added in v0.3.0

func (iter *WrappedLineIter) NextSegment(segment *Segment) error

NextSegment retrieves the next soft- or hard-wrapped line. For hard-wrapped lines, the grapheme cluster containing the newline will be included at the end of the line. If a segment is too long to fit on any line, put it in its own line.

Jump to

Keyboard shortcuts

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