Commit e1bf7f85 authored by Fabián Orccón's avatar Fabián Orccón Committed by Alexandru Băluț

plugins: Add history to the Developer Console

parent ada96215
......@@ -20,11 +20,56 @@
import code
import sys
from gi.repository import GObject
from gi.repository import Gtk
from utils import FakeOut
from utils import swap_std
class ConsoleHistory(GObject.Object):
"""Represents a console commands history."""
__gsignals__ = {
"pos-changed": (GObject.SIGNAL_RUN_FIRST, None, ()),
}
def __init__(self):
GObject.Object.__init__(self)
self._pos = 0
self._history = [""]
def add(self, cmd):
"""Adds a command line to the history."""
if not cmd.strip():
return
if len(self._history) > 1 and cmd == self._history[-2]:
return
self._history[-1] = cmd
self._history.append("")
self._pos = len(self._history) - 1
def get(self):
"""Gets the command line at the current position."""
return self._history[self._pos]
# pylint: disable=invalid-name
def up(self, cmd):
"""Sets the current command line with the previous used command line."""
if self._pos > 0:
self._history[self._pos] = cmd
self._pos -= 1
self.emit("pos-changed")
def down(self, cmd):
"""Sets the current command line with the next available used command line."""
if self._pos < len(self._history) - 1:
self._history[self._pos] = cmd
self._pos += 1
self.emit("pos-changed")
class ConsoleBuffer(Gtk.TextBuffer):
def __init__(self, namespace):
......@@ -37,9 +82,14 @@ class ConsoleBuffer(Gtk.TextBuffer):
self._stderr = FakeOut(self)
self._console = code.InteractiveConsole(namespace)
self.history = ConsoleHistory()
namespace["__history__"] = self.history
self.history.connect("pos-changed", self.__history_pos_changed_cb)
def process_command_line(self):
"""Process the current input command line executing it if complete."""
cmd = self.get_command_line()
self.history.add(cmd)
with swap_std(self._stdout, self._stderr):
self.write("\n")
......@@ -81,3 +131,7 @@ class ConsoleBuffer(Gtk.TextBuffer):
end_iter = self.get_end_iter()
self.delete(after_prompt_iter, end_iter)
self.write(cmd)
def __history_pos_changed_cb(self, history):
cmd = history.get()
self.set_command_line(cmd)
......@@ -67,8 +67,10 @@ class ConsoleWidget(Gtk.ScrolledWindow):
return True
if event.keyval in (Gdk.KEY_KP_Down, Gdk.KEY_Down):
buf.history.down(buf.get_command_line())
return True
if event.keyval in (Gdk.KEY_KP_Up, Gdk.KEY_Up):
buf.history.up(buf.get_command_line())
return True
if event.keyval in (Gdk.KEY_KP_Left, Gdk.KEY_Left, Gdk.KEY_BackSpace):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment