From a207ba6302ca0a9755b2e16b6ca7b42fcdf50a05 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Sat, 22 Feb 2020 14:22:09 -0800 Subject: [PATCH] split out History --- babi/history.py | 34 ++++++++++++++++++++++++++++++++++ babi/main.py | 30 +----------------------------- 2 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 babi/history.py diff --git a/babi/history.py b/babi/history.py new file mode 100644 index 0000000..4c85213 --- /dev/null +++ b/babi/history.py @@ -0,0 +1,34 @@ +import collections +import contextlib +import os.path +from typing import Dict +from typing import Generator +from typing import List + + +class History: + def __init__(self) -> None: + self._orig_len: Dict[str, int] = collections.defaultdict(int) + self.data: Dict[str, List[str]] = collections.defaultdict(list) + self.prev: Dict[str, str] = {} + + @contextlib.contextmanager + def save(self) -> Generator[None, None, None]: + history_dir = os.path.join( + os.environ.get('XDG_DATA_HOME') or + os.path.expanduser('~/.local/share'), + 'babi/history', + ) + os.makedirs(history_dir, exist_ok=True) + for filename in os.listdir(history_dir): + with open(os.path.join(history_dir, filename)) as f: + self.data[filename] = f.read().splitlines() + self._orig_len[filename] = len(self.data[filename]) + try: + yield + finally: + for k, v in self.data.items(): + new_history = v[self._orig_len[k]:] + if new_history: + with open(os.path.join(history_dir, k), 'a+') as f: + f.write('\n'.join(new_history) + '\n') diff --git a/babi/main.py b/babi/main.py index a83b3a4..ed4ebdf 100644 --- a/babi/main.py +++ b/babi/main.py @@ -14,7 +14,6 @@ import sys from typing import Any from typing import Callable from typing import cast -from typing import Dict from typing import Generator from typing import IO from typing import List @@ -27,6 +26,7 @@ from typing import Tuple from typing import TypeVar from typing import Union +from babi.history import History from babi.horizontal_scrolling import line_x from babi.horizontal_scrolling import scrolled_line from babi.list_spy import ListSpy @@ -76,34 +76,6 @@ class Key(NamedTuple): keyname: bytes -class History: - def __init__(self) -> None: - self._orig_len: Dict[str, int] = collections.defaultdict(int) - self.data: Dict[str, List[str]] = collections.defaultdict(list) - self.prev: Dict[str, str] = {} - - @contextlib.contextmanager - def save(self) -> Generator[None, None, None]: - history_dir = os.path.join( - os.environ.get('XDG_DATA_HOME') or - os.path.expanduser('~/.local/share'), - 'babi/history', - ) - os.makedirs(history_dir, exist_ok=True) - for filename in os.listdir(history_dir): - with open(os.path.join(history_dir, filename)) as f: - self.data[filename] = f.read().splitlines() - self._orig_len[filename] = len(self.data[filename]) - try: - yield - finally: - for k, v in self.data.items(): - new_history = v[self._orig_len[k]:] - if new_history: - with open(os.path.join(history_dir, k), 'a+') as f: - f.write('\n'.join(new_history) + '\n') - - def _restore_lines_eof_invariant(lines: MutableSequenceNoSlice) -> None: """The file lines will always contain a blank empty string at the end to simplify rendering. This should be called whenever the end of the file