Skip to content

Commit

Permalink
Add Python console option to save history across sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
yorikvanhavre authored and wwmayer committed Sep 7, 2019
1 parent 17ce36b commit 164409c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
69 changes: 68 additions & 1 deletion src/Gui/PythonConsole.cpp
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions src/Gui/PythonConsole.h
Expand Up @@ -79,13 +79,13 @@ class GuiExport ConsoleHistory
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;
};

Expand Down Expand Up @@ -150,6 +150,8 @@ private Q_SLOTS:
void insertPythonError (const QString&);
void runSourceFromMimeData(const QString&);
void appendOutput(const QString&, int);
void loadHistory() const;
void saveHistory() const;

Q_SIGNALS:
void pendingSource( void );
Expand All @@ -162,7 +164,8 @@ private Q_SLOTS:

private:
PythonConsoleHighlighter* pythonSyntax;
QString *_sourceDrain;
QString *_sourceDrain;
QString _historyFile;
};

/**
Expand Down

0 comments on commit 164409c

Please sign in to comment.