Commit 47b2dbf0 authored by lm's avatar lm

Added command history

parent af9f2479
...@@ -58,6 +58,7 @@ DebugConsole::DebugConsole(QWidget *parent) : ...@@ -58,6 +58,7 @@ DebugConsole::DebugConsole(QWidget *parent) :
lowpassDataRate(0.0f), lowpassDataRate(0.0f),
dataRateThreshold(500), dataRateThreshold(500),
autoHold(true), autoHold(true),
commandIndex(0),
m_ui(new Ui::DebugConsole) m_ui(new Ui::DebugConsole)
{ {
// Setup basic user interface // Setup basic user interface
...@@ -471,6 +472,12 @@ void DebugConsole::appendSpecialSymbol() ...@@ -471,6 +472,12 @@ void DebugConsole::appendSpecialSymbol()
void DebugConsole::sendBytes() void DebugConsole::sendBytes()
{ {
// Store command history
commandHistory.append(m_ui->sendText->text());
// Since text was just sent, we're at position commandHistory.length()
// which is the current text
commandIndex = commandHistory.length();
if (!m_ui->sentText->isVisible()) if (!m_ui->sentText->isVisible())
{ {
m_ui->sentText->setVisible(true); m_ui->sentText->setVisible(true);
...@@ -678,18 +685,12 @@ void DebugConsole::keyPressEvent(QKeyEvent * event) ...@@ -678,18 +685,12 @@ void DebugConsole::keyPressEvent(QKeyEvent * event)
{ {
if (event->key() == Qt::Key_Up) if (event->key() == Qt::Key_Up)
{ {
qDebug() << "UP KEY PRESSED";
cycleCommandHistory(true); cycleCommandHistory(true);
} }
else if (event->key() == Qt::Key_Down) else if (event->key() == Qt::Key_Down)
{ {
qDebug() << "DOWN KEY PRESSED";
cycleCommandHistory(false); cycleCommandHistory(false);
} }
else if (event->key() == Qt::Key_Enter)
{
this->sendBytes();
}
else else
{ {
QWidget::keyPressEvent(event); QWidget::keyPressEvent(event);
...@@ -698,40 +699,54 @@ void DebugConsole::keyPressEvent(QKeyEvent * event) ...@@ -698,40 +699,54 @@ void DebugConsole::keyPressEvent(QKeyEvent * event)
void DebugConsole::cycleCommandHistory(bool up) void DebugConsole::cycleCommandHistory(bool up)
{ {
// Store current command if we're not in history yet // Only cycle if there is a history
if (commandIndex == commandHistory.length()) if (commandHistory.length() > 0)
{ {
currCommand = m_ui->sendText->text(); // Store current command if we're not in history yet
} if (commandIndex == commandHistory.length() && up)
{
currCommand = m_ui->sendText->text();
}
if (up) if (up)
{
// UP
commandIndex--;
if (commandIndex >= 0)
{ {
m_ui->sendText->setText(commandHistory.at(commandIndex)); // UP
commandIndex--;
if (commandIndex >= 0)
{
m_ui->sendText->setText(commandHistory.at(commandIndex));
}
// If the index
} }
else
{
// DOWN
commandIndex++;
if (commandIndex < commandHistory.length())
{
m_ui->sendText->setText(commandHistory.at(commandIndex));
}
// If the index is at history length, load the last current command
// If the index }
}
else // Restore current command if we went out of history
{ if (commandIndex == commandHistory.length())
// DOWN
commandIndex++;
if (commandIndex < commandHistory.length())
{ {
m_ui->sendText->setText(commandHistory.at(commandIndex)); m_ui->sendText->setText(currCommand);
} }
// If the index is at history length, load the last current command
} // If we are too far down or too far up, wrap around to current command
if (commandIndex < 0 || commandIndex > commandHistory.length())
{
commandIndex = commandHistory.length();
m_ui->sendText->setText(currCommand);
}
// If we are too far down or too far up, wrap around to current command // Bound the index
if (commandIndex < 0 || commandIndex > commandHistory.length()) if (commandIndex < 0) commandIndex = 0;
{ if (commandIndex > commandHistory.length()) commandIndex = commandHistory.length();
commandIndex = commandHistory.length();
m_ui->sendText->setText(currCommand);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment