53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
"""Zsh history parser."""
|
|
|
|
import os
|
|
import re
|
|
from datetime import datetime
|
|
from typing import List, Optional
|
|
|
|
from .base import HistoryParser, ParsedCommand
|
|
|
|
|
|
class ZshHistoryParser(HistoryParser):
|
|
"""Parser for Zsh history files."""
|
|
|
|
SHELL_NAME = "zsh"
|
|
DEFAULT_HISTORY_FILE = os.path.expanduser("~/.zsh_history")
|
|
|
|
ZSH_HISTORY_PATTERN = re.compile(
|
|
r'^: (\d+):(\d+);(.*)$',
|
|
re.MULTILINE
|
|
)
|
|
|
|
def parse_file(self, filepath: str) -> List[ParsedCommand]:
|
|
"""Parse a zsh history file."""
|
|
try:
|
|
with open(filepath, 'r', encoding='utf-8', errors='replace') as f:
|
|
content = f.read()
|
|
return self.parse_content(content)
|
|
except FileNotFoundError:
|
|
return []
|
|
except Exception:
|
|
return []
|
|
|
|
def parse_content(self, content: str) -> List[ParsedCommand]:
|
|
"""Parse zsh history content from a string."""
|
|
commands = []
|
|
matches = self.ZSH_HISTORY_PATTERN.findall(content)
|
|
|
|
for line_num, match in enumerate(matches, start=1):
|
|
timestamp_str, _ , command = match
|
|
try:
|
|
timestamp = datetime.fromtimestamp(int(timestamp_str))
|
|
except (ValueError, OSError):
|
|
timestamp = None
|
|
|
|
cmd = ParsedCommand(
|
|
raw_command=command,
|
|
timestamp=timestamp,
|
|
line_number=line_num
|
|
)
|
|
commands.append(cmd)
|
|
|
|
return commands
|