comparison src/tools/frontends/misc.py @ 673:903c4749de15

moved sat.tools.frontend to sat.tools.frontends
author Goffi <goffi@goffi.org>
date Fri, 08 Nov 2013 16:35:51 +0100
parents src/tools/frontend/misc.py@4e691a231763
children f610864eb7a5
comparison
equal deleted inserted replaced
672:712e3782af12 673:903c4749de15
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # SAT helpers methods for plugins
5 # Copyright (C) 2013 Adrien Cossa (souliane@mailoo.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 class InputHistory(object):
22
23 def _updateInputHistory(self, text=None, step=None, callback=None, mode=""):
24 """Update the lists of previously sent messages. Several lists can be
25 handled as they are stored in a dictionary, the argument "mode" being
26 used as the entry key. There's also a temporary list to allow you play
27 with previous entries before sending a new message. Parameters values
28 can be combined: text is None and step is None to initialize a main
29 list and the temporary one, step is None to update a list and
30 reinitialize the temporary one, step is not None to update
31 the temporary list between two messages.
32 @param text: text to be saved.
33 @param step: step to move the temporary index.
34 @param callback: method to display temporary entries.
35 @param mode: the dictionary key for main lists.
36 """
37 if not hasattr(self, "input_histories"):
38 self.input_histories = {}
39 history = self.input_histories.setdefault(mode, [])
40 if step is None and text is not None:
41 # update the main list
42 if text in history:
43 history.remove(text)
44 history.append(text)
45 length = len(history)
46 if step is None or length == 0:
47 # prepare the temporary list and index
48 self.input_history_tmp = history[:]
49 self.input_history_tmp.append("")
50 self.input_history_index = length
51 if step is None:
52 return
53 # update the temporary list
54 if text is not None:
55 # save the current entry
56 self.input_history_tmp[self.input_history_index] = text
57 # move to another entry if possible
58 index_tmp = self.input_history_index + step
59 if index_tmp >= 0 and index_tmp < len(self.input_history_tmp):
60 if callback is not None:
61 callback(self.input_history_tmp[index_tmp])
62 self.input_history_index = index_tmp