Documentation
¶
Overview ¶
Package attrrange simplifies tracking of attributes that apply to a range of items. Refer to the examples in the test file for details on usage.
Example ¶
// Caller has a slice of some attributes, like a cell color that applies
// to a portion of text.
attrs := []cell.Color{cell.ColorRed, cell.ColorBlue}
redIdx := 0
blueIdx := 1
// This is the text the colors apply to.
const text = "HelloWorld"
// Assuming that we want the word "Hello" in red and the word "World" in
// green, we can set our ranges as follows:
tr := NewTracker()
if err := tr.Add(0, len("Hello"), redIdx); err != nil {
panic(err)
}
if err := tr.Add(len("Hello")+1, len(text), blueIdx); err != nil {
panic(err)
}
// Now to get the index into attrs (i.e. the color) for a particular
// character, we can do:
for i, c := range text {
ar, err := tr.ForPosition(i)
if err != nil {
panic(err)
}
log.Printf("character at text[%d] = %q, color index %d = %v, range low:%d, high:%d", i, c, ar.AttrIdx, attrs[ar.AttrIdx], ar.Low, ar.High)
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AttrRange ¶
type AttrRange struct {
// Low is the first position where these attributes apply.
Low int
// High is the end of the range. The attributes apply to all items in range
// Low <= b < high.
High int
// AttrIdx is the index of the attributes that apply to this range.
AttrIdx int
}
AttrRange is a range of items that share the same attributes.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker tracks attributes that apply to a range of items. This object is not thread safe.
func NewTracker ¶
func NewTracker() *Tracker
NewTracker returns a new tracker of ranges that share the same attributes.
Click to show internal directories.
Click to hide internal directories.