33from __future__ import annotations
44
55import functools
6+ import json as _json
67import os
78import sys
89from collections .abc import Callable
@@ -173,6 +174,28 @@ def print_search_results(response: SearchResponse) -> None:
173174 _typer .echo (r .content )
174175
175176
177+ def print_search_results_json (response : SearchResponse ) -> None :
178+ """Print search results as machine-readable JSON."""
179+ payload = {
180+ "success" : response .success ,
181+ "results" : [
182+ {
183+ "file_path" : r .file_path ,
184+ "language" : r .language ,
185+ "content" : r .content ,
186+ "start_line" : r .start_line ,
187+ "end_line" : r .end_line ,
188+ "score" : r .score ,
189+ }
190+ for r in response .results
191+ ],
192+ "total_returned" : response .total_returned ,
193+ "offset" : response .offset ,
194+ "message" : response .message ,
195+ }
196+ _typer .echo (_json .dumps (payload , indent = 2 ))
197+
198+
176199def _run_index_with_progress (project_root : str ) -> None :
177200 """Run indexing with streaming progress display. Exits on failure."""
178201 from rich .console import Console as _Console
@@ -543,6 +566,7 @@ def search(
543566 offset : int = _typer .Option (0 , "--offset" , help = "Number of results to skip" ),
544567 limit : int = _typer .Option (10 , "--limit" , help = "Maximum results to return" ),
545568 refresh : bool = _typer .Option (False , "--refresh" , help = "Refresh index before searching" ),
569+ json_output : bool = _typer .Option (False , "--json" , help = "Print results as JSON" ),
546570) -> None :
547571 """Semantic search across the codebase."""
548572 project_root = str (require_project_root ())
@@ -568,7 +592,10 @@ def search(
568592 limit = limit ,
569593 offset = offset ,
570594 )
571- print_search_results (resp )
595+ if json_output :
596+ print_search_results_json (resp )
597+ else :
598+ print_search_results (resp )
572599
573600
574601@app .command ()
0 commit comments