Initial upload: shell-history-semantic-search v0.1.0
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
39
src/shell_history_search/parsers/bash.py
Normal file
39
src/shell_history_search/parsers/bash.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from typing import Iterator
|
||||||
|
import re
|
||||||
|
|
||||||
|
from shell_history_search.parsers import HistoryEntry, HistoryParser
|
||||||
|
|
||||||
|
|
||||||
|
class BashHistoryParser(HistoryParser):
|
||||||
|
shell_type = "bash"
|
||||||
|
|
||||||
|
def get_history_path(self) -> Path:
|
||||||
|
return Path.home() / ".bash_history"
|
||||||
|
|
||||||
|
def parse(self, history_path: Path) -> Iterator[HistoryEntry]:
|
||||||
|
timestamp_pattern = re.compile(r"^#(\d{10,})$")
|
||||||
|
|
||||||
|
for line in self.read_history_file(history_path):
|
||||||
|
line = line.strip()
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.startswith("#"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
timestamp_match = timestamp_pattern.match(line)
|
||||||
|
if timestamp_match:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.startswith(" "):
|
||||||
|
continue
|
||||||
|
|
||||||
|
entry = HistoryEntry(
|
||||||
|
command=line,
|
||||||
|
shell_type=self.shell_type,
|
||||||
|
timestamp=None,
|
||||||
|
hostname=None,
|
||||||
|
command_hash=HistoryEntry.create_hash(line),
|
||||||
|
)
|
||||||
|
yield entry
|
||||||
Reference in New Issue
Block a user