#pragma once #include #include namespace roro { // Lines already entered, browsed shell-style: up() goes back in time, down() forward, and past the // newest line the draft being typed comes back. class InputHistory { public: explicit InputHistory(size_t capacity) : capacity_(capacity) {} void add(const std::string& line) { browsing_ = -1; if (line.empty() || (!lines_.empty() && lines_.back() == line)) return; lines_.push_back(line); if (lines_.size() > capacity_) lines_.pop_front(); } // `current` is what the editor holds now (kept as the draft when browsing starts). bool up(const std::string& current, std::string& out) { if (lines_.empty() || browsing_ == 0) return false; if (browsing_ < 0) { draft_ = current; browsing_ = static_cast(lines_.size()); } out = lines_[--browsing_]; return true; } bool down(std::string& out) { if (browsing_ < 0) return false; if (++browsing_ >= static_cast(lines_.size())) { browsing_ = -1; out = draft_; } else { out = lines_[browsing_]; } return true; } private: size_t capacity_; std::deque lines_; int browsing_ = -1; // index being shown, -1 when editing the draft std::string draft_; }; } // namespace roro