Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Switched to POSIX regular expressions for --search
Browse files Browse the repository at this point in the history
  • Loading branch information
eprovst committed Dec 22, 2018
1 parent 4557a87 commit b3862b9
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 203 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ sudo chmod 644 /etc/bash_completion.d/tldr
tldr -u
```
The database is then stored in the cache directory of your platform.
- If you want all the commands matching a pattern, let's say `git-*`, use:
- If you want all the commands matching a grep style regex, let's say `g[ie]t$`, use:
```
tldr -s "git-*"
tldr -s 'g[ie]t$'
```
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func main() {
cmd.Flags().BoolVar(&purge, "purge", false, "remove database from disk")
cmd.Flags().StringVar(&render, "render", "", "render local `page`")
cmd.Flags().BoolVarP(&update, "update", "u", false, "redownload pages")
cmd.Flags().StringVarP(&search, "search", "s", "", "list pages matching `pattern`")
cmd.Flags().StringVarP(&search, "search", "s", "", "list pages matching `regex`")

// Here for compatibility sake
cmd.Flags().BoolVarP(&purge, "clear-cache", "c", false, "purge database")
Expand Down
191 changes: 0 additions & 191 deletions pages/globmatch.go

This file was deleted.

2 changes: 1 addition & 1 deletion pages/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ _tldr_completion()
if [[ "${COMP_WORDS[$COMP_CWORD]}" == "-"* ]]; then
COMPREPLY=($(compgen -W "--help --platform --purge --render --search --update --version" -- ${COMP_WORDS[$COMP_CWORD]}))
else
COMPREPLY=($(tldr --search "${COMP_WORDS[$COMP_CWORD]}*" 2> /dev/null))
COMPREPLY=($(tldr --search "^${COMP_WORDS[$COMP_CWORD]}" 2> /dev/null))
fi
fi
}
Expand Down
22 changes: 14 additions & 8 deletions pages/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ package pages

import (
"fmt"
"os"
"regexp"

"github.com/elecprog/tldr/targets"
"go.etcd.io/bbolt"
)

// Search shows all pages that contain the pattern
func Search(database *bbolt.DB, pattern string) {
// Search shows all pages that matches the regex
func Search(database *bbolt.DB, regex string) {
// Iterate through keys, printing all that match the pattern,
// as keys are byte ordered, they are also in alphabethic order.
err := database.View(
Expand All @@ -36,8 +38,13 @@ func Search(database *bbolt.DB, pattern string) {
return nil
}

// Create a matcher from the pattern
matcher := NewGlobMatcher(pattern)
// Create a matcher from the regex
matcher, err := regexp.CompilePOSIX(regex)

if err != nil {
fmt.Println(err)
os.Exit(1)
}

// Search in both the local and the common bucket
searchInBucket(root.Bucket([]byte(targets.OsDir)), matcher)
Expand All @@ -50,13 +57,12 @@ func Search(database *bbolt.DB, pattern string) {
}
}

func searchInBucket(bucket *bbolt.Bucket, matcher *GlobMatcher) error {
func searchInBucket(bucket *bbolt.Bucket, matcher *regexp.Regexp) error {
// Print all the pages that match the pattern
return bucket.ForEach(
func(page, _ []byte) error {
pageS := string(page)
if matcher.Match(pageS) {
fmt.Println(pageS)
if matcher.Match(page) {
fmt.Println(string(page))
}

return nil
Expand Down

0 comments on commit b3862b9

Please sign in to comment.