"""Utility functions for shell history automation.""" import os import re from datetime import datetime from typing import Optional def normalize_command(command: str) -> str: """Normalize a shell command for comparison.""" return command.strip() def extract_command_keywords(command: str) -> list[str]: """Extract keywords from a command.""" parts = command.split() keywords = [p for p in parts if not p.startswith("-")] return keywords def format_timestamp(dt: Optional[datetime]) -> str: """Format a datetime object to a readable string.""" if dt is None: return "Unknown" return dt.strftime("%Y-%m-%d %H:%M:%S") def parse_timestamp(ts: str) -> Optional[datetime]: """Parse a timestamp string to datetime.""" try: return datetime.strptime(ts, "%Y-%m-%d %H:%M:%S") except ValueError: return None def escape_shell_string(s: str) -> str: """Escape special characters in a string for shell use.""" return re.sub(r"([\\'"])", r"\\\1", s)