attrrange

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

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)
}
Output:

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.

func (*Tracker) Add

func (t *Tracker) Add(low, high, attrIdx int) error

Add adds a new range of items that share attributes with the specified index. The low position of the range must not overlap with low position of any existing range.

func (*Tracker) ForPosition

func (t *Tracker) ForPosition(pos int) (*AttrRange, error)

ForPosition returns attribute index that apply to the specified position. Returns ErrNotFound when the requested position wasn't found in any of the known ranges.

Jump to

Keyboard shortcuts

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