window.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // window.h
  3. // ncurses-wrapper
  4. //
  5. // Created by Sam Jaffe on 7/23/24.
  6. //
  7. #pragma once
  8. #include <sstream>
  9. #include <string>
  10. #include <ncurses-wrapper/attribute_scope.h>
  11. #include <ncurses-wrapper/bounds.h>
  12. #include <ncurses-wrapper/forward.h>
  13. #undef getch
  14. #undef delch
  15. namespace curses {
  16. constexpr struct Bold_t {} Bold;
  17. constexpr struct NoEcho_t {} NoEcho; // Don't automatically print input characters
  18. constexpr struct WithColor_t {} WithColor;
  19. constexpr struct Scrollable_t {} Scrollable; // Enable scrolling
  20. constexpr struct ExtendedKeys_t {} ExtendedKeys; // Enable KEY_* keys as unique items
  21. class Window {
  22. private:
  23. friend class AttributeScope;
  24. WINDOW *self_;
  25. public:
  26. template <typename... Flags>
  27. Window(Flags const &...flags) : Window(initscr(), flags...) {}
  28. template <typename... Flags>
  29. Window(Bounds const &term, Flags const &...flags)
  30. : Window(newwin(term.height, term.width, 0, 0), flags...) {}
  31. ~Window();
  32. void move(Position const &pos);
  33. void move(Offset const &off);
  34. Position getpos();
  35. Bounds getsize();
  36. int getch();
  37. int delch();
  38. void clear_line();
  39. void clear();
  40. Status printf(char const *fmt, ...) GCC_PRINTFLIKE(2, 3);
  41. template <typename... Attrs> AttributeScope with(Attrs const &...attrs) {
  42. return {{to_attr(attrs...)}, *this};
  43. }
  44. private:
  45. template <typename... Flags>
  46. Window(WINDOW *win, Flags const &...flags) : self_(win) {
  47. clear();
  48. [[maybe_unused]] int _[] = {(init_with(flags), 0)...};
  49. }
  50. void init_with(NoEcho_t);
  51. void init_with(WithColor_t);
  52. void init_with(Scrollable_t);
  53. void init_with(ExtendedKeys_t);
  54. NCURSES_ATTR_T to_attr(Bold_t) const;
  55. NCURSES_ATTR_T to_attr(ColorPair const &color) const;
  56. };
  57. template <typename T>
  58. Window &operator<<(Window &os, T const &value) {
  59. std::stringstream ss;
  60. ss << value;
  61. os.printf("%s", ss.str().c_str());
  62. return os;
  63. }
  64. inline Window &operator<<(Window &os, char const *str) {
  65. os.printf("%s", str);
  66. return os;
  67. }
  68. }