Have history append instead of overwrite

This commit is contained in:
Anthony Sottile
2019-11-30 16:00:13 -08:00
parent b4f7cabb28
commit 26d3c0826c
2 changed files with 26 additions and 2 deletions

View File

@@ -206,6 +206,7 @@ class Status:
self._status = ''
self._action_counter = -1
self._history: Dict[str, List[str]] = collections.defaultdict(list)
self._history_orig_len: Dict[str, int] = {}
@contextlib.contextmanager
def save_history(self) -> Generator[None, None, None]:
@@ -218,12 +219,13 @@ class Status:
for filename in os.listdir(history_dir):
with open(os.path.join(history_dir, filename)) as f:
self._history[filename] = f.read().splitlines()
self._history_orig_len[filename] = len(self._history[filename])
try:
yield
finally:
for k, v in self._history.items():
with open(os.path.join(history_dir, k), 'w') as f:
f.write('\n'.join(v) + '\n')
with open(os.path.join(history_dir, k), 'a+') as f:
f.write('\n'.join(v[self._history_orig_len[k]:]) + '\n')
def update(self, status: str) -> None:
self._status = status

View File

@@ -784,6 +784,28 @@ def test_search_history_is_saved_between_sessions(xdg_data_home):
h.press('Enter')
def test_multiple_sessions_append_to_history(xdg_data_home):
xdg_data_home.join('babi/history/search').ensure().write(
'orig\n'
'history\n',
)
with run() as h1, and_exit(h1):
with run() as h2, and_exit(h2):
h2.press('^W')
h2.press_and_enter('h2 history')
h1.press('^W')
h1.press_and_enter('h1 history')
contents = xdg_data_home.join('babi/history/search').read()
assert contents == (
'orig\n'
'history\n'
'h2 history\n'
'h1 history\n'
)
def test_search_reverse_search_history(xdg_data_home, ten_lines):
xdg_data_home.join('babi/history/search').ensure().write(
'line_5\n'