<commit_before>package attrrange

import (
	"testing"

	"github.com/kylelemons/godebug/pretty"
)

func TestForPosition(t *testing.T) {
	tests := []struct {
		desc string
		// if not nil, called before calling ForPosition.
		// Can add ranges.
		update        func(*Tracker) error
		pos           int
		want          *AttrRange
		wantErr       error
		wantUpdateErr bool
	}{
		{
			desc:    "fails when no ranges given",
			pos:     0,
			wantErr: ErrNotFound,
		},
		{
			desc: "fails to add a duplicate",
			update: func(tr *Tracker) error {
				if err := tr.Add(2, 5, 40); err != nil {<commit_msg>Adding an usage example for the attrrange package.<commit_after>package attrrange

import (
	"log"
	"testing"

	"github.com/kylelemons/godebug/pretty"
	"github.com/mum4k/termdash/cell"
)

func 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)
	}
}

func TestForPosition(t *testing.T) {
	tests := []struct {
		desc string
		// if not nil, called before calling ForPosition.
		// Can add ranges.
		update        func(*Tracker) error
		pos           int
		want          *AttrRange
		wantErr       error
		wantUpdateErr bool
	}{
		{
			desc:    "fails when no ranges given",
			pos:     0,
			wantErr: ErrNotFound,
		},
		{
			desc: "fails to add a duplicate",
			update: func(tr *Tracker) error {
				if err := tr.Add(2, 5, 40); err != nil {
{'fraction_non_alphanumeric': 0.1317722681359045, 'fraction_numerical': 0.006887052341597796, 'mean_word_length': 2.731164383561644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3682521', 'n_tokens_mistral': 832, 'n_tokens_neox': 763, 'n_words': 253}