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:
72
src/shell_history_search/parsers/fish.py
Normal file
72
src/shell_history_search/parsers/fish.py
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from typing import Iterator
|
||||||
|
import re
|
||||||
|
|
||||||
|
from shell_history_search.parsers import HistoryEntry, HistoryParser
|
||||||
|
|
||||||
|
|
||||||
|
class FishHistoryParser(HistoryParser):
|
||||||
|
shell_type = "fish"
|
||||||
|
|
||||||
|
def get_history_path(self) -> Path:
|
||||||
|
return Path.home() / ".local" / "share" / "fish" / "fish_history"
|
||||||
|
|
||||||
|
def parse(self, history_path: Path) -> Iterator[HistoryEntry]:
|
||||||
|
timestamp_pattern = re.compile(r"^- (\d+)$")
|
||||||
|
cmd_pattern = re.compile(r"^cmd (\d+) \(.+?\)$")
|
||||||
|
|
||||||
|
current_timestamp: int | None = None
|
||||||
|
current_cmd: str | None = None
|
||||||
|
|
||||||
|
for line in self.read_history_file(history_path):
|
||||||
|
line = line.strip()
|
||||||
|
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
timestamp_match = timestamp_pattern.match(line)
|
||||||
|
if timestamp_match:
|
||||||
|
try:
|
||||||
|
current_timestamp = int(timestamp_match.group(1))
|
||||||
|
except ValueError:
|
||||||
|
current_timestamp = None
|
||||||
|
continue
|
||||||
|
|
||||||
|
cmd_match = cmd_pattern.match(line)
|
||||||
|
if cmd_match:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.startswith("cmd "):
|
||||||
|
if current_cmd is not None:
|
||||||
|
entry = HistoryEntry(
|
||||||
|
command=current_cmd,
|
||||||
|
shell_type=self.shell_type,
|
||||||
|
timestamp=current_timestamp,
|
||||||
|
hostname=None,
|
||||||
|
command_hash=HistoryEntry.create_hash(current_cmd),
|
||||||
|
)
|
||||||
|
yield entry
|
||||||
|
|
||||||
|
current_cmd = line[4:]
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.startswith("status ") or line.startswith("when "):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if line.startswith(" begin") or line.startswith(" end"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if current_cmd is None:
|
||||||
|
current_cmd = line
|
||||||
|
else:
|
||||||
|
current_cmd = current_cmd + "\n" + line
|
||||||
|
|
||||||
|
if current_cmd is not None:
|
||||||
|
entry = HistoryEntry(
|
||||||
|
command=current_cmd,
|
||||||
|
shell_type=self.shell_type,
|
||||||
|
timestamp=current_timestamp,
|
||||||
|
hostname=None,
|
||||||
|
command_hash=HistoryEntry.create_hash(current_cmd),
|
||||||
|
)
|
||||||
|
yield entry
|
||||||
Reference in New Issue
Block a user