diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index db2af7fe9..92e0bc8db 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -126,6 +126,14 @@ bool ConsoleController::handleEvent(Ion::Events::Event event) { m_selectableTableView.reloadData(); m_selectableTableView.selectCellAtLocation(0, m_consoleStore.numberOfLines()); return true; + } else if (event == Ion::Events::Backspace) { + int selectedRow = m_selectableTableView.selectedRow(); + assert(selectedRow >= 0 && selectedRow < m_consoleStore.numberOfLines()); + m_selectableTableView.deselectTable(); + int firstDeletedLineIndex = m_consoleStore.deleteCommandAndResultsAtIndex(selectedRow); + m_selectableTableView.reloadData(); + m_selectableTableView.selectCellAtLocation(0, firstDeletedLineIndex); + return true; } return false; } diff --git a/apps/code/console_store.cpp b/apps/code/console_store.cpp index 625c9a30f..fc0cb1a30 100644 --- a/apps/code/console_store.cpp +++ b/apps/code/console_store.cpp @@ -80,6 +80,26 @@ void ConsoleStore::deleteLastLineIfEmpty() { } } +int ConsoleStore::deleteCommandAndResultsAtIndex(int index) { + int numberOfLinesAtStart = numberOfLines(); + assert(index >= 0 && index < numberOfLinesAtStart); + int indexOfLineToDelete = index; + while (indexOfLineToDelete < numberOfLinesAtStart - 1) { + if (lineAtIndex(indexOfLineToDelete + 1).isCommand()) { + break; + } + indexOfLineToDelete++; + } + ConsoleLine lineToDelete = lineAtIndex(indexOfLineToDelete); + while (indexOfLineToDelete > 0 && !lineAtIndex(indexOfLineToDelete).isCommand()) { + deleteLineAtIndex(indexOfLineToDelete); + indexOfLineToDelete--; + lineToDelete = lineAtIndex(indexOfLineToDelete); + } + deleteLineAtIndex(indexOfLineToDelete); + return indexOfLineToDelete; +} + void ConsoleStore::push(const char marker, const char * text, size_t length) { // TODO: Verify that the text field does not accept texts that are bigger than // k_historySize, or put an alert message if the command is too big. @@ -114,6 +134,26 @@ int ConsoleStore::indexOfNullMarker() const { return 0; } +void ConsoleStore::deleteLineAtIndex(int index) { + assert(index >=0 && index < numberOfLines()); + int currentLineIndex = 0; + for (int i = 0; i < k_historySize - 1; i++) { + if (m_history[i] == 0) { + currentLineIndex++; + continue; + } + if (currentLineIndex == index) { + int nextLineStart = i; + while (m_history[nextLineStart] != 0 && nextLineStart < k_historySize - 2) { + nextLineStart++; + } + nextLineStart++; + memcpy(&m_history[i], &m_history[nextLineStart], (k_historySize - 1) - nextLineStart + 1); + return; + } + } +} + void ConsoleStore::deleteFirstLine() { if (m_history[0] == 0) { return; diff --git a/apps/code/console_store.h b/apps/code/console_store.h index a60764a90..245ecbcef 100644 --- a/apps/code/console_store.h +++ b/apps/code/console_store.h @@ -16,6 +16,7 @@ public: void pushCommand(const char * text, size_t length); void pushResult(const char * text, size_t length); void deleteLastLineIfEmpty(); + int deleteCommandAndResultsAtIndex(int index); private: static constexpr char CurrentSessionCommandMarker = 0x01; static constexpr char CurrentSessionResultMarker = 0x02; @@ -31,6 +32,7 @@ private: void push(const char marker, const char * text, size_t length); ConsoleLine::Type lineTypeForMarker(char marker) const; int indexOfNullMarker() const; + void deleteLineAtIndex(int index); void deleteFirstLine(); /* When there is no room left to store a new ConsoleLine, we have to delete * old ConsoleLines. deleteFirstLine() deletes the first ConsoleLine of