77
88 python3 embed_qtconsole.py
99"""
10+ import json
1011import os
1112import site
1213
14+ from pathlib import Path
1315from typing import List
1416
1517from PyQt5 import QtCore , QtGui , QtWidgets
1921
2022site .addsitedir (os .path .join (os .path .dirname (__file__ ), "lib" ))
2123
24+ from pyqtconsole .commandhistory import CommandHistory # noqa: E402
2225from 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
2558class 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