-
Notifications
You must be signed in to change notification settings - Fork 5
/
history.py
54 lines (43 loc) · 1.28 KB
/
history.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
class History:
def __init__(self):
self._history = []
self._idx = 0
def append(self, cmdline):
self._history.append(cmdline)
def reset(self):
self._idx = 0
def up(self):
self._idx = max(self._idx - 1, -len(self._history))
if self._idx:
return self._history[self._idx]
return ""
def down(self):
self._idx = min(self._idx + 1, 0)
if self._idx:
return self._history[self._idx]
return ""
@property
def history(self):
return self._history
@history.setter
def history(self, value):
self._history = value
class CmdHistory(History):
def __init__(self):
super().__init__()
hidden_prefix = "." if os.name != "nt" else "_"
path = os.path.expanduser(f"~/{hidden_prefix}aaa_history")
if os.path.isfile(path) and os.access(path, os.R_OK):
with open(path) as f:
self._history = f.read().splitlines()
try:
self._file = open(path, "a")
except IOError:
self._file = None
def append(self, cmdline):
super().append(cmdline)
print(cmdline, file=self._file)
def __del__(self):
if self._file:
self._file.close()