window.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ExtendedKeys_t {} ExtendedKeys; // Enable KEY_* keys as unique items
  20. class Window {
  21. private:
  22. friend class AttributeScope;
  23. WINDOW *self_;
  24. public:
  25. template <typename... Flags>
  26. Window(Flags const &...flags) : Window(initscr(), flags...) {}
  27. template <typename... Flags>
  28. Window(Bounds const &term, Flags const &...flags)
  29. : Window(newwin(term.height, term.width, 0, 0), flags...) {}
  30. ~Window();
  31. void move(Position const &pos);
  32. void move(Offset const &off);
  33. Position getpos();
  34. Bounds getsize();
  35. int getch();
  36. int delch();
  37. void clear_line();
  38. void clear();
  39. Status printf(char const *fmt, ...) GCC_PRINTFLIKE(2, 3);
  40. template <typename... Attrs> AttributeScope with(Attrs const &...attrs) {
  41. return {{to_attr(attrs...)}, *this};
  42. }
  43. private:
  44. template <typename... Flags>
  45. Window(WINDOW *win, Flags const &...flags) : self_(win) {
  46. clear();
  47. [[maybe_unused]] int _[] = {(init_with(flags), 0)...};
  48. }
  49. void init_with(NoEcho_t);
  50. void init_with(WithColor_t);
  51. void init_with(ExtendedKeys_t);
  52. NCURSES_ATTR_T to_attr(Bold_t) const;
  53. NCURSES_ATTR_T to_attr(ColorPair const &color) const;
  54. };
  55. template <typename T>
  56. Window &operator<<(Window &os, T const &value) {
  57. std::stringstream ss;
  58. ss << value;
  59. os.printf("%s", ss.str().c_str());
  60. return os;
  61. }
  62. inline Window &operator<<(Window &os, char const *str) {
  63. os.printf("%s", str);
  64. return os;
  65. }
  66. }