0

I have a function for scrolling text left to right in an NCurses interface, but it depends upon a timer every 500 ms or so to properly write out a new character in front and delete the previous character behind. Here is the code:

void GekkoFyre::TuiHangouts::gui_scrollText(WINDOW *display, const char *msg,
                                            const int &msgPosY)
{
    size_t maxX = getmaxx(display);
    size_t msgLen = strlen(msg);
    short origPosY = 0;
    short origPosX = 0;
    getyx(display, origPosY, origPosX); // Obtain the original cursor position

    if (msgLen > maxX) {
        // size_t diff = (msgLen - maxX);
        size_t i = 0;
        for (i = maxX; i < msgLen; ++i) {
            // Scroll the message from left to right, then vice versa, so that it fits within the
            // designated window.
            move(msgPosY, 0);
            clrtoeol(); // Delete just the given line, <http://stackoverflow.com/questions/5072881/how-to-clear-a-specific-line-with-ncurses>
            move(origPosY, origPosX); // Go back to the original cursor position
            std::string tmp((msgLen - i), msg[(msgLen - i)]);
            wattron(display, A_REVERSE); // Highlight selection
            mvwaddstr(display, msgPosY, 0, tmp.c_str());
            std::this_thread::sleep_for(std::chrono::milliseconds(500));
        }
    }
}

I honestly have no idea if this works because I've been unable to get it to work. But is there a timer just like, std::this_thread::sleep_for(std::chrono::milliseconds(500));, that you can use within a function but is asynchronous? That would be beautiful! I've done some research and come across std::async and std::thread, but they depend upon being external to the function.

Any help would be greatly appreciated, thank you.

Phobos D'thorga
  • 439
  • 5
  • 17
  • It probably won't work anyway, since it uses threads (see [FAQ](http://invisible-island.net/ncurses/ncurses.faq.html#multithread)). – Thomas Dickey Feb 21 '16 at 23:07

1 Answers1

1

But is there a timer just like, std::this_thread::sleep_for(std::chrono::milliseconds(500));, that you can use within a function but is asynchronous?

No, you can't. What did you grasp actually from the term asynchronous behavior. You'll need to have a joining point elsewhere in your code to capture the timer elapsed event.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • What do you mean by a _joining point_? Apologies, but I really struggle with the nomenclature of programming. I've never studied formally but am entirely self-taught, doing my best to learn along the way and it's really frustrating to not know a lot of the nomenclature which others 'just get'. – Phobos D'thorga Feb 21 '16 at 18:19
  • @Phobos Well a _joining point_ for the timer elapsed merely means you need to have a callback capturing it outside of your function. – πάντα ῥεῖ Feb 21 '16 at 18:21