feat: color docs search output

This commit is contained in:
Peter Steinberger
2026-01-10 21:02:18 +01:00
parent 9bd5d4355c
commit 0ef429f532

View File

@@ -1,6 +1,8 @@
import { hasBinary } from "../agents/skills.js";
import { runCommandWithTimeout } from "../process/exec.js";
import type { RuntimeEnv } from "../runtime.js";
import { formatDocsLink } from "../terminal/links.js";
import { isRich, theme } from "../terminal/theme.js";
const SEARCH_TOOL = "https://docs.clawd.bot/mcp.SearchClawdbot";
const SEARCH_TIMEOUT_MS = 30_000;
@@ -125,6 +127,32 @@ function buildMarkdown(query: string, results: DocResult[]): string {
return lines.join("\n");
}
function formatLinkLabel(link: string): string {
return link.replace(/^https?:\/\//i, "");
}
function renderRichResults(
query: string,
results: DocResult[],
runtime: RuntimeEnv,
) {
runtime.log(`${theme.heading("Docs search:")} ${theme.info(query)}`);
if (results.length === 0) {
runtime.log(theme.muted("No results."));
return;
}
for (const item of results) {
const linkLabel = formatLinkLabel(item.link);
const link = formatDocsLink(item.link, linkLabel);
runtime.log(
`${theme.muted("-")} ${theme.command(item.title)} ${theme.muted("(")}${link}${theme.muted(")")}`,
);
if (item.snippet) {
runtime.log(` ${theme.muted(item.snippet)}`);
}
}
}
async function renderMarkdown(markdown: string, runtime: RuntimeEnv) {
const width = process.stdout.columns ?? 0;
const args = width > 0 ? ["--width", String(width)] : [];
@@ -149,8 +177,14 @@ export async function docsSearchCommand(
) {
const query = queryParts.join(" ").trim();
if (!query) {
runtime.log("Docs: https://docs.clawd.bot/");
runtime.log('Search: clawdbot docs "your query"');
const docs = formatDocsLink("/", "docs.clawd.bot");
if (isRich()) {
runtime.log(`${theme.muted("Docs:")} ${docs}`);
runtime.log(`${theme.muted("Search:")} clawdbot docs "your query"`);
} else {
runtime.log("Docs: https://docs.clawd.bot/");
runtime.log('Search: clawdbot docs "your query"');
}
return;
}
@@ -169,6 +203,10 @@ export async function docsSearchCommand(
}
const results = parseSearchOutput(res.stdout);
if (isRich()) {
renderRichResults(query, results, runtime);
return;
}
const markdown = buildMarkdown(query, results);
await renderMarkdown(markdown, runtime);
}