MCPcopy
hub / github.com/mum4k/termdash / Example

Function Example

private/attrrange/attrrange_test.go:25–54  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

23)
24
25func Example() {
26 // Caller has a slice of some attributes, like a cell color that applies
27 // to a portion of text.
28 attrs := []cell.Color{cell.ColorRed, cell.ColorBlue}
29 redIdx := 0
30 blueIdx := 1
31
32 // This is the text the colors apply to.
33 const text = "HelloWorld"
34
35 // Assuming that we want the word "Hello" in red and the word "World" in
36 // green, we can set our ranges as follows:
37 tr := NewTracker()
38 if err := tr.Add(0, len("Hello"), redIdx); err != nil {
39 panic(err)
40 }
41 if err := tr.Add(len("Hello")+1, len(text), blueIdx); err != nil {
42 panic(err)
43 }
44
45 // Now to get the index into attrs (i.e. the color) for a particular
46 // character, we can do:
47 for i, c := range text {
48 ar, err := tr.ForPosition(i)
49 if err != nil {
50 panic(err)
51 }
52 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)
53 }
54}
55
56func TestForPosition(t *testing.T) {
57 tests := []struct {

Callers

nothing calls this directly

Calls 3

AddMethod · 0.95
ForPositionMethod · 0.95
NewTrackerFunction · 0.85

Tested by

no test coverage detected