feat(help): show topic previews
All checks were successful
Security Scan / security (push) Successful in 17s
All checks were successful
Security Scan / security (push) Successful in 17s
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
419e7f745b
commit
181d9546b4
2 changed files with 42 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package help
|
package help
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"forge.lthn.ai/core/cli/pkg/cli"
|
"forge.lthn.ai/core/cli/pkg/cli"
|
||||||
|
|
@ -63,10 +64,34 @@ func renderTopicList(topics []*gohelp.Topic) error {
|
||||||
cli.Section("Available Help Topics")
|
cli.Section("Available Help Topics")
|
||||||
for _, topic := range topics {
|
for _, topic := range topics {
|
||||||
cli.Println(" %s - %s", topic.ID, topic.Title)
|
cli.Println(" %s - %s", topic.ID, topic.Title)
|
||||||
|
if summary := topicSummary(topic); summary != "" {
|
||||||
|
cli.Println("%s", cli.DimStr(" "+summary))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func topicSummary(topic *gohelp.Topic) string {
|
||||||
|
if topic == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
content := strings.TrimSpace(topic.Content)
|
||||||
|
if content == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(strings.NewReader(content))
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return line
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func renderTopic(t *gohelp.Topic) {
|
func renderTopic(t *gohelp.Topic) {
|
||||||
cli.Blank()
|
cli.Blank()
|
||||||
cli.Println("%s", cli.TitleStyle.Render(t.Title))
|
cli.Println("%s", cli.TitleStyle.Render(t.Title))
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,23 @@ func TestRenderSearchResults_Good(t *testing.T) {
|
||||||
assert.Contains(t, out, "Core is configured via environment variables.")
|
assert.Contains(t, out, "Core is configured via environment variables.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRenderTopicList_Good(t *testing.T) {
|
||||||
|
out := captureOutput(t, func() {
|
||||||
|
err := renderTopicList([]*gohelp.Topic{
|
||||||
|
{
|
||||||
|
ID: "config",
|
||||||
|
Title: "Configuration",
|
||||||
|
Content: "# Configuration\n\nCore is configured via environment variables.\n\nMore details follow.",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
assert.Contains(t, out, "AVAILABLE HELP TOPICS")
|
||||||
|
assert.Contains(t, out, "config - Configuration")
|
||||||
|
assert.Contains(t, out, "Core is configured via environment variables.")
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddHelpCommands_Bad(t *testing.T) {
|
func TestAddHelpCommands_Bad(t *testing.T) {
|
||||||
t.Run("missing search results", func(t *testing.T) {
|
t.Run("missing search results", func(t *testing.T) {
|
||||||
cmd := newHelpCommand(t)
|
cmd := newHelpCommand(t)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue