Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/cocoindex_code/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ def print_index_stats(status: ProjectStatusResponse) -> None:
_typer.echo(f" {lang}: {count} chunks")


def _echo_search_text(text: str) -> None:
"""Echo result text, replacing characters unsupported by the console codec."""
try:
_typer.echo(text)
except UnicodeEncodeError as error:
safe_text = text.encode(error.encoding, errors="replace").decode(error.encoding)
_typer.echo(safe_text)


def print_search_results(response: SearchResponse) -> None:
"""Print formatted search results."""
if not response.success:
Expand All @@ -207,8 +216,8 @@ def print_search_results(response: SearchResponse) -> None:

for i, r in enumerate(response.results, 1):
_typer.echo(f"\n--- Result {i} (score: {r.score:.3f}) ---")
_typer.echo(f"File: {r.file_path}:{r.start_line}-{r.end_line} [{r.language}]")
_typer.echo(r.content)
_echo_search_text(f"File: {r.file_path}:{r.start_line}-{r.end_line} [{r.language}]")
_echo_search_text(r.content)


def _run_index_with_progress(project_root: str) -> None:
Expand Down
32 changes: 32 additions & 0 deletions tests/test_cli_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import io
from pathlib import Path

import pytest
Expand All @@ -13,6 +14,37 @@
require_project_root,
resolve_default_path,
)
from cocoindex_code.protocol import SearchResponse, SearchResult


def test_print_search_results_replaces_unencodable_console_characters(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Search output stays usable when the console cannot encode a result."""
raw_output = io.BytesIO()
gbk_stdout = io.TextIOWrapper(raw_output, encoding="gbk", errors="strict")
monkeypatch.setattr(cli.sys, "stdout", gbk_stdout)
response = SearchResponse(
success=True,
results=[
SearchResult(
file_path="notes↔.md",
language="markdown",
content="可编码内容: left ↔ right",
start_line=1,
end_line=1,
score=0.9,
)
],
)

cli.print_search_results(response)
gbk_stdout.flush()

output = raw_output.getvalue().decode("gbk")
assert "可编码内容" in output
assert "File: notes?.md" in output
assert "left ? right" in output


def test_require_project_root_success(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
Expand Down
Loading