// // examples/with-timer.cpp // // A simple example for ncurses-poll in which user input is displayed // on the first line, while the current timestamp on the second, which // is refreshed once per second. // // @author CismonX // #include "ncurses-poll.hpp" #include struct my_session { int chr = 0; int num_chr = 0; boost::asio::deadline_timer timer; my_session(ncurses_poll* ncp) : timer(*ncp->get_io_context()) {} }; using boost::system::error_code; using ncp = ncurses_poll; using callback = std::function; void tick(const error_code&, ncp*); inline void defer(ncp* poll) { poll->session()->timer.expires_from_now(boost::posix_time::seconds(1)); poll->session()->timer.async_wait(boost::bind(tick, boost::asio::placeholders::error, poll)); } void tick(const error_code& ec, ncp* poll) { if (ec) { return; } poll->on_writable([](const error_code& ec, ncp* poll) { if (ec) { return; } mvaddstr(1, 0, std::to_string(std::time(nullptr)).c_str()); refresh(); defer(poll); }); } int main() { boost::asio::io_context io_context; ncp poll(io_context); poll.init(); callback cb = [&cb](const error_code& ec, ncp* poll) { if (!ec) reenter(poll->coro) for (;;) { poll->session()->chr = getch() | A_UNDERLINE; yield poll->on_writable(cb); mvaddch(0, poll->session()->num_chr++, poll->session()->chr); refresh(); yield poll->on_readable(cb); } }; poll.on_writable([&cb](const error_code& ec, ncp* poll) { if (!ec) { curs_set(0); cbreak(); noecho(); refresh(); poll->on_readable(cb); } }); defer(&poll); io_context.run(); }