diff --git a/cli_memory/commands/search.py b/cli_memory/commands/search.py new file mode 100644 index 0000000..e9b3041 --- /dev/null +++ b/cli_memory/commands/search.py @@ -0,0 +1,122 @@ +import click +from rich import print as rprint +from rich.panel import Panel +from rich.table import Table + +from ..config import Config +from ..database import Database +from ..search import SearchEngine +from ..history import HistoryManager + + +@click.group() +def search(): + """Search command history.""" + pass + + +@search.command() +@click.argument("query") +@click.option("--project", "-p", help="Filter by project ID") +@click.option("--type", "-t", help="Filter by command type") +@click.option("--fuzzy", "-f", is_flag=True, help="Enable fuzzy search") +@click.option("--limit", "-l", default=20, help="Maximum results") +def commands(query, project, type, fuzzy, limit): + """Search recorded commands.""" + db = Database() + search_engine = SearchEngine() + + project_id = int(project) if project else None + commands = db.get_commands(limit=1000) + + results = search_engine.search_commands( + commands, query, + project_id=project_id, + command_type=type, + fuzzy=fuzzy, + limit=limit, + ) + + db.close() + + if not results: + rprint(f"[yellow]No commands found matching '{query}'[/yellow]") + return + + table = Table(title=f"Search Results: {query}") + table.add_column("Command", style="cyan") + table.add_column("Type", style="magenta") + table.add_column("Time", style="green") + + for cmd in results[:limit]: + table.add_row( + cmd.command[:60] + "..." if len(cmd.command) > 60 else cmd.command, + cmd.command_type.value, + cmd.timestamp.strftime("%Y-%m-%d %H:%M"), + ) + + rprint(Panel(table, title=f"Found {len(results)} commands")) + + +@search.command() +@click.argument("technology") +def tech(technology): + """Search by technology stack.""" + db = Database() + search_engine = SearchEngine() + + commands = db.get_commands(limit=1000) + results = search_engine.search_by_technology(commands, technology) + + db.close() + + rprint(f"[green]Found {len(results)} commands for technology: {technology}[/green]") + + +@search.command() +@click.option("--hours", "-h", default=24, help="Hours to look back") +def recent(hours): + """Show recent commands.""" + db = Database() + search_engine = SearchEngine() + + commands = db.get_commands(limit=1000) + results = search_engine.search_recent(commands, hours=hours) + + db.close() + + table = Table(title=f"Recent Commands (Last {hours} hours)") + table.add_column("Command", style="cyan") + table.add_column("Type", style="magenta") + + for cmd in results[:50]: + table.add_row( + cmd.command[:70] if len(cmd.command) > 70 else cmd.command, + cmd.command_type.value, + ) + + rprint(Panel(table, title=f"{len(results)} commands")) + + +@search.command() +def stats(): + """Show command statistics.""" + db = Database() + search_engine = SearchEngine() + + commands = db.get_commands(limit=10000) + stats = search_engine.get_command_statistics(commands) + + db.close() + + table = Table(title="Command Statistics") + table.add_column("Metric", style="cyan") + table.add_column("Value", style="magenta") + + table.add_row("Total Commands", str(stats["total_commands"])) + table.add_row("Avg Duration (ms)", str(stats["avg_duration_ms"])) + + for cmd_type, count in stats["by_type"].items(): + table.add_row(f"Type: {cmd_type}", str(count)) + + rprint(Panel(table, title="Statistics"))