diff --git a/cmd/skills/list.go b/cmd/skills/list.go index 8e3800b42c1..9d3c7102f4b 100644 --- a/cmd/skills/list.go +++ b/cmd/skills/list.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "path/filepath" + "sort" "strings" "time" @@ -97,6 +98,9 @@ func renderText(rules string, list []libskills.Skill) string { if len(list) == 0 { return withRules(rules) + "No skills found.\n" } + // Every width lays the list out again, so each description is joined into + // one line once, here. + list = oneLineSkills(list) // A description only makes its line longer, and the search hint is the one // line a wider width can leave out. So when the names do not fit even with // the hint, no width does, and the names-only text is the answer. @@ -106,29 +110,61 @@ func renderText(rules string, list []libskills.Skill) string { } maxLen := 0 for _, s := range list { - maxLen = max(maxLen, len(oneLine(s.Description))) - } - // The length is not monotonic in width: a wider width can drop the search - // hint, so a binary search could settle on a narrower width than one that - // fits. Try each width from the widest down and keep the first that fits. - for w := min(maxLen, textBudget); w > 0; w-- { - if out := layoutText(rules, list, w); len(out) <= textBudget { - return out - } + maxLen = max(maxLen, len(s.Description)) + } + // No width above maxLen cuts a description, so the widest width to try is + // the smaller of maxLen and the budget. + top := min(maxLen, textBudget) + fits := func(w int) bool { return len(layoutText(rules, list, w)) <= textBudget } + // The length is not monotonic in width: once no description is left out, the + // search hint goes away, so a wider width can be shorter. Below the first + // width at which no description is left out the hint is always there, and from + // that width on it never is, so the length only grows within each part. The + // widest fitting width is searched in the upper part first, then the lower. + complete := 1 + sort.Search(top, func(i int) bool { return !dropsAny(list, i+1) }) + if w := widestFitIn(complete, top, fits); w > 0 { + return layoutText(rules, list, w) + } + if w := widestFitIn(1, complete-1, fits); w > 0 { + return layoutText(rules, list, w) } return names } +// widestFitIn returns the widest width in [lo, hi] that fits, or 0 when lo does +// not fit. Within [lo, hi] fits must hold up to some width and fail above it. +func widestFitIn(lo, hi int, fits func(int) bool) int { + if hi < lo || !fits(lo) { + return 0 + } + first := sort.Search(hi-lo+1, func(i int) bool { return !fits(lo + i) }) + return lo + first - 1 +} + +// dropsAny reports whether layoutText at width leaves some description out. +// It goes false once no description is left out, and stays false above it. It +// must match the dropped rule in layoutText. +func dropsAny(list []libskills.Skill, width int) bool { + for _, s := range list { + if desc := s.Description; desc != "" && cutWords(desc, width) == "" { + return true + } + } + return false +} + // layoutText lays out the text output with descriptions cut to width. A width // of 0 leaves every description out. A skill is listed by its name alone when // its description is left out or has no word boundary within width, so one -// long description does not drop the others. +// long description does not drop the others. Descriptions must be one line. +// The search hint must stay the only line a wider width can leave out: the +// width search in renderText relies on it, together with dropsAny. func layoutText(rules string, list []libskills.Skill, width int) string { var b strings.Builder b.WriteString(withRules(rules)) dropped := false for _, s := range list { - desc := oneLine(s.Description) + desc := s.Description cut := "" if width > 0 { cut = cutWords(desc, width) @@ -147,6 +183,16 @@ func layoutText(rules string, list []libskills.Skill, width int) string { return b.String() } +// oneLineSkills returns list with each description joined into one line. +func oneLineSkills(list []libskills.Skill) []libskills.Skill { + out := make([]libskills.Skill, len(list)) + for i, s := range list { + s.Description = oneLine(s.Description) + out[i] = s + } + return out +} + // oneLine joins the words of s with single spaces, so a description takes // exactly one line of output. func oneLine(s string) string { diff --git a/cmd/skills/list_test.go b/cmd/skills/list_test.go index 4434a931218..ae0c4d906e1 100644 --- a/cmd/skills/list_test.go +++ b/cmd/skills/list_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "math/rand/v2" "strings" "testing" "time" @@ -152,6 +153,109 @@ func TestRenderTextKeepsWidestFittingWidth(t *testing.T) { assert.NotContains(t, out, "databricks skills search") } +// renderTextScan is a brute-force reference for the width search: it tries +// every width from the widest down and keeps the first that fits. It shares +// oneLineSkills and layoutText with renderText, so it checks the search only. +func renderTextScan(rules string, list []libskills.Skill) string { + if len(list) == 0 { + return withRules(rules) + "No skills found.\n" + } + list = oneLineSkills(list) + names := layoutText(rules, list, 0) + if len(names)-len(searchHint)-1 > textBudget { + return names + } + maxLen := 0 + for _, s := range list { + maxLen = max(maxLen, len(s.Description)) + } + for w := min(maxLen, textBudget); w > 0; w-- { + if out := layoutText(rules, list, w); len(out) <= textBudget { + return out + } + } + return names +} + +// randomCatalog returns a catalog whose sizes, names, descriptions and rules +// vary with the random source, so the width search meets every case. +func randomCatalog(r *rand.Rand) (string, []libskills.Skill) { + words := []string{"a", "deploy", "the", "workspace", "x", "notebook-run", "describes", "it.", "averyveryverylongwordthatkeepsgoing", " "} + var rules string + if r.IntN(2) == 0 { + rules = strings.Repeat("Pick the skill whose description matches. ", 1+r.IntN(3)) + } + // Some catalogs have names long enough that the names alone exceed the budget. + nameLen := 70 + if r.IntN(6) == 0 { + nameLen = 200 + } + var list []libskills.Skill + for i := range r.IntN(60) { + var desc []string + for range r.IntN(40) { + desc = append(desc, words[r.IntN(len(words))]) + } + sep := " " + if r.IntN(10) == 0 { + sep = "\n" + } + text := strings.Join(desc, sep) + if r.IntN(8) == 0 { + // An unbreakable description has no word boundary to cut at. + text = strings.Repeat("x", r.IntN(300)) + } + list = append(list, libskills.Skill{ + Name: fmt.Sprintf("skill-%d-%s", i, strings.Repeat("n", r.IntN(nameLen))), + Description: text, + }) + } + return rules, list +} + +func TestRenderTextMatchesWidthScan(t *testing.T) { + for seed := range uint64(300) { + r := rand.New(rand.NewPCG(seed, 1)) + rules, list := randomCatalog(r) + want := renderTextScan(rules, list) + got := renderText(rules, list) + if !assert.Equal(t, want, got, "seed %d", seed) { + return + } + } +} + +// fourHundredLongSkills returns 400 skills whose descriptions are each far +// longer than the text budget. +func fourHundredLongSkills() []libskills.Skill { + var list []libskills.Skill + for i := range 400 { + desc := strings.Repeat(fmt.Sprintf("word%d describes the skill in detail. ", i), 40) + list = append(list, libskills.Skill{Name: fmt.Sprintf("s%03d", i), Description: desc}) + } + return list +} + +func TestRenderTextFourHundredLongDescriptionsFitsBudget(t *testing.T) { + out := renderText("Pick the skill whose description matches the task.", fourHundredLongSkills()) + assert.LessOrEqual(t, len(out), textBudget) + assert.Contains(t, out, "s399") + // Names alone would also fit, so check that descriptions are kept. + assert.Contains(t, out, "s000: word0") +} + +// BenchmarkRenderTextFourHundredLongDescriptions measures the width search on +// a 400-skill catalog with long descriptions. The time per op is the figure to +// watch; the full width scan takes seconds on the same catalog. +func BenchmarkRenderTextFourHundredLongDescriptions(b *testing.B) { + list := fourHundredLongSkills() + const rules = "Pick the skill whose description matches the task." + b.ReportAllocs() + for b.Loop() { + renderText(rules, list) + } +} + func TestRenderTextNoSearchHintWithoutDescriptions(t *testing.T) { list := []libskills.Skill{{Name: "alpha"}, {Name: "beta"}} assert.Equal(t, "alpha\nbeta\n"+readHint+"\n", renderText("", list))