Add Python console option to save history across sessions
This commit is contained in:
@@ -92,7 +92,7 @@ struct PythonConsoleP
|
||||
InteractiveInterpreter* interpreter;
|
||||
CallTipsList* callTipsList;
|
||||
ConsoleHistory history;
|
||||
QString output, error, info;
|
||||
QString output, error, info, historyFile;
|
||||
QStringList statements;
|
||||
bool interactive;
|
||||
QMap<QString, QColor> colormap; // Color map
|
||||
@@ -106,6 +106,7 @@ struct PythonConsoleP
|
||||
interpreter = 0;
|
||||
callTipsList = 0;
|
||||
interactive = false;
|
||||
historyFile = QString::fromUtf8((App::Application::getUserAppDataDir() + "PythonHistory.log").c_str());
|
||||
colormap[QLatin1String("Text")] = Qt::black;
|
||||
colormap[QLatin1String("Bookmark")] = Qt::cyan;
|
||||
colormap[QLatin1String("Breakpoint")] = Qt::red;
|
||||
@@ -472,11 +473,13 @@ PythonConsole::PythonConsole(QWidget *parent)
|
||||
.arg(QString::fromLatin1(version), QString::fromLatin1(platform));
|
||||
d->output = d->info;
|
||||
printPrompt(PythonConsole::Complete);
|
||||
loadHistory();
|
||||
}
|
||||
|
||||
/** Destroys the object and frees any allocated resources */
|
||||
PythonConsole::~PythonConsole()
|
||||
{
|
||||
saveHistory();
|
||||
Base::PyGILStateLocker lock;
|
||||
getWindowParameter()->Detach( this );
|
||||
delete pythonSyntax;
|
||||
@@ -1273,6 +1276,16 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e )
|
||||
this->setWordWrapMode(QTextOption::NoWrap);
|
||||
}
|
||||
|
||||
QAction* saveh = menu.addAction(tr("Save history"));
|
||||
saveh->setToolTip(tr("Saves python history across FreeCAD sessions"));
|
||||
saveh->setCheckable(true);
|
||||
|
||||
if (hGrp->GetBool("SavePythonHistory", false)) {
|
||||
saveh->setChecked(true);
|
||||
} else {
|
||||
saveh->setChecked(false);
|
||||
}
|
||||
|
||||
QAction* exec = menu.exec(e->globalPos());
|
||||
if (exec == wrap) {
|
||||
if (wrap->isChecked()) {
|
||||
@@ -1282,7 +1295,14 @@ void PythonConsole::contextMenuEvent ( QContextMenuEvent * e )
|
||||
this->setWordWrapMode(QTextOption::NoWrap);
|
||||
hGrp->SetBool("PythonWordWrap", false);
|
||||
}
|
||||
} else if (exec == saveh) {
|
||||
if (saveh->isChecked()) {
|
||||
hGrp->SetBool("SavePythonHistory", true);
|
||||
} else {
|
||||
hGrp->SetBool("SavePythonHistory", false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void PythonConsole::onClearConsole()
|
||||
@@ -1363,6 +1383,53 @@ QString PythonConsole::readline( void )
|
||||
return inputBuffer.append(QChar::fromLatin1('\n')); //< pass a newline here, since the readline-caller may need it!
|
||||
}
|
||||
|
||||
/**
|
||||
* loads history contents from the default history file
|
||||
*/
|
||||
void PythonConsole::loadHistory() const
|
||||
{
|
||||
// only load contents if history is empty, to not overwrite anything
|
||||
if (!d->history.isEmpty())
|
||||
return;
|
||||
ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter().
|
||||
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General");
|
||||
if (!hGrp->GetBool("SavePythonHistory", false))
|
||||
return;
|
||||
QFile f(d->historyFile);
|
||||
if (f.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
QString l;
|
||||
while (!f.atEnd()) {
|
||||
l = QString::fromUtf8(f.readLine());
|
||||
if (!l.isEmpty()) {
|
||||
l.chop(1); // removes the last \n
|
||||
d->history.append(l);
|
||||
}
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* saves the current history to the default history file
|
||||
*/
|
||||
void PythonConsole::saveHistory() const
|
||||
{
|
||||
if (d->history.isEmpty())
|
||||
return;
|
||||
ParameterGrp::handle hGrp = App::GetApplication().GetUserParameter().
|
||||
GetGroup("BaseApp")->GetGroup("Preferences")->GetGroup("General");
|
||||
if (!hGrp->GetBool("SavePythonHistory", false))
|
||||
return;
|
||||
QFile f(d->historyFile);
|
||||
if (f.open(QIODevice::WriteOnly)) {
|
||||
QTextStream t (&f);
|
||||
const QStringList& hist = d->history.values();
|
||||
for (QStringList::ConstIterator it = hist.begin(); it != hist.end(); ++it)
|
||||
t << *it << "\n";
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
PythonConsoleHighlighter::PythonConsoleHighlighter(QObject* parent)
|
||||
|
||||
@@ -79,13 +79,13 @@ public:
|
||||
void append(const QString &inputLine);
|
||||
const QStringList& values() const;
|
||||
void restart();
|
||||
void markScratch( void );
|
||||
void doScratch( void );
|
||||
void markScratch( void );
|
||||
void doScratch( void );
|
||||
|
||||
private:
|
||||
QStringList _history;
|
||||
QStringList::ConstIterator _it;
|
||||
int _scratchBegin;
|
||||
int _scratchBegin;
|
||||
QString _prefix;
|
||||
};
|
||||
|
||||
@@ -150,6 +150,8 @@ private:
|
||||
void insertPythonError (const QString&);
|
||||
void runSourceFromMimeData(const QString&);
|
||||
void appendOutput(const QString&, int);
|
||||
void loadHistory() const;
|
||||
void saveHistory() const;
|
||||
|
||||
Q_SIGNALS:
|
||||
void pendingSource( void );
|
||||
@@ -162,7 +164,8 @@ private:
|
||||
|
||||
private:
|
||||
PythonConsoleHighlighter* pythonSyntax;
|
||||
QString *_sourceDrain;
|
||||
QString *_sourceDrain;
|
||||
QString _historyFile;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user