window.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. Window &operator<<(Window &os, std::string const &value);
  58. Window &operator<<(Window &os, std::string_view value);
  59. Window &operator<<(Window &os, char const *value);
  60. Window &operator<<(Window &os, int value);
  61. Window &operator<<(Window &os, char value);
  62. }