| 1 | package skill |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | "unicode/utf8" |
| 7 | ) |
| 8 | |
| 9 | func boundedCatalog(skills []Skill) string { |
| 10 | const budget = IndexMaxChars - len("```\n\n```") |
| 11 | render := func(limit int) string { |
| 12 | lines := make([]string, len(skills)) |
| 13 | for i, sk := range skills { |
| 14 | lines[i] = indexLineWithLimit(sk, limit) |
| 15 | } |
| 16 | return strings.Join(lines, "\n") |
| 17 | } |
| 18 | joined := render(130) |
| 19 | if utf8.RuneCountInString(joined) > budget { |
| 20 | if utf8.RuneCountInString(render(0)) <= budget { |
| 21 | low, high := 0, 130 |
| 22 | for low < high { |
| 23 | mid := (low + high + 1) / 2 |
| 24 | if utf8.RuneCountInString(render(mid)) <= budget { |
| 25 | low = mid |
| 26 | } else { |
| 27 | high = mid - 1 |
| 28 | } |
| 29 | } |
| 30 | joined = render(low) |
| 31 | } else { |
| 32 | joined = catalogPreview(skills, budget) |
| 33 | } |
| 34 | } |
| 35 | return "```\n" + joined + "\n```" |
| 36 | } |
| 37 | |
| 38 | func catalogPreview(skills []Skill, budget int) string { |
| 39 | var lines []string |
| 40 | used := 0 |
| 41 | for i, sk := range skills { |
| 42 | line := indexLineWithLimit(sk, 24) |
| 43 | size := utf8.RuneCountInString(line) + 1 |
| 44 | if used+size+utf8.RuneCountInString(catalogOverflow(len(skills)-i-1)) > budget { |
| 45 | break |
| 46 | } |
| 47 | lines = append(lines, line) |
| 48 | used += size |
| 49 | } |
| 50 | lines = append(lines, catalogOverflow(len(skills)-len(lines))) |
| 51 | return strings.Join(lines, "\n") |
| 52 | } |
| 53 | |
| 54 | func catalogOverflow(count int) string { |
| 55 | return fmt.Sprintf("… (%d more skills; discover with use_capability action=search and a task-specific query, or action=list.)", count) |
| 56 | } |
| 57 |