cli.h 783 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // cli.hpp
  3. // ncurses-wrapper
  4. //
  5. // Created by Sam Jaffe on 7/23/24.
  6. //
  7. #pragma once
  8. #include <string>
  9. #include <vector>
  10. #include <ncurses-wrapper/forward.h>
  11. #include <ncurses-wrapper/window.h>
  12. namespace curses {
  13. class Cli {
  14. private:
  15. Window window_;
  16. std::string prompt_;
  17. std::vector<std::string> stack_{""};
  18. int horizontal_offset_{0};
  19. int vertical_offset_{0};
  20. public:
  21. template <typename... Flags>
  22. explicit Cli(std::string const &prompt, Flags const &...flags)
  23. : window_(flags..., NoEcho, Scrollable, ExtendedKeys), prompt_(prompt) {}
  24. void loop(std::function<void(curses::Window &, std::string)> on_enter);
  25. private:
  26. size_t index() const { return stack_.size() - vertical_offset_ - 1; }
  27. std::string &get() { return stack_.at(index()); }
  28. };
  29. }