commit 6b72d815c57ef9848e148583b4e2344993d5c680
parent 6be6738facfaec23c3095b8f273aa56e75bd3483
Author: ThomasV <thomasv@gitorious>
Date: Sat, 23 Feb 2013 20:21:46 +0100
show tab completions in console
Diffstat:
M | lib/qt_console.py | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------ |
1 file changed, 68 insertions(+), 12 deletions(-)
diff --git a/lib/qt_console.py b/lib/qt_console.py
@@ -5,9 +5,12 @@ import traceback
from PyQt4 import QtCore
from PyQt4 import QtGui
+
+
class Console(QtGui.QPlainTextEdit):
def __init__(self, prompt='>> ', startup_message='', parent=None):
QtGui.QPlainTextEdit.__init__(self, parent)
+
self.prompt = prompt
self.history = []
self.namespace = {}
@@ -19,6 +22,7 @@ class Console(QtGui.QPlainTextEdit):
self.document().setDefaultFont(QtGui.QFont("monospace", 10, QtGui.QFont.Normal))
self.showMessage(startup_message)
+
def updateNamespace(self, namespace):
self.namespace.update(namespace)
@@ -35,6 +39,10 @@ class Console(QtGui.QPlainTextEdit):
prompt = '.' * len(self.prompt)
else:
prompt = self.prompt
+
+ self.completions_pos = self.textCursor().position()
+ self.completions_visible = False
+
self.appendPlainText(prompt)
self.moveCursor(QtGui.QTextCursor.End)
@@ -56,6 +64,35 @@ class Console(QtGui.QPlainTextEdit):
self.textCursor().insertText(command)
self.moveCursor(QtGui.QTextCursor.End)
+
+ def show_completions(self, completions):
+ if self.completions_visible:
+ self.hide_completions()
+
+ c = self.textCursor()
+ c.setPosition(self.completions_pos)
+ t = '\n' + ' '.join(completions)
+ if len(t) > 500:
+ t = t[:500] + '...'
+ c.insertText(t)
+ self.completions_end = c.position()
+
+ self.moveCursor(QtGui.QTextCursor.End)
+ self.completions_visible = True
+
+
+ def hide_completions(self):
+ if not self.completions_visible:
+ return
+ c = self.textCursor()
+ c.setPosition(self.completions_pos)
+ l = self.completions_end - self.completions_pos
+ for x in range(l): c.deleteChar()
+
+ self.moveCursor(QtGui.QTextCursor.End)
+ self.completions_visible = False
+
+
def getConstruct(self, command):
if self.construct:
prev_command = self.construct[-1]
@@ -147,6 +184,12 @@ class Console(QtGui.QPlainTextEdit):
self.newPrompt()
def keyPressEvent(self, event):
+ if event.key() == QtCore.Qt.Key_Tab:
+ self.completions()
+ return
+
+ self.hide_completions()
+
if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.runCommand()
return
@@ -166,32 +209,45 @@ class Console(QtGui.QPlainTextEdit):
return
elif event.key() == QtCore.Qt.Key_L and event.modifiers() == QtCore.Qt.ControlModifier:
self.clear()
- if event.key() == QtCore.Qt.Key_Tab:
- self.completion()
- return
+
super(Console, self).keyPressEvent(event)
- def completion(self):
+
+ def completions(self):
cmd = self.getCommand()
path = cmd.split('.')
ns = self.namespace.keys()
if len(path) == 1:
ns = ns
+ prefix = ''
else:
obj = self.namespace.get(path[0])
+ prefix = path[0] + '.'
ns = dir(obj)
+
-
- print ns
- prefixes = []
+ completions = []
for x in ns:
- if x.startswith(cmd):
- prefixes.append(x)
-
- if len(prefixes) == 1:
- self.setCommand(prefixes[0])
+ if x[0] == '_':continue
+ xx = prefix + x
+ if xx.startswith(cmd):
+ completions.append(xx)
+
+ if not completions:
+ self.hide_completions()
+ elif len(completions) == 1:
+ self.hide_completions()
+ self.setCommand(completions[0])
+ else:
+ # find common prefix
+ p = os.path.commonprefix(completions)
+ if len(p)>len(self.getCommand()):
+ self.hide_completions()
+ self.setCommand(p)
+ else:
+ self.show_completions(completions)
welcome_message = '''