2026-10-12 15:51:39 +02:00
|
|
|
|
//! Thin command-line adapter around the shared, CPU-only search engine.
|
|
|
|
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
|
|
use anyhow::{bail, Context, Result};
|
|
|
|
|
|
use clap::{Parser, Subcommand};
|
2026-10-12 16:07:53 +02:00
|
|
|
|
use searchi_core::{IndexOptions, SearchIndex, SearchOptions};
|
2026-10-12 15:51:39 +02:00
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Parser)]
|
|
|
|
|
|
#[command(name = "searchi", version, about = "Search your local files offline")]
|
|
|
|
|
|
struct Cli {
|
|
|
|
|
|
#[command(subcommand)]
|
|
|
|
|
|
command: Command,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Subcommand)]
|
|
|
|
|
|
enum Command {
|
|
|
|
|
|
/// Discover files and refresh an index for one folder.
|
|
|
|
|
|
Index {
|
|
|
|
|
|
/// SQLite index file; parent directory must exist.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
db: PathBuf,
|
|
|
|
|
|
/// Folder to index recursively.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
root: PathBuf,
|
2026-10-12 16:07:53 +02:00
|
|
|
|
/// Additional directory names to exclude; repeat this flag as needed.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
exclude: Vec<String>,
|
2026-10-12 15:51:39 +02:00
|
|
|
|
},
|
|
|
|
|
|
/// Search filenames and indexed document content.
|
|
|
|
|
|
Search {
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
db: PathBuf,
|
|
|
|
|
|
/// Search text; quote phrases containing spaces in your shell.
|
|
|
|
|
|
query: String,
|
|
|
|
|
|
/// Maximum number of returned results (1–1000).
|
|
|
|
|
|
#[arg(long, default_value_t = 20, value_parser = clap::value_parser!(u16).range(1..=1000))]
|
|
|
|
|
|
limit: u16,
|
|
|
|
|
|
/// Emit a JSON array for scripting.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
json: bool,
|
2026-10-12 16:07:53 +02:00
|
|
|
|
/// Restrict results to this folder and descendants.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
folder: Option<PathBuf>,
|
|
|
|
|
|
/// File extension, with or without the leading dot.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
extension: Option<String>,
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
min_size: Option<u64>,
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
max_size: Option<u64>,
|
|
|
|
|
|
/// Inclusive modification time in Unix milliseconds.
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
modified_after_ms: Option<i64>,
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
modified_before_ms: Option<i64>,
|
2026-10-12 15:51:39 +02:00
|
|
|
|
},
|
|
|
|
|
|
/// Show the indexed file and content counts.
|
|
|
|
|
|
Stats {
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
db: PathBuf,
|
|
|
|
|
|
#[arg(long)]
|
|
|
|
|
|
json: bool,
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() -> Result<()> {
|
|
|
|
|
|
match Cli::parse().command {
|
2026-10-12 16:07:53 +02:00
|
|
|
|
Command::Index { db, root, exclude } => {
|
|
|
|
|
|
let mut options = IndexOptions::default();
|
|
|
|
|
|
options.excluded_directory_names.extend(exclude);
|
2026-10-12 15:51:39 +02:00
|
|
|
|
let mut index = SearchIndex::open(&db)
|
|
|
|
|
|
.with_context(|| format!("Cannot open index {}", db.display()))?;
|
|
|
|
|
|
let report = index
|
2026-10-12 16:07:53 +02:00
|
|
|
|
.index_folder_with_options(&root, &options)
|
2026-10-12 15:51:39 +02:00
|
|
|
|
.with_context(|| format!("Cannot index folder {}", root.display()))?;
|
|
|
|
|
|
println!(
|
2026-10-12 16:07:53 +02:00
|
|
|
|
"Discovered: {} Indexed: {} Unchanged: {} Removed: {} Skipped: {} Extraction failures: {}",
|
|
|
|
|
|
report.discovered, report.indexed, report.unchanged, report.removed, report.skipped, report.failures
|
2026-10-12 15:51:39 +02:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
Command::Search {
|
|
|
|
|
|
db,
|
|
|
|
|
|
query,
|
|
|
|
|
|
limit,
|
|
|
|
|
|
json,
|
2026-10-12 16:07:53 +02:00
|
|
|
|
folder,
|
|
|
|
|
|
extension,
|
|
|
|
|
|
min_size,
|
|
|
|
|
|
max_size,
|
|
|
|
|
|
modified_after_ms,
|
|
|
|
|
|
modified_before_ms,
|
2026-10-12 15:51:39 +02:00
|
|
|
|
} => {
|
|
|
|
|
|
if query.trim().is_empty() {
|
|
|
|
|
|
bail!("Search text cannot be empty");
|
|
|
|
|
|
}
|
|
|
|
|
|
require_existing_index(&db)?;
|
|
|
|
|
|
let index = SearchIndex::open(&db)
|
|
|
|
|
|
.with_context(|| format!("Cannot open index {}", db.display()))?;
|
|
|
|
|
|
let results = index
|
2026-10-12 16:07:53 +02:00
|
|
|
|
.search_with_options(
|
|
|
|
|
|
&query,
|
|
|
|
|
|
usize::from(limit),
|
|
|
|
|
|
&SearchOptions {
|
|
|
|
|
|
folder,
|
|
|
|
|
|
extension,
|
|
|
|
|
|
min_size,
|
|
|
|
|
|
max_size,
|
|
|
|
|
|
modified_after_unix_ms: modified_after_ms,
|
|
|
|
|
|
modified_before_unix_ms: modified_before_ms,
|
|
|
|
|
|
},
|
|
|
|
|
|
)
|
2026-10-12 15:51:39 +02:00
|
|
|
|
.context("Search failed")?;
|
|
|
|
|
|
if json {
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&results)?);
|
|
|
|
|
|
} else if results.is_empty() {
|
|
|
|
|
|
println!("No matching files.");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
for result in results {
|
|
|
|
|
|
println!("{} [{}]", result.path, result.match_kind);
|
2026-10-12 16:07:53 +02:00
|
|
|
|
if let Some(source) = result.source {
|
|
|
|
|
|
println!(" {} {}", source.kind, source.number);
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(error) = result.extraction_error {
|
|
|
|
|
|
println!(" Text extraction unavailable: {error}");
|
|
|
|
|
|
}
|
2026-10-12 15:51:39 +02:00
|
|
|
|
if !result.snippet.is_empty() {
|
|
|
|
|
|
println!(" {}", result.snippet.replace(['\r', '\n'], " "));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Command::Stats { db, json } => {
|
|
|
|
|
|
require_existing_index(&db)?;
|
|
|
|
|
|
let index = SearchIndex::open(&db)
|
|
|
|
|
|
.with_context(|| format!("Cannot open index {}", db.display()))?;
|
|
|
|
|
|
let stats = index.stats().context("Cannot read index statistics")?;
|
|
|
|
|
|
if json {
|
|
|
|
|
|
println!("{}", serde_json::to_string_pretty(&stats)?);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
println!(
|
|
|
|
|
|
"Files: {} Files with searchable content: {}",
|
|
|
|
|
|
stats.files, stats.content_files
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Read commands must not silently create a new empty database after a typo.
|
|
|
|
|
|
fn require_existing_index(db: &std::path::Path) -> Result<()> {
|
|
|
|
|
|
if !db.is_file() {
|
|
|
|
|
|
bail!(
|
|
|
|
|
|
"Index {} does not exist; run `searchi index --db <path> --root <folder>` first",
|
|
|
|
|
|
db.display()
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn rejects_zero_or_excessive_result_limits() {
|
|
|
|
|
|
for limit in ["0", "1001"] {
|
|
|
|
|
|
assert!(Cli::try_parse_from([
|
|
|
|
|
|
"searchi", "search", "--db", "index.db", "hello", "--limit", limit
|
|
|
|
|
|
])
|
|
|
|
|
|
.is_err());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn supports_json_search_and_quoted_query() {
|
|
|
|
|
|
let cli = Cli::try_parse_from([
|
|
|
|
|
|
"searchi",
|
|
|
|
|
|
"search",
|
|
|
|
|
|
"--db",
|
|
|
|
|
|
"index.db",
|
|
|
|
|
|
"termination clause",
|
|
|
|
|
|
"--json",
|
|
|
|
|
|
])
|
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
assert!(matches!(
|
|
|
|
|
|
cli.command,
|
|
|
|
|
|
Command::Search {
|
|
|
|
|
|
json: true,
|
|
|
|
|
|
limit: 20,
|
|
|
|
|
|
..
|
|
|
|
|
|
}
|
|
|
|
|
|
));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|