diff --git a/apps/code/console_line.h b/apps/code/console_line.h index ad0855f24..ee857c64b 100644 --- a/apps/code/console_line.h +++ b/apps/code/console_line.h @@ -8,13 +8,18 @@ namespace Code { class ConsoleLine { public: enum class Type { - Command = 0, - Result = 1 + CurrentSessionCommand = 0, + CurrentSessionResult = 1, + PreviousSessionCommand = 2, + PreviousSessionResult = 3 }; - ConsoleLine(Type type = Type::Command, const char * text = nullptr) : + ConsoleLine(Type type = Type::CurrentSessionCommand, const char * text = nullptr) : m_type(type), m_text(text) {} Type type() const { return m_type; } const char * text() const { return m_text; } + bool isFromCurrentSession() const { return m_type == Type::CurrentSessionCommand || m_type == Type::CurrentSessionResult; } + bool isCommand() const { return m_type == Type::CurrentSessionCommand || m_type == Type::PreviousSessionCommand; } + bool isResult() const { return m_type == Type::CurrentSessionResult || m_type == Type::PreviousSessionResult; } static inline size_t sizeOfConsoleLine(size_t textLength) { return 1 + textLength + 1; // Marker, text, null termination } diff --git a/apps/code/console_line_cell.cpp b/apps/code/console_line_cell.cpp index 89e3dc56f..c5f9e60fe 100644 --- a/apps/code/console_line_cell.cpp +++ b/apps/code/console_line_cell.cpp @@ -3,7 +3,6 @@ #include #include #include -#include namespace Code { @@ -19,7 +18,7 @@ void ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::setLine(Consol void ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::drawRect(KDContext * ctx, KDRect rect) const { ctx->fillRect(bounds(), KDColorWhite); - ctx->drawString(m_line->text(), KDPointZero, ConsoleController::k_fontSize, KDColorBlack, isHighlighted()? Palette::Select : KDColorWhite); + ctx->drawString(m_line->text(), KDPointZero, ConsoleController::k_fontSize, textColor(m_line), isHighlighted()? Palette::Select : KDColorWhite); } KDSize ConsoleLineCell::ScrollableConsoleLineView::ConsoleLineView::minimalSizeForOptimalDisplay() const { @@ -48,6 +47,7 @@ ConsoleLineCell::ConsoleLineCell(Responder * parentResponder) : void ConsoleLineCell::setLine(ConsoleLine line) { m_line = line; m_scrollableView.consoleLineView()->setLine(&m_line); + m_promptView.setTextColor(textColor(&m_line)); reloadCell(); } @@ -63,32 +63,32 @@ void ConsoleLineCell::reloadCell() { } int ConsoleLineCell::numberOfSubviews() const { - if (m_line.type() == ConsoleLine::Type::Command) { + if (m_line.isCommand()) { return 2; } - assert(m_line.type() == ConsoleLine::Type::Result); + assert(m_line.isResult()); return 1; } View * ConsoleLineCell::subviewAtIndex(int index) { - if (m_line.type() == ConsoleLine::Type::Command) { + if (m_line.isCommand()) { assert(index >= 0 && index < 2); View * views[] = {&m_promptView, &m_scrollableView}; return views[index]; } - assert(m_line.type() == ConsoleLine::Type::Result); + assert(m_line.isResult()); assert(index == 0); return &m_scrollableView; } void ConsoleLineCell::layoutSubviews() { - if (m_line.type() == ConsoleLine::Type::Command) { + if (m_line.isCommand()) { KDSize promptSize = KDText::stringSize(I18n::translate(I18n::Message::ConsolePrompt), ConsoleController::k_fontSize); m_promptView.setFrame(KDRect(KDPointZero, promptSize.width(), bounds().height())); m_scrollableView.setFrame(KDRect(KDPoint(promptSize.width(), 0), bounds().width() - promptSize.width(), bounds().height())); return; } - assert(m_line.type() == ConsoleLine::Type::Result); + assert(m_line.isResult()); m_promptView.setFrame(KDRectZero); m_scrollableView.setFrame(bounds()); } diff --git a/apps/code/console_line_cell.h b/apps/code/console_line_cell.h index 8b777951f..7348062a9 100644 --- a/apps/code/console_line_cell.h +++ b/apps/code/console_line_cell.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,9 @@ private: private: ConsoleLineView m_consoleLineView; }; + static KDColor textColor(ConsoleLine * line) { + return line->isFromCurrentSession() ? KDColorBlack : Palette::GreyDark; + } MessageTextView m_promptView; ScrollableConsoleLineView m_scrollableView; ConsoleLine m_line; diff --git a/apps/code/console_store.cpp b/apps/code/console_store.cpp index 476c845b0..625c9a30f 100644 --- a/apps/code/console_store.cpp +++ b/apps/code/console_store.cpp @@ -14,6 +14,23 @@ void ConsoleStore::clear() { m_history[0] = 0; } +void ConsoleStore::startNewSession() { + if (k_historySize < 1) { + return; + } + + m_history[0] = makePrevious(m_history[0]); + + for (int i = 0; i < k_historySize - 1; i++) { + if (m_history[i] == 0) { + if (m_history[i+1] == 0) { + return ; + } + m_history[i+1] = makePrevious(m_history[i+1]); + } + } +} + ConsoleLine ConsoleStore::lineAtIndex(int i) const { assert(i >= 0 && i < numberOfLines()); int currentLineIndex = 0; @@ -48,11 +65,11 @@ int ConsoleStore::numberOfLines() const { } void ConsoleStore::pushCommand(const char * text, size_t length) { - push(CommandMarker, text, length); + push(CurrentSessionCommandMarker, text, length); } void ConsoleStore::pushResult(const char * text, size_t length) { - push(ResultMarker, text, length); + push(CurrentSessionResultMarker, text, length); } void ConsoleStore::deleteLastLineIfEmpty() { @@ -80,7 +97,7 @@ void ConsoleStore::push(const char marker, const char * text, size_t length) { } ConsoleLine::Type ConsoleStore::lineTypeForMarker(char marker) const { - assert(marker == 0x01 || marker == 0x02); + assert(marker == CurrentSessionCommandMarker || marker == CurrentSessionResultMarker || marker == PreviousSessionCommandMarker || marker == PreviousSessionResultMarker); return static_cast(marker-1); } diff --git a/apps/code/console_store.h b/apps/code/console_store.h index 756e23c68..a60764a90 100644 --- a/apps/code/console_store.h +++ b/apps/code/console_store.h @@ -10,15 +10,24 @@ class ConsoleStore { public: ConsoleStore(); void clear(); + void startNewSession(); ConsoleLine lineAtIndex(int i) const; int numberOfLines() const; void pushCommand(const char * text, size_t length); void pushResult(const char * text, size_t length); void deleteLastLineIfEmpty(); private: - static constexpr char CommandMarker = 0x01; - static constexpr char ResultMarker = 0x02; + static constexpr char CurrentSessionCommandMarker = 0x01; + static constexpr char CurrentSessionResultMarker = 0x02; + static constexpr char PreviousSessionCommandMarker = 0x03; + static constexpr char PreviousSessionResultMarker = 0x04; static constexpr int k_historySize = 1024; + static char makePrevious(char marker) { + if (marker == CurrentSessionCommandMarker || marker == CurrentSessionResultMarker) { + return marker + 0x02; + } + return marker; + } void push(const char marker, const char * text, size_t length); ConsoleLine::Type lineTypeForMarker(char marker) const; int indexOfNullMarker() const;