diff --git a/shell_alias_gen/parsers/bash.py b/shell_alias_gen/parsers/bash.py new file mode 100644 index 0000000..58dd627 --- /dev/null +++ b/shell_alias_gen/parsers/bash.py @@ -0,0 +1,41 @@ +"""Bash history parser.""" + +import os +from datetime import datetime +from typing import List, Optional + +from .base import HistoryParser, ParsedCommand + + +class BashHistoryParser(HistoryParser): + """Parser for Bash history files.""" + + SHELL_NAME = "bash" + DEFAULT_HISTORY_FILE = os.path.expanduser("~/.bash_history") + + def parse_file(self, filepath: str) -> List[ParsedCommand]: + """Parse a bash 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 bash history content from a string.""" + commands = [] + lines = content.splitlines() + + for line_num, line in enumerate(lines, start=1): + line = line.rstrip('\n\r') + if line and not line.startswith('#'): + cmd = ParsedCommand( + raw_command=line, + line_number=line_num + ) + commands.append(cmd) + + return commands