forked from ThomasGerstenberg/serial_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_history.py
36 lines (27 loc) · 930 Bytes
/
command_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
class CommandHistory(object):
def __init__(self):
self._entries = [""]
self.index = 0
def add_entry(self, entry):
if not entry:
return
if entry in self._entries:
self._entries.remove(entry)
self._entries.insert(1, entry)
if len(self._entries) > 20:
self._entries.pop()
self.index = 0
def get_entries_with(self, text):
return [e.rstrip("\r") for e in self._entries if text in e]
def has_previous(self):
return self.index > 0
def get_previous(self):
if self.has_previous():
self.index -= 1
return self._entries[self.index]
def has_next(self):
return self.index < (len(self._entries) - 1)
def get_next(self):
if self.has_next():
self.index += 1
return self._entries[self.index]