1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::printer::{COLOR_CYAN, COLOR_GRAY, COLOR_RESET};
use crate::renderers::RESULT_PREFIX;
use distri_types::{Part, ToolResponse};
/// Render search tool results.
pub fn render_search(result: &ToolResponse) {
for part in &result.parts {
match part {
Part::Data(value) => {
if let Some(obj) = value.as_object()
&& let Some(serde_json::Value::Array(items)) = obj.get("data") {
let count = items.len();
println!(
"{}{}{} result{}{}",
COLOR_CYAN,
RESULT_PREFIX,
count,
if count == 1 { "" } else { "s" },
COLOR_RESET
);
for (i, item) in items.iter().take(3).enumerate() {
let title = item
.get("title")
.and_then(|t| t.as_str())
.unwrap_or("(untitled)");
println!("{} {}. {}{}", COLOR_GRAY, i + 1, title, COLOR_RESET);
}
if count > 3 {
println!(
"{} … and {} more{}",
COLOR_GRAY,
count - 3,
COLOR_RESET
);
}
continue;
}
crate::renderers::data::render_data_compact(value);
}
Part::Text(text) => {
let first = text.lines().next().unwrap_or("");
println!("{}{}{}{}", COLOR_GRAY, RESULT_PREFIX, first, COLOR_RESET);
}
_ => {}
}
}
}