import { colors } from "@cliffy/ansi/colors"; import { Command, ValidationError } from "@cliffy/command"; import { maxOf } from "@std/collections"; import { search } from "../search.ts"; import { print } from "../utils.ts"; import { Hsh } from "../hsh.ts"; import { iconForEntry } from "../entry.ts"; function register(cmd: Command, hsh: Hsh) { cmd .command("search", "search notebook") .alias("q") .option("-a, --all", "search across all notebooks", { default: false }) .option("-e, --regexp", "search by regex", { default: false }) .option("-t, --tag ", "include tags", { collect: true }) .option("-T, --title-only", "only show entry titles", { default: false }) .arguments("[...terms:string]") .action(async ({ all, regexp, titleOnly, tag }, ...terms: string[]) => { if (terms.length === 0 && (tag?.length ?? 0) === 0) { throw new ValidationError("at least one term or tag must be specified"); } const notebooks = all ? hsh.allNotebooks() : [hsh.getNotebook()]; const matches = await search( notebooks, [...terms, ...(tag ?? []).map((t) => `#${t}`)], !regexp, ); for (const match of matches) { print( `[${ colors.cyan( match.entry.folders.length > 0 ? `${match.entry.folders.join("/")}/` : "", ) }${colors.cyan(match.entry.filename)}] ${ iconForEntry(match.entry) }${match.entry.title}`, ); if (titleOnly) continue; print("-".repeat(Deno.consoleSize().columns)); const width = Math.log10(maxOf(match.matches, (m) => m.line)!) + 1; for (const line of match.matches) { print( `${ colors.green(line.line.toString().padStart(width)) }: ${line.text}`, ); } print(); } }); } export default { register };