##// END OF EJS Templates
Reading a line is now correctly implemented in ConsoleWidget.
epatters -
Show More
@@ -141,6 +141,7 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
141 141 self._prompt = ''
142 142 self._prompt_pos = 0
143 143 self._reading = False
144 self._reading_callback = None
144 145
145 146 # Set a monospaced font.
146 147 self.reset_font()
@@ -191,6 +192,11 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
191 192
192 193 self._context_menu.exec_(event.globalPos())
193 194
195 def dragMoveEvent(self, event):
196 """ Reimplemented to disable dropping text.
197 """
198 event.ignore()
199
194 200 def keyPressEvent(self, event):
195 201 """ Reimplemented to create a console-like interface.
196 202 """
@@ -256,12 +262,15 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
256 262
257 263 else:
258 264 if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
259 intercepted = True
260 265 if self._reading:
261 intercepted = False
266 self.appendPlainText('\n')
262 267 self._reading = False
268 if self._reading_callback:
269 self._reading_callback()
270 self._reading_callback = None
263 271 elif not self._executing:
264 272 self.execute(interactive=True)
273 intercepted = True
265 274
266 275 elif key == QtCore.Qt.Key_Up:
267 276 if self._reading or not self._up_pressed():
@@ -304,7 +313,8 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
304 313
305 314 # Line deletion (remove continuation prompt)
306 315 len_prompt = len(self._continuation_prompt)
307 if cursor.columnNumber() == len_prompt and \
316 if not self._reading and \
317 cursor.columnNumber() == len_prompt and \
308 318 position != self._prompt_pos:
309 319 cursor.setPosition(position - len_prompt,
310 320 QtGui.QTextCursor.KeepAnchor)
@@ -678,18 +688,44 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
678 688 self.setReadOnly(True)
679 689 self._prompt_finished_hook()
680 690
681 def _readline(self, prompt=''):
682 """ Read and return one line of input from the user. The trailing
683 newline is stripped.
691 def _readline(self, prompt='', callback=None):
692 """ Reads one line of input from the user.
693
694 Parameters
695 ----------
696 prompt : str, optional
697 The prompt to print before reading the line.
698
699 callback : callable, optional
700 A callback to execute with the read line. If not specified, input is
701 read *synchronously* and this method does not return until it has
702 been read.
703
704 Returns
705 -------
706 If a callback is specified, returns nothing. Otherwise, returns the
707 input string with the trailing newline stripped.
684 708 """
685 if not self.isVisible():
686 raise RuntimeError('Cannot read a line if widget is not visible!')
709 if self._reading:
710 raise RuntimeError('Cannot read a line. Widget is already reading.')
711
712 if not callback and not self.isVisible():
713 # If the user cannot see the widget, this function cannot return.
714 raise RuntimeError('Cannot synchronously read a line if the widget'
715 'is not visible!')
687 716
688 717 self._reading = True
689 718 self._show_prompt(prompt)
690 while self._reading:
691 QtCore.QCoreApplication.processEvents()
692 return self.input_buffer.rstrip('\n\r')
719
720 if callback is None:
721 self._reading_callback = None
722 while self._reading:
723 QtCore.QCoreApplication.processEvents()
724 return self.input_buffer.rstrip('\n')
725
726 else:
727 self._reading_callback = lambda: \
728 callback(self.input_buffer.rstrip('\n'))
693 729
694 730 def _set_position(self, position):
695 731 """ Convenience method to set the position of the cursor.
@@ -710,10 +746,11 b' class ConsoleWidget(QtGui.QPlainTextEdit):'
710 746 # Use QTextDocumentFragment intermediate object because it strips
711 747 # out the Unicode line break characters that Qt insists on inserting.
712 748 cursor = self._get_end_cursor()
713 cursor.movePosition(QtGui.QTextCursor.Left,
714 QtGui.QTextCursor.KeepAnchor)
715 if str(cursor.selection().toPlainText()) not in '\r\n':
716 self.appendPlainText('\n')
749 if cursor.position() > 0:
750 cursor.movePosition(QtGui.QTextCursor.Left,
751 QtGui.QTextCursor.KeepAnchor)
752 if str(cursor.selection().toPlainText()) != '\n':
753 self.appendPlainText('\n')
717 754
718 755 if prompt is not None:
719 756 self._prompt = prompt
@@ -293,7 +293,10 b' class FrontendWidget(HistoryConsoleWidget):'
293 293 self._call_tip()
294 294
295 295 def _handle_req(self):
296 print self._readline()
296 def callback(line):
297 print repr(line)
298 self._show_prompt()
299 self._readline(callback=callback)
297 300
298 301 def _handle_sub(self, omsg):
299 302 if self._hidden:
@@ -345,5 +348,3 b' class FrontendWidget(HistoryConsoleWidget):'
345 348 doc = rep['content']['docstring']
346 349 if doc:
347 350 self._call_tip_widget.show_docstring(doc)
348
349
General Comments 0
You need to be logged in to leave comments. Login now