| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #pragma once
- #include <string>
- #include <vector>
- #include <numeric> // For std::iota
- namespace lumacs {
- /// @brief Manages history for a specific minibuffer type (e.g., commands, file paths).
- class HistoryManager {
- public:
- HistoryManager() : history_index_(0) {}
- /// @brief Adds an item to the history.
- /// Only adds if the item is not empty and not a duplicate of the last item.
- void add_item(const std::string& item) {
- if (!item.empty() && (history_.empty() || history_.back() != item)) {
- history_.push_back(item);
- }
- history_index_ = history_.size(); // Reset index to end after adding
- }
- /// @brief Navigates to the previous item in history.
- /// @return The previous item, or empty string if no more history.
- std::string previous() {
- if (!history_.empty()) {
- if (history_index_ > 0) {
- history_index_--;
- }
- return history_[history_index_];
- }
- return "";
- }
- /// @brief Navigates to the next item in history.
- /// @return The next item, or empty string if at the end of history.
- std::string next() {
- if (!history_.empty()) {
- if (history_index_ < history_.size() - 1) {
- history_index_++;
- return history_[history_index_];
- } else if (history_index_ == history_.size() - 1) {
- // If at the end, and pressing next, clear input (or original input before history nav)
- history_index_++; // Move past the last valid index
- return "";
- }
- }
- return "";
- }
-
- /// @brief Clears the history.
- void clear() {
- history_.clear();
- history_index_ = 0;
- }
- /// @brief Returns true if history is empty, false otherwise.
- bool empty() const {
- return history_.empty();
- }
- private:
- std::vector<std::string> history_;
- size_t history_index_; // Current position in history
- };
- } // namespace lumacs
|