Skip to content

Commit

Permalink
Merge pull request #2525 from carapace-sh/man-sections
Browse files Browse the repository at this point in the history
man: added ActionSections
  • Loading branch information
rsteube authored Sep 14, 2024
2 parents 420b937 + 9def307 commit dac1918
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions pkg/actions/tools/man/section.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package man

import (
"regexp"
"strings"

"github.com/carapace-sh/carapace"
)

// ActionSections completes sections of given manpage (or all if empty).
//
// 1 (General commands)
// 2 (System calls)
func ActionSections(manpage string) carapace.Action {
return carapace.ActionCallback(func(c carapace.Context) carapace.Action {
a := carapace.ActionValuesDescribed(
"1", "General commands",
"2", "System calls",
"3", "Library functions",
"4", "Special files",
"5", "File formats and conventions",
"6", "Games and screensavers",
"7", "Miscellaneous",
"8", "System administration commands and daemons",
)

if manpage != "" {
output, err := c.Command("man", "-f", manpage).Output()
if err != nil {
return carapace.ActionMessage(err.Error())
}

r := regexp.MustCompile(`^` + regexp.QuoteMeta(manpage) + ` \((?P<section>[^]]+)\).*$`)
sections := make([]string, 0)
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if matches := r.FindStringSubmatch(line); matches != nil {
sections = append(sections, matches[1][:1]) // only use first character
}
}
a = a.Retain(sections...)
}
return a
})
}

0 comments on commit dac1918

Please sign in to comment.