Finishing up library version of ripgrep.

This commit is contained in:
Dessalines 2018-10-22 15:31:34 -07:00
parent 2efb9194e2
commit 83806ba8e6
3 changed files with 17 additions and 10 deletions

View File

@ -2,4 +2,4 @@ cd ../server/ui
yarn yarn
yarn build yarn build
cd ../service cd ../service
cargo run cargo run --release

View File

@ -9,7 +9,7 @@ use actix_web::{fs, http, server, App, HttpResponse, Query};
use std::error::Error; use std::error::Error;
use grep::regex::RegexMatcher; use grep::regex::RegexMatcherBuilder;
use grep::searcher::sinks::Lossy; use grep::searcher::sinks::Lossy;
use grep::searcher::{BinaryDetection, SearcherBuilder}; use grep::searcher::{BinaryDetection, SearcherBuilder};
use std::fs::File; use std::fs::File;
@ -47,23 +47,30 @@ fn search(query: Query<SearchQuery>) -> HttpResponse {
fn ripgrep(query: Query<SearchQuery>) -> String { fn ripgrep(query: Query<SearchQuery>) -> String {
let page = query.page.unwrap_or(1); let page = query.page.unwrap_or(1);
let size = query.size.unwrap_or(10); let size = query.size.unwrap_or(10);
let offset = size * (page - 1) + 1; let offset = size * (page - 1);
let csv_file = File::open("../../torrents.csv"); let csv_file = File::open("../../torrents.csv");
println!( println!("query = {} , page = {}, size = {}", query.q, page, size);
"query = {} , page = {}, size = {}, offset = {}",
query.q, page, size, offset
);
let results = search_file(csv_file.unwrap(), &query.q).unwrap(); let results = search_file(csv_file.unwrap(), &query.q).unwrap();
results[offset - 1..offset + size].join("") let last: usize = if offset + size < results.len() {
offset + size
} else {
results.len()
};
results[offset..last].join("")
} }
fn search_file(file: File, query: &str) -> Result<Vec<String>, Box<Error>> { fn search_file(file: File, query: &str) -> Result<Vec<String>, Box<Error>> {
let pattern = query.replace(" ", ".*"); let pattern = query.replace(" ", ".*");
let matcher = RegexMatcher::new_line_matcher(&pattern)?; let matcher = RegexMatcherBuilder::new()
.case_insensitive(true)
.build(&pattern)
.unwrap();
let mut matches: Vec<String> = vec![]; let mut matches: Vec<String> = vec![];
let mut searcher = SearcherBuilder::new() let mut searcher = SearcherBuilder::new()

View File

@ -1,5 +1,5 @@
import { render, Component, linkEvent } from 'inferno'; import { render, Component, linkEvent } from 'inferno';
import moment from 'moment'; import * as moment from 'moment';
import { endpoint } from './env'; import { endpoint } from './env';
import { SearchParams, Results, State } from './interfaces'; import { SearchParams, Results, State } from './interfaces';