From 06321bd308dad1dc08ad86a59df9b2172b50faa1 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 29 Aug 2015 22:04:32 +0200 Subject: [PATCH] Expose complete history to QML The use case for this functionality is to be able to e.g. display the terminal application's output after it was terminated. This requires exposing a `clearScreen` method to QML as the history only contains the lines not currently displayed in the widget. If we want to access the whole output we have to be able to flush the screen beforehand. The single argument overload of `Emulation::writeToStream` simplifies reading the complete history at once without having to further expose it's length. --- lib/Emulation.cpp | 5 +++++ lib/Emulation.h | 12 +++++++++++- src/ksession.cpp | 18 ++++++++++++++++++ src/ksession.h | 5 +++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/Emulation.cpp b/lib/Emulation.cpp index 8cc32ef..edfd7b2 100644 --- a/lib/Emulation.cpp +++ b/lib/Emulation.cpp @@ -296,6 +296,11 @@ void Emulation::writeToStream( TerminalCharacterDecoder* _decoder , _currentScreen->writeLinesToStream(_decoder,startLine,endLine); } +void Emulation::writeToStream( TerminalCharacterDecoder* _decoder) +{ + _currentScreen->writeLinesToStream(_decoder, 0, _currentScreen->getHistLines()); +} + int Emulation::lineCount() const { // sum number of lines currently on _screen plus number of lines in history diff --git a/lib/Emulation.h b/lib/Emulation.h index feecdf0..5c1383e 100644 --- a/lib/Emulation.h +++ b/lib/Emulation.h @@ -171,7 +171,17 @@ public: * @param endLine Index of last line to copy */ virtual void writeToStream(TerminalCharacterDecoder* decoder,int startLine,int endLine); - + + /** + * Copies the complete output history into @p stream, using @p decoder to convert + * the terminal characters into text. + * + * @param decoder A decoder which converts lines of terminal characters with + * appearance attributes into output text. PlainTextDecoder is the most commonly + * used decoder. + */ + virtual void writeToStream(TerminalCharacterDecoder* decoder); + /** Returns the codec used to decode incoming characters. See setCodec() */ const QTextCodec* codec() const { return _codec; } /** Sets the codec used to decode incoming characters. */ diff --git a/src/ksession.cpp b/src/ksession.cpp index d77b585..77b4763 100644 --- a/src/ksession.cpp +++ b/src/ksession.cpp @@ -209,6 +209,19 @@ int KSession::historySize() const } } +QString KSession::getHistory() const +{ + QString history; + QTextStream historyStream(&history); + PlainTextDecoder historyDecoder; + + historyDecoder.begin(&historyStream); + m_session->emulation()->writeToStream(&historyDecoder); + historyDecoder.end(); + + return history; +} + void KSession::sendText(QString text) { m_session->sendText(text); @@ -227,6 +240,11 @@ void KSession::sendKey(int rep, int key, int mod) const // } } +void KSession::clearScreen() +{ + m_session->emulation()->clearEntireScreen(); +} + void KSession::search(const QString ®exp, int startLine, int startColumn, bool forwards) { HistorySearch *history = new HistorySearch( QPointer(m_session->emulation()), QRegExp(regexp), forwards, startColumn, startLine, this); diff --git a/src/ksession.h b/src/ksession.h index d1cf794..86fc27c 100644 --- a/src/ksession.h +++ b/src/ksession.h @@ -39,6 +39,7 @@ class KSession : public QObject Q_PROPERTY(QString title READ getTitle WRITE setTitle NOTIFY titleChanged) Q_PROPERTY(QString shellProgram WRITE setShellProgram) Q_PROPERTY(QStringList shellProgramArgs WRITE setArgs) + Q_PROPERTY(QString history READ getHistory) public: KSession(QObject *parent = 0); @@ -68,6 +69,8 @@ public: void setHistorySize(int lines); //infinite if lines < 0 int historySize() const; + QString getHistory() const; + // Sets whether flow control is enabled void setFlowControlEnabled(bool enabled); @@ -132,6 +135,8 @@ public slots: // Send some text to terminal void sendKey(int rep, int key, int mod) const; + void clearScreen(); + // Search history void search(const QString ®exp, int startLine = 0, int startColumn = 0, bool forwards = true );