Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering the help which is displayed. #54

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 46 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"fmt"
"math/rand"
"os"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -47,7 +48,7 @@ func main() {
}

// If we have a file, then read the content
if len(flag.Args()) > 0 {
if len(flag.Args()) > 0 && !*hlp {
content, err := os.ReadFile(flag.Args()[0])
if err != nil {
fmt.Printf("Error reading %s:%s\n", os.Args[1], err)
Expand Down Expand Up @@ -80,6 +81,9 @@ func main() {
// Show the help?
if *hlp {

// Are we just showing a specific thing?
show := flag.Args()

// Read the standard library
pre := stdlib.Contents()

Expand Down Expand Up @@ -114,28 +118,53 @@ func main() {
// Is it a procedure?
prc, ok := val.(*primitive.Procedure)

// Does it have help too?
if ok && len(prc.Help) > 0 {
// If it isn't a procedure skip it
if !ok {
continue
}

txt := prc.Help
// No help? skip it
if len(prc.Help) == 0 {
continue
}

// Show the arguments for functions,
// if these are not builtin.
args := ""
// Get the text
txt := prc.Help

if len(prc.Args) > 0 {
// Build up the arguments
args := ""

for _, arg := range prc.Args {
args += " " + arg.ToString()
}
args = strings.TrimSpace(args)
args = " (" + args + ")"
if len(prc.Args) > 0 {

for _, arg := range prc.Args {
args += " " + arg.ToString()
}
args = strings.TrimSpace(args)
args = " (" + args + ")"
}

entry := key + args + "\n"
entry += strings.Repeat("=", len(key+args)) + "\n"
entry += txt + "\n\n\n"

// Are we going to show this?
match := false
for _, x := range show {

r, er := regexp.Compile(x)
if er != nil {
fmt.Printf("Error compiling regexp %s:%s", show, er)
return
}

res := r.FindStringSubmatch(entry)
if len(res) > 0 {
match = true
}
}

// Name
fmt.Printf("%s%s\n", key, args)
fmt.Printf("%s\n", strings.Repeat("=", len(key+args)))
fmt.Printf("%s\n\n\n\n", txt)
if (len(show) == 0) || match {
fmt.Printf("%s", entry)
}

}
Expand Down