grep

package module
v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2022 License: Apache-2.0 Imports: 11 Imported by: 3

README ¶

🌳 Go Async Grep with Regex

Follow the KEG Site for docs.

Documentation ¶

Index ¶

Examples ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var Cmd = &Z.Cmd{
	Name:      `grep`,
	Aliases:   []string{`bongrep`},
	Copyright: `Copyright 2021 Robert S Muhlestein`,
	Version:   `v0.2.5`,
	License:   `Apache-2.0`,
	Summary:   `async grep with regex`,
	Usage:     `(help|PATTERN [TARGET ...])`,
	UseVars:   true,
	UseConf:   true,
	Commands:  []*Z.Cmd{help.Cmd},

	Description: `
				The {{aka}} command is a simple utility similar to grep that can
				be composed into bonzai stateful command trees. It uses Go
				regular expressions to return all occurrences within a given
				file. Each TARGET is either a file or directory which will be
				recursively searched. If no TARGET is provided assumes the
				current directory.

			`,

	Call: func(x *Z.Cmd, args ...string) error {

		var padding = 20
		if len(args) == 0 {
			return help.Cmd.Call(x, args...)
		}

		pad, err := x.Get(`padding`)
		if err != nil {
			return err
		}
		if pad != "" {
			padding, err = strconv.Atoi(pad)
			if err != nil {
				return err
			}
		}

		if len(args) == 1 {
			args = append(args, ".")
		}
		results, err := This(args[0], padding, args[1:]...)
		if err != nil {
			return err
		}
		results.ShowFile = true
		if term.IsInteractive() {
			fmt.Print(results.Pretty())
			return nil
		}
		fmt.Print(results)
		return nil
	},
}
View Source
var DefFileColor = term.Black
View Source
var DefMatchColor = term.Yellow

Functions ¶

This section is empty.

Types ¶

type Result ¶

type Result struct {
	File       string // file or path to the file (rel, abs, whatever)
	Text       string // sample of text containing the hit
	Beg        int    // byte of beginning within overall file
	End        int    // byte of ending within overall file
	TextBeg    int    // beginning within Text
	TextEnd    int    // ending within Text
	FileColor  string // color to use for Pretty File
	MatchColor string // color to use for Pretty Match
	ShowFile   bool   // display file in Pretty or not
}

func (Result) Plain ¶

func (r Result) Plain() string

func (Result) Pretty ¶

func (r Result) Pretty() string

Pretty returns a colorized pretty version of the Result with padding around the match. Note that no other characters (including line returns and invisible characters) are escaped. That is left to the caller.

func (Result) String ¶

func (r Result) String() string

type Results ¶

type Results struct {
	ShowFile   bool     // include file in marshaled output
	FileColor  string   // color for file name (see DefFileColor)
	MatchColor string   // color for the match in text (see DefMatchColor
	Hits       []Result // actual results
}

func This ¶

func This(pattern string, pad int, targets ...string) (*Results, error)

This (as in grep.This) searches the targets for all instances of the regular expression pattern which is cached. Subsequent searches for the same pattern will use the cached compiled regular expression for that pattern. Add pad number of bytes to beginning and ending to give context. Does not return an error if no results are found, only if something related to reading the targets involved.

Example (Hit_First) ¶
package main

import (
	"fmt"

	"github.com/rwxrob/grep"
)

func main() {
	results, err := grep.This(`advent`, 90, `testdata/advent.md`)
	if err != nil {
		fmt.Println(err)
	}
	hit := results.Hits[0]
	fmt.Printf("%v\n", hit.Text[hit.Beg:hit.End])
	fmt.Printf("%v\n", hit.Text[hit.TextBeg:hit.TextEnd])
	fmt.Println(hit.Beg == hit.TextBeg)
	fmt.Println(hit.End == hit.TextEnd)

}
Output:

advent
advent
true
true
Example (Hit_Last) ¶
package main

import (
	"fmt"

	"github.com/rwxrob/grep"
)

func main() {
	results, err := grep.This(`holidays`, 90, `testdata/advent.md`)
	if err != nil {
		fmt.Println(err)
	}
	hit := results.Hits[len(results.Hits)-1]
	fmt.Printf("%v\n", hit.Text[hit.TextBeg:hit.TextEnd])
	fmt.Printf("%v:%v\n", hit.Beg, hit.End)
	fmt.Printf("%v:%v\n", hit.TextBeg, hit.TextEnd)
	fmt.Printf("%q\n", hit.Text)

}
Output:

holidays
1585:1593
90:98
"ires whiteboard interviews, there is something seriously wrong with your view of what the holidays are actually about. It ain't that.\n\n"

func (Results) Pretty ¶

func (r Results) Pretty() string

func (Results) String ¶

func (r Results) String() string

String fulfills fmt.Stringer interface in different ways depending on if the term.IsInteractive of not. Use the specific MD and Pretty methods instead if needed.

Directories ¶

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL