rg

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Reset     = "\033[0m"
	Bold      = "\033[1m"
	Underline = "\033[4m"

	FgBlack   = "\033[30m"
	FgRed     = "\033[31m"
	FgGreen   = "\033[32m"
	FgYellow  = "\033[33m"
	FgBlue    = "\033[34m"
	FgMagenta = "\033[35m"
	FgCyan    = "\033[36m"
	FgWhite   = "\033[37m"

	FgBrightBlack   = "\033[90m"
	FgBrightRed     = "\033[91m"
	FgBrightGreen   = "\033[92m"
	FgBrightYellow  = "\033[93m"
	FgBrightBlue    = "\033[94m"
	FgBrightMagenta = "\033[95m"
	FgBrightCyan    = "\033[96m"
	FgBrightWhite   = "\033[97m"

	BgBlack   = "\033[40m"
	BgRed     = "\033[41m"
	BgGreen   = "\033[42m"
	BgYellow  = "\033[43m"
	BgBlue    = "\033[44m"
	BgMagenta = "\033[45m"
	BgCyan    = "\033[46m"
	BgWhite   = "\033[47m"
)

ANSI color codes for terminal output.

View Source
const (
	// NoMatch means no pattern matched
	NoMatch = pkgrg.NoMatch
	// Ignore means the path should be ignored
	Ignore = pkgrg.Ignore
	// Include means the path was re-included via negation
	Include = pkgrg.Include
)

Variables

This section is empty.

Functions

func ApplyColorSpec

func ApplyColorSpec(scheme *ColorScheme, spec string) error

ApplyColorSpec applies a color specification to a scheme

func ColorNameToCode

func ColorNameToCode(name string, isBg bool) string

ColorNameToCode converts a color name to ANSI code

func Colorize

func Colorize(text, color string) string

Colorize wraps text with color codes

func FormatByteOffset

func FormatByteOffset(offset int64, scheme ColorScheme, useColor bool) string

FormatByteOffset formats a byte offset with colors (uses same color as line number)

func FormatColumn

func FormatColumn(col int, scheme ColorScheme, useColor bool) string

FormatColumn formats a column number with colors

func FormatLineNumber

func FormatLineNumber(lineNum int, scheme ColorScheme, useColor bool) string

FormatLineNumber formats a line number with colors

func FormatPath

func FormatPath(path string, scheme ColorScheme, useColor bool) string

FormatPath formats a file path with colors

func FormatSeparator

func FormatSeparator(sep string, scheme ColorScheme, useColor bool) string

FormatSeparator formats a separator with colors

func HighlightLiteralMatches

func HighlightLiteralMatches(line, pattern string, caseInsensitive bool, scheme ColorScheme, useColor bool) string

HighlightLiteralMatches highlights all literal string matches in the line

func HighlightMatches

func HighlightMatches(line string, re *regexp.Regexp, scheme ColorScheme, useColor bool) string

HighlightMatches highlights all regex matches in the line

func ParseColorSpec

func ParseColorSpec(spec string) (component, attr, value string, err error)

ParseColorSpec parses a ripgrep-style color specification like "path:fg:magenta"

func Run

func Run(w io.Writer, pattern string, paths []string, opts Options) error

Run executes the rg command

func ShouldUseColor

func ShouldUseColor(mode ColorMode) bool

ShouldUseColor determines if colors should be used based on mode and terminal

Types

type ByteOffset

type ByteOffset struct {
	// contains filtered or unexported fields
}

ByteOffset calculates the byte offset of a match in a file

func NewByteOffset

func NewByteOffset() *ByteOffset

NewByteOffset creates a new byte offset tracker

func (*ByteOffset) AddLine

func (b *ByteOffset) AddLine(line string)

AddLine adds a line's length to the running offset

func (*ByteOffset) GetMatchOffset

func (b *ByteOffset) GetMatchOffset(column int) int64

GetMatchOffset returns the absolute byte offset of a match

type ColorMode

type ColorMode int

ColorMode represents when to use colors

const (
	ColorAuto   ColorMode = iota // Use colors if terminal supports it
	ColorAlways                  // Always use colors
	ColorNever                   // Never use colors
)

func ParseColorMode

func ParseColorMode(s string) ColorMode

ParseColorMode parses a color mode string

type ColorScheme

type ColorScheme struct {
	Path      string // Color for file paths
	Line      string // Color for line numbers
	Column    string // Color for column numbers
	Match     string // Color for matched text
	MatchBg   string // Background color for matched text
	Separator string // Color for separators
	Context   string // Color for context lines
}

ColorScheme defines the colors used for output

func DefaultScheme

func DefaultScheme() ColorScheme

DefaultScheme returns the default color scheme matching ripgrep

func NoColorScheme

func NoColorScheme() ColorScheme

NoColorScheme returns a scheme with no colors (for --color=never)

type FileResult

type FileResult struct {
	Path    string  `json:"path"`
	Matches []Match `json:"matches"`
	Count   int     `json:"count"`
}

FileResult represents matches in a single file

type Formatter

type Formatter struct {
	// contains filtered or unexported fields
}

Formatter handles output formatting with color support

func NewFormatter

func NewFormatter(w io.Writer, opts FormatterOptions) *Formatter

NewFormatter creates a new output formatter

func (*Formatter) PrintContextSeparator

func (f *Formatter) PrintContextSeparator()

PrintContextSeparator prints the context separator ("--")

func (*Formatter) PrintCount

func (f *Formatter) PrintCount(path string, count int)

PrintCount prints the count for a file (for -c mode)

func (*Formatter) PrintFileHeader

func (f *Formatter) PrintFileHeader(path string)

PrintFileHeader prints a file header (only used in heading mode)

func (*Formatter) PrintFilesWithMatch

func (f *Formatter) PrintFilesWithMatch(path string)

PrintFilesWithMatch prints just the filename (for -l mode)

func (*Formatter) PrintMatch

func (f *Formatter) PrintMatch(path string, lineNum, column int, byteOffset int64, line string, isContext bool)

PrintMatch prints a single match line byteOffset is the byte offset of the line start in the file (used with -b flag)

type FormatterOptions

type FormatterOptions struct {
	UseColor        bool
	Scheme          ColorScheme
	Format          OutputFormat
	ShowLineNumber  bool
	ShowColumn      bool
	ShowByteOffset  bool
	OnlyMatching    bool
	Trim            bool
	Replace         string
	Regex           *regexp.Regexp
	Pattern         string
	CaseInsensitive bool
	UseLiteral      bool
}

FormatterOptions configures the formatter

type Gitignore

type Gitignore = pkgrg.Gitignore

Gitignore is an alias for the pkg type

type GitignoreSet

type GitignoreSet = pkgrg.GitignoreSet

GitignoreSet is an alias for the pkg type

func NewGitignoreSet

func NewGitignoreSet(searchDir string) *GitignoreSet

NewGitignoreSet creates a new GitignoreSet loading patterns from multiple sources

type Match

type Match struct {
	Path       string `json:"path"`
	LineNumber int    `json:"line_number"`
	Column     int    `json:"column,omitempty"`
	ByteOffset int64  `json:"byte_offset,omitempty"`
	Line       string `json:"line"`
	Match      string `json:"match,omitempty"`
}

Match represents a single match result

type MatchResult

type MatchResult = pkgrg.MatchResult

MatchResult indicates the result of matching a path against gitignore patterns

type Options

type Options struct {
	IgnoreCase     bool     // -i: case insensitive search
	SmartCase      bool     // -S: smart case (case insensitive if pattern is lowercase)
	WordRegexp     bool     // -w: match whole words only
	LineNumber     bool     // -n: show line numbers (default true)
	Count          bool     // -c: only show count of matches
	FilesWithMatch bool     // -l: only show file names with matches
	InvertMatch    bool     // -v: show non-matching lines
	Context        int      // -C: lines of context (before and after)
	Before         int      // -B: lines before match
	After          int      // -A: lines after match
	Types          []string // -t: file types to include
	TypesNot       []string // -T: file types to exclude
	Glob           []string // -g: glob patterns to include
	Hidden         bool     // --hidden: search hidden files
	NoIgnore       bool     // --no-ignore: don't respect gitignore
	MaxCount       int      // -m: max matches per file
	MaxDepth       int      // --max-depth: max directory depth
	FollowSymlinks bool     // -L: follow symlinks
	JSON           bool     // --json: JSON output
	JSONStream     bool     // --json-stream: streaming NDJSON output
	NoHeading      bool     // --no-heading: no file name headings
	OnlyMatching   bool     // -o: only show matching part
	Quiet          bool     // -q: quiet mode, exit on first match
	Fixed          bool     // -F: treat pattern as literal string
	Threads        int      // --threads: number of worker threads (0 = auto)

	// New options for ripgrep compatibility
	Color      string   // --color: when to use colors (auto, always, never)
	Colors     []string // --colors: custom color specifications
	Replace    string   // -r/--replace: replacement string for matches
	Multiline  bool     // -U/--multiline: enable multiline matching
	Trim       bool     // --trim: trim leading/trailing whitespace
	ShowColumn bool     // --column: show column numbers
	ByteOffset bool     // -b/--byte-offset: show byte offset (not implemented)
	Stats      bool     // --stats: show search statistics
	Passthru   bool     // --passthru: show all lines, highlighting matches
}

Options configures the rg command behavior

type OutputFormat

type OutputFormat int

OutputFormat represents the output format type

const (
	FormatDefault    OutputFormat = iota // Default ripgrep-style output
	FormatNoHeading                      // No file name headings (all on one line)
	FormatJSON                           // Full JSON output
	FormatJSONStream                     // Streaming NDJSON output
)

type Pattern

type Pattern = pkgrg.Pattern

Pattern is an alias for the pkg type

type Result

type Result struct {
	Files      []FileResult `json:"files"`
	TotalFiles int          `json:"total_files"`
	TotalMatch int          `json:"total_matches"`
}

Result represents the complete search result

type Stats

type Stats struct {
	FilesSearched int
	FilesMatched  int
	TotalMatches  int
	BytesSearched int64
}

Stats tracks search statistics

func (*Stats) PrintStats

func (s *Stats) PrintStats(w io.Writer)

PrintStats prints search statistics

type StreamBegin

type StreamBegin struct {
	Path string `json:"path"`
}

StreamBegin is sent at the start of searching a file

type StreamContext

type StreamContext struct {
	Path       string      `json:"path"`
	LineNumber int         `json:"line_number"`
	Lines      StreamLines `json:"lines"`
}

StreamContext is sent for context lines

type StreamEnd

type StreamEnd struct {
	Path       string `json:"path"`
	MatchCount int    `json:"match_count"`
}

StreamEnd is sent at the end of searching a file

type StreamLines

type StreamLines struct {
	Text string `json:"text"`
}

StreamLines holds line text for streaming output

type StreamMatch

type StreamMatch struct {
	Path       string      `json:"path"`
	LineNumber int         `json:"line_number"`
	Column     int         `json:"column,omitempty"`
	Lines      StreamLines `json:"lines"`
	Match      string      `json:"match,omitempty"`
}

StreamMatch is sent for each match

type StreamMessage

type StreamMessage struct {
	Type string `json:"type"` // "begin", "match", "context", "end", "summary"
	Data any    `json:"data"`
}

StreamMessage represents a message in NDJSON streaming output

type StreamSummary

type StreamSummary struct {
	TotalFiles   int `json:"total_files"`
	TotalMatches int `json:"total_matches"`
}

StreamSummary is sent at the end of all searching

Jump to

Keyboard shortcuts

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