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:
41
shell_alias_gen/parsers/bash.py
Normal file
41
shell_alias_gen/parsers/bash.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user