Skip to content

Commit 15ebd89

Browse files
committed
Remember history from last time.
1 parent c161b25 commit 15ebd89

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

__init__.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
88
python3 embed_qtconsole.py
99
"""
10+
import json
1011
import os
1112
import site
1213

14+
from pathlib import Path
1315
from typing import List
1416

1517
from PyQt5 import QtCore, QtGui, QtWidgets
@@ -19,8 +21,39 @@
1921

2022
site.addsitedir(os.path.join(os.path.dirname(__file__), "lib"))
2123

24+
from pyqtconsole.commandhistory import CommandHistory # noqa: E402
2225
from pyqtconsole.console import PythonConsole # noqa: E402
2326

27+
history_file = Path(__file__).parent.joinpath("history.json")
28+
29+
30+
class PersistentCommandHistory(CommandHistory):
31+
32+
_json: List[str]
33+
34+
def __init__(self, parent):
35+
super().__init__(parent)
36+
37+
self._json = []
38+
if history_file.exists():
39+
with open(history_file, "r") as fp:
40+
self._json = json.load(fp)
41+
for command in self._json:
42+
super().add(command)
43+
44+
def add(self, str_):
45+
super().add(str_)
46+
47+
if str_:
48+
if str_ in self._json:
49+
self._json.remove(str_)
50+
self._json.append(str_)
51+
52+
# Keep only the 100 last lines:
53+
self._json = self._json[-100:]
54+
with open(history_file, "w") as fp:
55+
json.dump(self._json, fp, indent=2)
56+
2457

2558
class InfinitePythonConsole(PythonConsole):
2659
def __init__(self, *args, **kwargs):
@@ -30,6 +63,8 @@ def __init__(self, *args, **kwargs):
3063

3164
super().__init__(*args, **kwargs)
3265

66+
self.command_history = PersistentCommandHistory(self)
67+
3368
styles = self.pbar.highlighter.styles
3469
self.pbar.highlighter.rules = [
3570
# Match the prompt incase of a console
@@ -104,17 +139,13 @@ def author(self) -> str:
104139
return "Holt59"
105140

106141
def description(self) -> str:
107-
return self._tr("Run IPython from MO2")
142+
return self._tr("Run Python from MO2")
108143

109144
def version(self) -> mobase.VersionInfo:
110-
return mobase.VersionInfo(1, 0, 0, mobase.ReleaseType.FINAL)
111-
112-
def isActive(self) -> bool:
113-
return self._organizer.pluginSetting(self.name(), "enabled")
145+
return mobase.VersionInfo(1, 0, 1, mobase.ReleaseType.FINAL)
114146

115147
def settings(self) -> List[mobase.PluginSetting]:
116148
return [
117-
mobase.PluginSetting("enabled", "enable this plugin", True),
118149
mobase.PluginSetting(
119150
"font family", "font family for the terminal", "Courier New"
120151
),

0 commit comments

Comments
 (0)