From 5410055f9f9f2a778fd6fab09125a2aec5c71a2f Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Thu, 12 Mar 2026 23:00:07 +0000 Subject: [PATCH] Implement find command with language/file filtering and rendering --- crates/bough-cli/src/main.rs | 23 ++++++++- crates/bough-cli/src/render.rs | 89 +++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/crates/bough-cli/src/main.rs b/crates/bough-cli/src/main.rs index b1d9679b..11d31cdb 100644 --- a/crates/bough-cli/src/main.rs +++ b/crates/bough-cli/src/main.rs @@ -505,8 +505,27 @@ fn main() { Box::new(Noop) } - Command::Find { .. } => { - todo!("find command") + Command::Find { ref lang, ref file } => { + let mut session = Session::new(cli.config.clone()).expect("session creation"); + session.tend_add_missing_states().expect("tend states"); + let results = session.find_best_mutations().expect("find best mutations"); + let filtered: Vec<_> = results + .into_iter() + .filter(|(_, state, _)| { + if let Some(l) = lang { + if state.mutation().mutant().lang() != *l { + return false; + } + } + if let Some(f) = file { + if state.mutation().mutant().twig().path() != f.as_path() { + return false; + } + } + true + }) + .collect(); + Box::new(render::FindBestMutations(filtered)) } Command::Noop => { diff --git a/crates/bough-cli/src/render.rs b/crates/bough-cli/src/render.rs index 3fcfc3a3..cb295b38 100644 --- a/crates/bough-cli/src/render.rs +++ b/crates/bough-cli/src/render.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use bough_core::{LanguageId, Mutation, State, Status}; +use bough_core::{LanguageId, Mutation, MutationHash, State, Status}; use bough_typed_hash::{TypedHash, TypedHashable, UnvalidatedHash}; use facet::Facet; @@ -846,6 +846,93 @@ impl Render for UnapplyMutation { } } +pub struct FindBestMutations(pub Vec<(MutationHash, State, f64)>); + +impl Render for FindBestMutations { + fn markdown(&self) -> String { + let mut out = format!("# Find Best Mutations\n\n{} selected\n\n", self.0.len()); + out.push_str("| # | Score | Hash | File | Location | Kind | Subst |\n"); + out.push_str("|---|-------|------|------|----------|------|-------|\n"); + for (i, (hash, state, score)) in self.0.iter().enumerate() { + let m = state.mutation(); + let mutant = m.mutant(); + out.push_str(&format!( + "| {} | {:.2} | `{}` | {} | {}:{} | {:?} | `{}` |\n", + i + 1, + score, + hash, + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + )); + } + out + } + + fn terse(&self) -> String { + self.0 + .iter() + .map(|(_, state, score)| { + let m = state.mutation(); + let mutant = m.mutant(); + format!( + "{HASH}{}{RESET} {:.2} {} {PATH}{}:{}:{}{RESET} {:?} → {STRING}{}{RESET}", + mutation_hash(m), + score, + fmt_status_colored(state), + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + ) + }) + .collect::>() + .join("\n") + } + + fn verbose(&self) -> String { + let mut out = format!("{TITLE}Find Best Mutations{RESET} ({} selected)\n", self.0.len()); + for (i, (hash, state, score)) in self.0.iter().enumerate() { + let m = state.mutation(); + let mutant = m.mutant(); + out.push_str(&format!( + "\n {HASH}#{}{RESET} score={:.2} {HASH}{}{RESET} {} {PATH}{}:{}:{}{RESET} {:?} → {STRING}{}{RESET}", + i + 1, + score, + hash, + fmt_status_colored(state), + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + mutant.kind(), + m.subst(), + )); + } + out + } + + fn json(&self) -> String { + let items: Vec = self.0.iter().map(|(hash, state, score)| { + let m = state.mutation(); + let mutant = m.mutant(); + format!( + r#"{{"hash":"{}","score":{},"file":"{}","line":{},"col":{},"kind":"{}","subst":"{}"}}"#, + hash, + score, + mutant.twig().path().display(), + mutant.span().start().line() + 1, + mutant.span().start().col() + 1, + format!("{:?}", mutant.kind()), + m.subst(), + ) + }).collect(); + format!("[{}]", items.join(",")) + } +} + impl Render for Config { fn markdown(&self) -> String { format!( -- knotmirror 0.0.1