use clap::Parser;
use ix::builder::Builder;
use ix::idle::{IdleTracker};
use ix::watcher::Watcher;
use ix::format::Beacon;
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
#[derive(Parser)]
#[command(
name = "ixd",
version = "0.1.0",
about = "ix background daemon. Automatically maintains a fresh trigram index.",
after_help = "The daemon monitors the filesystem for changes and incrementally updates the index.
It remains active in the background to ensure the index is always fresh."
)]
struct Cli {
#[arg(default_value = ".", value_name = "PATH")]
path: PathBuf,
}
fn main() -> ix::error::Result<()> {
let cli = Cli::parse();
let root = cli.path.canonicalize().map_err(ix::error::Error::Io)?;
println!("ixd: watching {}...", root.display());
let mut builder = Builder::new(&root);
builder.build()?;
println!(
"ixd: initial build complete ({} files, {} trigrams)",
builder.files_len(),
builder.trigrams_len()
);
let mut watcher = Watcher::new(&root)?;
let rx = watcher.start()?;
let ix_dir = root.join(".ix");
if !ix_dir.exists() {
fs::create_dir_all(&ix_dir)?;
}
let mut beacon = Beacon::new(&root);
beacon.write_to(&ix_dir)?;
let mut idle = IdleTracker::new();
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
ctrlc::set_handler(move || {
r.store(false, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");
while running.load(Ordering::SeqCst) {
match rx.recv_timeout(Duration::from_secs(5)) {
Ok(changed_files) => {
println!(
"ixd: {} files changed, updating index...",
changed_files.len()
);
beacon.status = "indexing".to_string();
beacon.last_event_at = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
let _ = beacon.write_to(&ix_dir);
idle.record_change();
builder.update(&changed_files)?;
beacon.status = "idle".to_string();
let _ = beacon.write_to(&ix_dir);
}
Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
continue;
}
Err(crossbeam_channel::RecvTimeoutError::Disconnected) => break,
}
}
println!("ixd: shutting down");
let _ = fs::remove_file(ix_dir.join("beacon.json"));
watcher.stop();
Ok(())
}