Initial upload: Shell History Alias Generator with full test suite
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
52
shell_alias_gen/parsers/zsh.py
Normal file
52
shell_alias_gen/parsers/zsh.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user