import type { Command } from "commander"; import { getContactStore } from "../contacts/index.js"; import type { Platform } from "../contacts/types.js"; import { danger } from "../globals.js"; import { defaultRuntime } from "../runtime.js"; import { formatDocsLink } from "../terminal/links.js"; import { renderTable } from "../terminal/table.js"; import { theme } from "../terminal/theme.js"; function formatTimestamp(ts: number): string { const date = new Date(ts); const now = new Date(); const diffMs = now.getTime() - date.getTime(); const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24)); if (diffDays === 0) { return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); } if (diffDays === 1) { return "Yesterday"; } if (diffDays < 7) { return date.toLocaleDateString([], { weekday: "short" }); } return date.toLocaleDateString([], { month: "short", day: "numeric" }); } function parseTimestamp(value: string): number | null { if (!value) return null; // Handle relative times like "1h", "2d", "1w" const relativeMatch = value.match(/^(\d+)([hdwm])$/i); if (relativeMatch) { const amount = parseInt(relativeMatch[1]!, 10); const unit = relativeMatch[2]!.toLowerCase(); const now = Date.now(); switch (unit) { case "h": return now - amount * 60 * 60 * 1000; case "d": return now - amount * 24 * 60 * 60 * 1000; case "w": return now - amount * 7 * 24 * 60 * 60 * 1000; case "m": return now - amount * 30 * 24 * 60 * 60 * 1000; } } // Handle ISO date strings const parsed = Date.parse(value); if (!isNaN(parsed)) { return parsed; } return null; } const VALID_PLATFORMS: Platform[] = [ "whatsapp", "telegram", "discord", "slack", "signal", "imessage", ]; export function registerSearchCli(program: Command) { program .command("search") .description("Search messages across all messaging platforms") .argument("", "Search query") .option("--from ", "Filter by sender (contact name, username, or ID)") .option( "--platform ", "Filter by platform (whatsapp, telegram, discord, slack, signal, imessage)", ) .option("--since