Merge pull request #713 from campagnola/console-scrolling

Console scrolling
This commit is contained in:
Luke Campagnola 2018-06-20 13:13:23 -07:00 committed by GitHub
commit c4a5ffa01c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 42 deletions

View File

@ -9,19 +9,18 @@ class CmdInput(QtGui.QLineEdit):
QtGui.QLineEdit.__init__(self, parent)
self.history = [""]
self.ptr = 0
#self.lastCmd = None
#self.setMultiline(False)
def keyPressEvent(self, ev):
#print "press:", ev.key(), QtCore.Qt.Key_Up, QtCore.Qt.Key_Down, QtCore.Qt.Key_Enter
if ev.key() == QtCore.Qt.Key_Up and self.ptr < len(self.history) - 1:
self.setHistory(self.ptr+1)
ev.accept()
return
elif ev.key() == QtCore.Qt.Key_Down and self.ptr > 0:
self.setHistory(self.ptr-1)
ev.accept()
return
if ev.key() == QtCore.Qt.Key_Up:
if self.ptr < len(self.history) - 1:
self.setHistory(self.ptr+1)
ev.accept()
return
elif ev.key() == QtCore.Qt.Key_Down:
if self.ptr > 0:
self.setHistory(self.ptr-1)
ev.accept()
return
elif ev.key() == QtCore.Qt.Key_Return:
self.execCmd()
else:
@ -32,7 +31,6 @@ class CmdInput(QtGui.QLineEdit):
cmd = asUnicode(self.text())
if len(self.history) == 1 or cmd != self.history[1]:
self.history.insert(1, cmd)
#self.lastCmd = cmd
self.history[0] = ""
self.setHistory(0)
self.sigExecuteCmd.emit(cmd)
@ -40,23 +38,3 @@ class CmdInput(QtGui.QLineEdit):
def setHistory(self, num):
self.ptr = num
self.setText(self.history[self.ptr])
#def setMultiline(self, m):
#height = QtGui.QFontMetrics(self.font()).lineSpacing()
#if m:
#self.setFixedHeight(height*5)
#else:
#self.setFixedHeight(height+15)
#self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
#self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
#def sizeHint(self):
#hint = QtGui.QPlainTextEdit.sizeHint(self)
#height = QtGui.QFontMetrics(self.font()).lineSpacing()
#hint.setHeight(height)
#return hint

View File

@ -101,7 +101,6 @@ class ConsoleWidget(QtGui.QWidget):
pickle.dump(open(self.historyFile, 'wb'), history)
def runCmd(self, cmd):
#cmd = str(self.input.lastCmd)
self.stdout = sys.stdout
self.stderr = sys.stderr
encCmd = re.sub(r'>', '&gt;', re.sub(r'<', '&lt;', cmd))
@ -114,22 +113,20 @@ class ConsoleWidget(QtGui.QWidget):
sys.stdout = self
sys.stderr = self
if self.multiline is not None:
self.write("<br><b>%s</b>\n"%encCmd, html=True)
self.write("<br><b>%s</b>\n"%encCmd, html=True, scrollToBottom=True)
self.execMulti(cmd)
else:
self.write("<br><div style='background-color: #CCF; color: black'><b>%s</b>\n"%encCmd, html=True)
self.write("<br><div style='background-color: #CCF; color: black'><b>%s</b>\n"%encCmd, html=True, scrollToBottom=True)
self.inCmd = True
self.execSingle(cmd)
if not self.inCmd:
self.write("</div>\n", html=True)
self.write("</div>\n", html=True, scrollToBottom=True)
finally:
sys.stdout = self.stdout
sys.stderr = self.stderr
sb = self.output.verticalScrollBar()
sb.setValue(sb.maximum())
sb = self.ui.historyList.verticalScrollBar()
sb.setValue(sb.maximum())
@ -201,11 +198,23 @@ class ConsoleWidget(QtGui.QWidget):
self.displayException()
self.multiline = None
def write(self, strn, html=False):
def write(self, strn, html=False, scrollToBottom='auto'):
"""Write a string into the console.
If scrollToBottom is 'auto', then the console is automatically scrolled
to fit the new text only if it was already at the bottom.
"""
isGuiThread = QtCore.QThread.currentThread() == QtCore.QCoreApplication.instance().thread()
if not isGuiThread:
self.stdout.write(strn)
return
sb = self.output.verticalScrollBar()
scroll = sb.value()
if scrollToBottom == 'auto':
atBottom = scroll == sb.maximum()
scrollToBottom = atBottom
self.output.moveCursor(QtGui.QTextCursor.End)
if html:
self.output.textCursor().insertHtml(strn)
@ -213,10 +222,13 @@ class ConsoleWidget(QtGui.QWidget):
if self.inCmd:
self.inCmd = False
self.output.textCursor().insertHtml("</div><br><div style='font-weight: normal; background-color: #FFF; color: black'>")
#self.stdout.write("</div><br><div style='font-weight: normal; background-color: #FFF;'>")
self.output.insertPlainText(strn)
#self.stdout.write(strn)
if scrollToBottom:
sb.setValue(sb.maximum())
else:
sb.setValue(scroll)
def displayException(self):
"""
Display the current exception and stack.