Project

Profile

Help

Revision 947e7664

Added by Vincent Le Goff over 4 years ago

Add a basic history

View differences:

src/ui/window.py
142 142
        self.engine = engine
143 143
        self.client = None
144 144
        self.world = world
145
        self.index = -1
146
        self.history = []
145 147
        sizer = wx.BoxSizer(wx.VERTICAL)
146 148
        self.SetSizer(sizer)
147 149

  
......
191 193
        msg = event.GetString().encode(encoding, "replace")
192 194
        self.client.write(msg + "\r\n")
193 195

  
196
        # Write in the history
197
        if event.GetEventObject() == self.input:
198
            if self.index == -1 and msg:
199
                self.history.append(msg)
200

  
194 201
    def OnFocus(self, evt):
195 202
        """The GUIClient requires a change of focus.
196 203

  
197 204
        This event is triggered when the GUIClient asks a change of
198 205
        focus in the input field (hiding the password field) or in
199 206
        the password field (hiding the input field).
200

  
201 207
        """
202 208
        val = evt.GetValue()
203 209
        if val == "input":
......
211 217

  
212 218
    def OnKeyDown(self, e):
213 219
        """A key is pressed in the window."""
220
        skip = True
214 221
        modifiers = e.GetModifiers()
215 222
        key = e.GetUnicodeKey()
216 223
        if not key:
217 224
            key = e.GetKeyCode()
218 225

  
226
        if e.GetEventObject() == self.input:
227
            skip = self.HandleHistory(modifiers, key)
228

  
219 229
        # Look for matching macros
220 230
        for code, macro in self.engine.macros.items():
221 231
            if code == (key, modifiers):
......
227 237
            e.SetEventObject(self.input)
228 238
            #wx.PostEvent(self.input, e)
229 239
            e.Skip()
230
        else:
240
        elif skip:
231 241
            e.Skip()
242

  
243
    def HandleHistory(self, modifiers, key):
244
        """Handle the history commands."""
245
        if modifiers == 0:
246
            if key == wx.WXK_UP:
247
                self.HistoryGoUp()
248
                return False
249
            elif key == wx.WXK_DOWN:
250
                self.HistoryGoDown()
251
                return False
252

  
253
        return True
254

  
255
    def HistoryGoUp(self):
256
        """Go up in the history."""
257
        if self.index == -1:
258
            self.index = len(self.history) - 1
259
        elif self.index > 0:
260
            self.index -= 1
261
        else:
262
            return
263

  
264
        message = self.history[self.index]
265
        encoding = self.engine.settings["options.general.encoding"]
266
        message = message.decode(encoding, "replace")
267
        self.input.Clear()
268
        self.input.SetValue(message)
269
        self.input.SetInsertionPoint(len(message) + 1)
270

  
271
    def HistoryGoDown(self):
272
        """Go down in the history."""
273
        if self.index == -1:
274
            return
275
        elif self.index >= len(self.history) - 1:
276
            self.index = -1
277
            message = ""
278
        else:
279
            self.index += 1
280
            message = self.history[self.index]
281

  
282
        encoding = self.engine.settings["options.general.encoding"]
283
        message = message.decode(encoding, "replace")
284
        self.input.Clear()
285
        self.input.SetValue(message)
286
        self.input.SetInsertionPoint(len(message) + 1)

Also available in: Unified diff