Skip to main content

Crate ripgrep_api

Crate ripgrep_api 

Source
Expand description

§ripgrep-api

Coder-friendly Rust API wrapper around ripgrep’s core crates.

§Install

Crates.io:

ripgrep-api = "0.1"

Git dependency (current repo):

ripgrep-api = { git = "https://github.com/AlextheYounga/ripgrep-api.git" }

§Quickstart

use ripgrep_api::SearchBuilder;

let matches: Vec<_> = SearchBuilder::new("todo")
    .path(".")
    .glob("**/*.rs")
    .smart_case()
    .context(2)
    .build()?
    .collect();

for mat in matches {
    println!("{}:{}:{}", mat.path.display(), mat.line.unwrap_or(0), mat.line_text);
}

§Streaming callbacks

use ripgrep_api::{Match, SearchBuilder};

let root = ".";
SearchBuilder::new("alpha")
    .path(root)
    .for_each(|mat: &Match| {
        println!("{}:{}", mat.path.display(), mat.line.unwrap_or(0));
        true
    })?;
use ripgrep_api::SearchBuilder;

let haystack = b"zero\nmatch\nthree\n";
let matches = SearchBuilder::new("match")
    .search_slice(haystack)?;

assert_eq!(matches.len(), 1);

§PCRE2 (feature flag)

use ripgrep_api::SearchBuilder;

let matches = SearchBuilder::new(r"(foo)(bar)\1")
    .pcre2()
    .search_slice(b"foobarfoo")?;

assert_eq!(matches.len(), 1);

Enable the feature in Cargo:

ripgrep-api = { version = "0.1", features = ["pcre2"] }

§Performance knobs

use ripgrep_api::SearchBuilder;
use grep_searcher::MmapChoice;

let matches = SearchBuilder::new("alpha")
    .path(".")
    .threads(4)
    .memory_map(unsafe { MmapChoice::auto() })
    .heap_limit(64 * 1024)
    .build()?
    .collect::<Vec<_>>();

§Custom file types and overrides

use ripgrep_api::SearchBuilder;

let matches = SearchBuilder::new("alpha")
    .path(".")
    .type_add("notes", "*.note")
    .type_("notes")
    .glob("!ignored.note")
    .build()?
    .collect::<Vec<_>>();

§Walk-only file listing

use ripgrep_api::SearchBuilder;

let files = SearchBuilder::new("irrelevant")
    .path(".")
    .glob("**/*.rs")
    .walk_files()?;

assert!(!files.is_empty());

§rg flag -> API method

rg flagAPI method
-g/--globglob(...)
-t/--typetype_(...)
-T/--type-nottype_not(...)
-d/--max-depthmax_depth(...)
--max-filesizemax_filesize(...)
-i/--ignore-caseignore_case()
-S/--smart-casesmart_case()
-w/--word-regexpword()
-x/--line-regexpline_regexp()
-A/--after-contextafter_context(...)
-B/--before-contextbefore_context(...)
-C/--contextcontext(...)
-m/--max-countmax_count(...)
-uuuhidden() + ignore(false)
-P/--pcre2pcre2() (feature: pcre2)
-j/--threadsthreads(...)
--mmapmemory_map(...)
--no-mmapmemory_map(MmapChoice::never())
--heap-limitheap_limit(...)

Structs§

ContextLine
Match
A structured match result.
Search
SearchBuilder
Fluent builder for rg-style search configuration.
SubMatch

Enums§

ContextKind
SearchError

Traits§

MatchSink

Functions§

rg
Create a new SearchBuilder with rg-style defaults.