[code] The console history can be colored in grey.

Change-Id: If3da306140d4f80d1abd014c5bf0e1989e3a252d
This commit is contained in:
Léa Saviot
2017-11-30 15:15:24 +01:00
parent 1de30cbe06
commit ed2a17ad31
5 changed files with 51 additions and 16 deletions

View File

@@ -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
}

View File

@@ -3,7 +3,6 @@
#include <kandinsky/point.h>
#include <kandinsky/coordinate.h>
#include <apps/i18n.h>
#include <escher/palette.h>
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());
}

View File

@@ -4,6 +4,7 @@
#include <escher/highlight_cell.h>
#include <escher/message_text_view.h>
#include <escher/responder.h>
#include <escher/palette.h>
#include <escher/scrollable_view.h>
#include <escher/scroll_view_data_source.h>
#include <assert.h>
@@ -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;

View File

@@ -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<ConsoleLine::Type>(marker-1);
}

View File

@@ -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;