Save JS "Console..." history across sessions
Showing the changes to interface/src/ui/JSConsole.cpp (1 of 2 files). Show all
Unified · Split
| @@ -24,13 +24,14 @@ · 23 unmodified lines | |||
| 24 | #include "ScriptHighlighting.h" | 24 | #include "ScriptHighlighting.h" |
| 25 | 25 | ||
| 26 | const int NO_CURRENT_HISTORY_COMMAND = -1; | 26 | const int NO_CURRENT_HISTORY_COMMAND = -1; |
| 27 | const int MAX_HISTORY_SIZE = 64; | 27 | const int MAX_HISTORY_SIZE = 256; |
| 28 | const QString HISTORY_FILENAME = "JSConsole.history.json"; | ||
| 28 | 29 | ||
| 29 | const QString COMMAND_STYLE = "color: #266a9b;"; | 30 | const QString COMMAND_STYLE = "color: #266a9b;"; |
| 30 | 31 | ||
| 31 | const QString RESULT_SUCCESS_STYLE = "color: #677373;"; | 32 | const QString RESULT_SUCCESS_STYLE = "color: #677373;"; |
| 32 | const QString RESULT_INFO_STYLE = "color: #223bd1;"; | 33 | const QString RESULT_INFO_STYLE = "color: #223bd1;"; |
| 33 | const QString RESULT_WARNING_STYLE = "color: #d13b22;"; | 34 | const QString RESULT_WARNING_STYLE = "color: #999922;"; |
| 34 | const QString RESULT_ERROR_STYLE = "color: #d13b22;"; | 35 | const QString RESULT_ERROR_STYLE = "color: #d13b22;"; |
| 35 | 36 | ||
| 36 | const QString GUTTER_PREVIOUS_COMMAND = "<span style=\"color: #57b8bb;\"><</span>"; | 37 | const QString GUTTER_PREVIOUS_COMMAND = "<span style=\"color: #57b8bb;\"><</span>"; |
| @@ -38,14 +39,35 @@ const QString GUTTER_ERROR = "<span style=\"color: #d13b22;\">X</span>"; · 1 unmodified line | |||
| 38 | 39 | ||
| 39 | const QString JSConsole::_consoleFileName { "about:console" }; | 40 | const QString JSConsole::_consoleFileName { "about:console" }; |
| 40 | 41 | ||
| 42 | const QString JSON_KEY = "entries"; | ||
| 43 | QList<QString> _readLines(const QString& filename) { | ||
| 44 | QFile file(filename); | ||
| 45 | file.open(QFile::ReadOnly); | ||
| 46 | auto json = QTextStream(&file).readAll().toUtf8(); | ||
| 47 | auto root = QJsonDocument::fromJson(json).object(); | ||
| 48 | // TODO: check root["version"] | ||
| 49 | return root[JSON_KEY].toVariant().toStringList(); | ||
| 50 | } | ||
| 51 | |||
| 52 | void _writeLines(const QString& filename, const QList<QString>& lines) { | ||
| 53 | QFile file(filename); | ||
| 54 | file.open(QFile::WriteOnly); | ||
| 55 | auto root = QJsonObject(); | ||
| 56 | root["version"] = 1.0; | ||
| 57 | root["last-modified"] = QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate); | ||
| 58 | root[JSON_KEY] = QJsonArray::fromStringList(lines); | ||
| 59 | auto json = QJsonDocument(root).toJson(); | ||
| 60 | QTextStream(&file) << json; | ||
| 61 | } | ||
| 62 | |||
| 41 | JSConsole::JSConsole(QWidget* parent, ScriptEngine* scriptEngine) : | 63 | JSConsole::JSConsole(QWidget* parent, ScriptEngine* scriptEngine) : |
| 42 | QWidget(parent), | 64 | QWidget(parent), |
| 43 | _ui(new Ui::Console), | 65 | _ui(new Ui::Console), |
| 44 | _currentCommandInHistory(NO_CURRENT_HISTORY_COMMAND), | 66 | _currentCommandInHistory(NO_CURRENT_HISTORY_COMMAND), |
| 45 | _commandHistory(), | 67 | _savedHistoryFilename(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + HISTORY_FILENAME), |
| 68 | _commandHistory(_readLines(_savedHistoryFilename)), | ||
| 46 | _ownScriptEngine(scriptEngine == NULL), | 69 | _ownScriptEngine(scriptEngine == NULL), |
| 47 | _scriptEngine(NULL) { | 70 | _scriptEngine(NULL) { |
| 48 | |||
| 49 | _ui->setupUi(this); | 71 | _ui->setupUi(this); |
| 50 | _ui->promptTextEdit->setLineWrapMode(QTextEdit::NoWrap); | 72 | _ui->promptTextEdit->setLineWrapMode(QTextEdit::NoWrap); |
| 51 | _ui->promptTextEdit->setWordWrapMode(QTextOption::NoWrap); | 73 | _ui->promptTextEdit->setWordWrapMode(QTextOption::NoWrap); |
| @@ -101,9 +123,12 @@ void JSConsole::setScriptEngine(ScriptEngine* scriptEngine) { · 49 unmodified lines | |||
| 101 | } | 123 | } |
| 102 | 124 | ||
| 103 | void JSConsole::executeCommand(const QString& command) { | 125 | void JSConsole::executeCommand(const QString& command) { |
| 104 | _commandHistory.prepend(command); | 126 | if (_commandHistory.isEmpty() || _commandHistory.constFirst() != command) { |
| 105 | if (_commandHistory.length() > MAX_HISTORY_SIZE) { | 127 | _commandHistory.prepend(command); |
| 106 | _commandHistory.removeLast(); | 128 | if (_commandHistory.length() > MAX_HISTORY_SIZE) { |
| 129 | _commandHistory.removeLast(); | ||
| 130 | } | ||
| 131 | _writeLines(_savedHistoryFilename, _commandHistory); | ||
| 107 | } | 132 | } |
| 108 | 133 | ||
| 109 | _ui->promptTextEdit->setDisabled(true); | 134 | _ui->promptTextEdit->setDisabled(true); |
| @@ -182,7 +207,7 @@ bool JSConsole::eventFilter(QObject* sender, QEvent* event) { · 72 unmodified lines | |||
| 182 | // a new QTextBlock isn't created. | 207 | // a new QTextBlock isn't created. |
| 183 | keyEvent->setModifiers(keyEvent->modifiers() & ~Qt::ShiftModifier); | 208 | keyEvent->setModifiers(keyEvent->modifiers() & ~Qt::ShiftModifier); |
| 184 | } else { | 209 | } else { |
| 185 | QString command = _ui->promptTextEdit->toPlainText().trimmed(); | 210 | QString command = _ui->promptTextEdit->toPlainText().replace("\r\n","\n").trimmed(); |
| 186 | 211 | ||
| 187 | if (!command.isEmpty()) { | 212 | if (!command.isEmpty()) { |
| 188 | QTextCursor cursor = _ui->promptTextEdit->textCursor(); | 213 | QTextCursor cursor = _ui->promptTextEdit->textCursor(); |