plug-ins: support saving the history of commands.

Since I use the Python console a lot myself to test GIMP API, the
ability to recall previously ran commands is extremely useful. Let's
just add support for this as an AUX argument. This was already done in
the Script-fu console, now in the Python console too!
This commit is contained in:
Jehan
2024-09-20 17:40:21 +02:00
parent 85a686b8c5
commit 94c7ca6809
2 changed files with 21 additions and 9 deletions

View File

@ -90,9 +90,13 @@ class _ReadLine(object):
pass
class History(object):
def __init__(self):
def __init__(self, initial_history):
object.__init__(self)
self.items = ['']
if initial_history is None or len(initial_history) == 0:
initial_history = ['']
elif initial_history[0] != '':
initial_history = [''] + initial_history
self.items = initial_history
self.ptr = 0
self.edited = {}
@ -122,7 +126,7 @@ class _ReadLine(object):
except KeyError:
return self.items[self.ptr]
def __init__(self, quit_func=None):
def __init__(self, quit_func=None, initial_history=None):
object.__init__(self)
self.quit_func = quit_func
@ -161,7 +165,7 @@ class _ReadLine(object):
self.in_modal_raw_input = False
self.run_on_raw_input = None
self.tab_pressed = 0
self.history = _ReadLine.History()
self.history = _ReadLine.History(initial_history)
self.nonword_re = re.compile(r"[^\w\._]")
def freeze_undo(self):
@ -540,8 +544,9 @@ class _ReadLine(object):
class _Console(_ReadLine, code.InteractiveInterpreter):
def __init__(self, locals=None, banner=None,
completer=None, use_rlcompleter=True,
start_script=None, quit_func=None):
_ReadLine.__init__(self, quit_func)
start_script=None, quit_func=None,
initial_history=None):
_ReadLine.__init__(self, quit_func, initial_history)
code.InteractiveInterpreter.__init__(self, locals)
self.locals["__console__"] = self

View File

@ -61,12 +61,13 @@ def run(procedure, config, data):
'Pango': gi.repository.Pango }
class GimpConsole(pyconsole.Console):
def __init__(self, quit_func=None):
def __init__(self, quit_func=None, initial_history=None):
banner = ('GIMP %s Python Console\nPython %s\n' %
(Gimp.version(), sys.version))
pyconsole.Console.__init__(self,
locals=namespace, banner=banner,
quit_func=quit_func)
quit_func=quit_func,
initial_history=initial_history)
def _commit(self):
pyconsole.Console._commit(self)
Gimp.displays_flush()
@ -90,7 +91,8 @@ def run(procedure, config, data):
RESPONSE_CLEAR,
Gtk.ResponseType.OK ])
self.cons = GimpConsole(quit_func=lambda: Gtk.main_quit())
history = config.get_property('history')
self.cons = GimpConsole(quit_func=lambda: Gtk.main_quit(), initial_history=history)
self.style_set (None, None)
@ -136,6 +138,8 @@ def run(procedure, config, data):
elif response_id == Gtk.ResponseType.OK:
self.save_dialog()
else:
# Store up to 100 commands.
config.set_property('history', self.cons.history.items[-100:])
Gtk.main_quit()
self.cons.grab_focus()
@ -317,6 +321,9 @@ class PythonConsole (Gimp.PlugIn):
_("The run mode"), Gimp.RunMode,
Gimp.RunMode.INTERACTIVE,
GObject.ParamFlags.READWRITE)
procedure.add_string_array_aux_argument ("history",
"Command history", "Command history",
GObject.ParamFlags.READWRITE)
procedure.add_menu_path ("<Image>/Filters/Development/Python-Fu")
return procedure