jython-console.diff

Cesar Martinez Izquierdo, 09/06/2016 05:03 PM

Download (5.53 KB)

View differences:

new/org.gvsig.scripting/org.gvsig.scripting.app/org.gvsig.scripting.app.mainplugin/src/main/resources-plugin/scripting/lib/console/console.py 2016-09-06 16:21:49.628916688 +0200
41 41

  
42 42
        self.frame = frame # TODO do I need a reference to frame after the constructor?
43 43
        self.history = History(self)
44
        self.bs = 0 # what is this?
44
        self.promptPosition = 0
45 45

  
46 46
        # command buffer
47 47
        self.buffer = []
......
59 59
        keyBindings = [
60 60
            (KeyEvent.VK_ENTER, 0, "jython.enter", self.enter),
61 61
            (KeyEvent.VK_DELETE, 0, "jython.delete", self.delete),
62
            (KeyEvent.VK_BACK_SPACE, 0, "jython.backspace", self.backspace),
62 63
            (KeyEvent.VK_HOME, 0, "jython.home", self.home),
63 64
            (KeyEvent.VK_UP, 0, "jython.up", self.history.historyUp),
64 65
            (KeyEvent.VK_DOWN, 0, "jython.down", self.history.historyDown),
......
183 184
        self.popup.show()
184 185
        self.popup.list.setSelectedIndex(0)
185 186

  
187
    def beyondPrompt(self, considerCurrent=True, considerSelection=False):
188
        """Determines wheter the cursor is in the editable area
189
           (i.e. beyond the last prompt position)"""
190
        caret = self.output.caretPosition
191
        caret0 = caret
192
        if considerCurrent:
193
            caret = caret + 1
194
        if considerSelection:
195
            if self.output.selectedText:
196
                caret = self.output.selectionEnd
197
        if caret > self.promptPosition:
198
            return True
199
        return False
200

  
186 201
    def inLastLine(self, include = 1):
187 202
        """ Determines whether the cursor is in the last line """
188 203
        limits = self.__lastLine()
......
240 255
            self.doc.remove(offset[0], offset[1]-offset[0]-1)
241 256
        self.__addOutput(self.infoColor, text)
242 257

  
258
    def __do_delete(self, event, pos=0):
259
        if self.output.selectedText:
260
            start = max(self.output.selectionStart, self.promptPosition)
261
            self.doc.remove(start, self.output.selectionEnd - start)
262
        else:
263
            self.doc.remove(self.output.caretPosition + pos, 1)
264

  
243 265
    # don't allow prompt to be deleted
244
    # this will cause problems when history can contain multiple lines
245 266
    def delete(self, event):
246
        """ Intercepts delete events only allowing it to work in the last line """
247
        if self.inLastLine():
248
            if self.output.selectedText:
249
                self.doc.remove(self.output.selectionStart, self.output.selectionEnd - self.output.selectionStart)
250
            elif self.output.caretPosition < self.doc.length:
251
                self.doc.remove(self.output.caretPosition, 1)
267
        """ Intercepts backspace events only allowing it to work in the last line """
268
        if self.beyondPrompt(considerCurrent=True, considerSelection=True):
269
            self.__do_delete(event)
270

  
271

  
272
    # don't allow prompt to be deleted
273
    def backspace(self, event):
274
        """ Intercepts backspace events only allowing it to work in the last line """
275
        if self.beyondPrompt(considerCurrent=False, considerSelection=True):
276
            self.__do_delete(event, -1)
252 277

  
253 278
    # why is there a keyTyped and a keyPressed?
254 279
    def keyTyped(self, event):
255
        #print >> sys.stderr, "keyTyped", event.getKeyCode()
256
        if not self.inLastLine():
257
            event.consume()
258
        if self.bs:
259
            event.consume()
260
        self.bs=0
280
        if not self.beyondPrompt():
281
            self.output.setCaretPosition(self.doc.length)
261 282

  
262 283
    def keyPressed(self, event):
284
        # skip Shift + delete and shift + backspace as the are incorrectly managed
285
        if event.keyCode == KeyEvent.VK_BACK_SPACE or event.keyCode == KeyEvent.VK_DELETE:
286
            if event.modifiers > 0:
287
                event.consume()
288

  
263 289
        if self.popup.visible:
264 290
            self.popup.key(event)
265
        #print >> sys.stderr, "keyPressed", event.getKeyCode()
266
        if event.keyCode == KeyEvent.VK_BACK_SPACE:
267
            offsets = self.__lastLine()
268
            if not self.inLastLine(include=0):
269
                self.bs = 1
270
            else:
271
                self.bs = 0
291

  
272 292

  
273 293
    # TODO refactor me
274 294
    def write(self, text):
......
276 296

  
277 297
    def printResult(self, msg):
278 298
        """ Prints the results of an operation """
279
        self.__addOutput(self.output.foreground, "\n" + str(msg))
299
        self.__addOutput(self.output.foreground, msg, True)
280 300

  
281 301
    def printError(self, msg): 
282
        self.__addOutput(self.errorColor, "\n" + str(msg))
302
        self.__addOutput(self.errorColor, msg, True)
283 303

  
284 304
    def printOnProcess(self):
285 305
        """ Prints the process symbol """
286
        self.__addOutput(self.infoColor, "\n" + Console.PROCESS)
306
        self.__addOutput(self.infoColor, Console.PROCESS, True)
287 307

  
288 308
    def printPrompt(self):
289 309
        """ Prints the prompt """
290
        self.__addOutput(self.infoColor, "\n" + Console.PROMPT)
310
        self.__addOutput(self.infoColor, Console.PROMPT, True)
311
        self.promptPosition = self.doc.length
291 312
		
292
    def __addOutput(self, color, msg):
313
    def __addOutput(self, color, msg, new_line=False):
293 314
        """ Adds the output to the text area using a given color """
315
        if new_line:
316
            if isinstance(msg, unicode):
317
                msg =  u"\n" + msg
318
            else:
319
                msg = "\n" + str(msg)
294 320
        from javax.swing.text import BadLocationException
295 321
        style = SimpleAttributeSet()
296 322