Show More
@@ -19,6 +19,7 class HistoryConsoleWidget(ConsoleWidget): | |||
|
19 | 19 | |
|
20 | 20 | # HistoryConsoleWidget protected variables. |
|
21 | 21 | self._history = [] |
|
22 | self._history_edits = {} | |
|
22 | 23 | self._history_index = 0 |
|
23 | 24 | self._history_prefix = '' |
|
24 | 25 | |
@@ -42,6 +43,9 class HistoryConsoleWidget(ConsoleWidget): | |||
|
42 | 43 | if history and (not self._history or self._history[-1] != history): |
|
43 | 44 | self._history.append(history) |
|
44 | 45 | |
|
46 | # Emulate readline: reset all history edits. | |
|
47 | self._history_edits = {} | |
|
48 | ||
|
45 | 49 | # Move the history index to the most recent item. |
|
46 | 50 | self._history_index = len(self._history) |
|
47 | 51 | |
@@ -113,7 +117,7 class HistoryConsoleWidget(ConsoleWidget): | |||
|
113 | 117 | #--------------------------------------------------------------------------- |
|
114 | 118 | |
|
115 | 119 | def history_previous(self, prefix=''): |
|
116 |
""" If possible, set the input buffer to a previous |
|
|
120 | """ If possible, set the input buffer to a previous history item. | |
|
117 | 121 | |
|
118 | 122 | Parameters: |
|
119 | 123 | ----------- |
@@ -123,40 +127,42 class HistoryConsoleWidget(ConsoleWidget): | |||
|
123 | 127 | index = self._history_index |
|
124 | 128 | while index > 0: |
|
125 | 129 | index -= 1 |
|
126 |
history = self._history |
|
|
130 | history = self._get_edited_history(index) | |
|
127 | 131 | if history.startswith(prefix): |
|
128 | 132 | break |
|
129 | 133 | else: |
|
130 | 134 | history = None |
|
131 | 135 | |
|
132 | 136 | if history is not None: |
|
137 | self._set_edited_input_buffer(history) | |
|
133 | 138 | self._history_index = index |
|
134 | self.input_buffer = history | |
|
135 | 139 | |
|
136 | 140 | def history_next(self, prefix=''): |
|
137 |
""" |
|
|
138 | original search prefix if there is no such item. | |
|
141 | """ If possible, set the input buffer to a subsequent history item. | |
|
139 | 142 | |
|
140 | 143 | Parameters: |
|
141 | 144 | ----------- |
|
142 | 145 | prefix : str, optional |
|
143 | 146 | If specified, search for an item with this prefix. |
|
144 | 147 | """ |
|
145 |
|
|
|
146 |
|
|
|
147 | history = self._history[self._history_index] | |
|
148 | index = self._history_index | |
|
149 | while self._history_index < len(self._history): | |
|
150 | index += 1 | |
|
151 | history = self._get_edited_history(index) | |
|
148 | 152 | if history.startswith(prefix): |
|
149 | 153 | break |
|
150 | 154 | else: |
|
151 | self._history_index = len(self._history) | |
|
152 | history = prefix | |
|
153 | self.input_buffer = history | |
|
155 | history = None | |
|
156 | ||
|
157 | if history is not None: | |
|
158 | self._set_edited_input_buffer(history) | |
|
159 | self._history_index = index | |
|
154 | 160 | |
|
155 | 161 | def history_tail(self, n=10): |
|
156 | 162 | """ Get the local history list. |
|
157 | 163 | |
|
158 | Parameters | |
|
159 | ---------- | |
|
164 | Parameters: | |
|
165 | ----------- | |
|
160 | 166 | n : int |
|
161 | 167 | The (maximum) number of history items to get. |
|
162 | 168 | """ |
@@ -166,8 +172,23 class HistoryConsoleWidget(ConsoleWidget): | |||
|
166 | 172 | # 'HistoryConsoleWidget' protected interface |
|
167 | 173 | #--------------------------------------------------------------------------- |
|
168 | 174 | |
|
175 | def _get_edited_history(self, index): | |
|
176 | """ Retrieves a history item, possibly with temporary edits. | |
|
177 | """ | |
|
178 | if index in self._history_edits: | |
|
179 | return self._history_edits[index] | |
|
180 | return self._history[index] | |
|
181 | ||
|
182 | def _set_edited_input_buffer(self, source): | |
|
183 | """ Sets the input buffer to 'source', saving the current input buffer | |
|
184 | as a temporary history edit. | |
|
185 | """ | |
|
186 | self._history_edits[self._history_index] = self.input_buffer | |
|
187 | self.input_buffer = source | |
|
188 | ||
|
169 | 189 | def _set_history(self, history): |
|
170 | 190 | """ Replace the current history with a sequence of history items. |
|
171 | 191 | """ |
|
172 | 192 | self._history = list(history) |
|
193 | self._history_edits = {} | |
|
173 | 194 | self._history_index = len(self._history) |
General Comments 0
You need to be logged in to leave comments.
Login now