| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //
- // window.h
- // ncurses-wrapper
- //
- // Created by Sam Jaffe on 7/23/24.
- //
- #pragma once
- #include <sstream>
- #include <string>
- #include <ncurses-wrapper/attribute_scope.h>
- #include <ncurses-wrapper/bounds.h>
- #include <ncurses-wrapper/forward.h>
- #undef getch
- #undef delch
- namespace curses {
- constexpr struct Bold_t {} Bold;
- constexpr struct NoEcho_t {} NoEcho; // Don't automatically print input characters
- constexpr struct WithColor_t {} WithColor;
- constexpr struct Scrollable_t {} Scrollable; // Enable scrolling
- constexpr struct ExtendedKeys_t {} ExtendedKeys; // Enable KEY_* keys as unique items
- class Window {
- private:
- friend class AttributeScope;
- WINDOW *self_;
-
- public:
- template <typename... Flags>
- Window(Flags const &...flags) : Window(initscr(), flags...) {}
-
- template <typename... Flags>
- Window(Bounds const &term, Flags const &...flags)
- : Window(newwin(term.height, term.width, 0, 0), flags...) {}
-
- ~Window();
-
- void move(Position const &pos);
- void move(Offset const &off);
- Position getpos();
- Bounds getsize();
- int getch();
- int delch();
- void clear_line();
- void clear();
-
- Status printf(char const *fmt, ...) GCC_PRINTFLIKE(2, 3);
- template <typename... Attrs> AttributeScope with(Attrs const &...attrs) {
- return {{to_attr(attrs...)}, *this};
- }
-
- private:
- template <typename... Flags>
- Window(WINDOW *win, Flags const &...flags) : self_(win) {
- clear();
- [[maybe_unused]] int _[] = {(init_with(flags), 0)...};
- }
- void init_with(NoEcho_t);
- void init_with(WithColor_t);
- void init_with(Scrollable_t);
- void init_with(ExtendedKeys_t);
-
- NCURSES_ATTR_T to_attr(Bold_t) const;
- NCURSES_ATTR_T to_attr(ColorPair const &color) const;
- };
- template <typename T>
- Window &operator<<(Window &os, T const &value) {
- std::stringstream ss;
- ss << value;
- os.printf("%s", ss.str().c_str());
- return os;
- }
- inline Window &operator<<(Window &os, char const *str) {
- os.printf("%s", str);
- return os;
- }
- }
|