0

I am trying to create a simple project with an "interactive console" (might be the wrong term) where I can basically have input and output put onto the screen at the same time safely. I have been trying to use the GNU Readline program because I thought it would help me to achieve my goals, but so far I have not had luck with it.

Example program below:

#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <thread>
#include <iostream>
#include <chrono>

int main()
{
    // Configure readline to auto-complete paths when the tab key is hit.
    rl_bind_key('\t', rl_complete);

    std::thread foo([]() {
        for (;;) {
            std::cout << "hello world " << std::endl;
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }
    });

    for(;;) {
        // Display prompt and read input
        char* input = readline("prompt> ");

        // Check for EOF.
        if (!input)
            break;

        // Add input to readline history.
        add_history(input);

        // Do stuff...

        // Free buffer that was allocated by readline
        free(input);
    }
    return 0;
}

My desired output would be

hello world
hello world
hello world
... keeps printing every second
prompt> I have some text sitting here not being disturbed by the above "hello world" output printing out every second

Please note, I do not need to use the readline library, but in this example program I was trying it out.

Is something like this possible in C/C++? I've seen it done in other programming languages such as Java...so I imagine it must be possible in C or C++.

Are there any libraries out there that support this? I have been google searching for 2 hours or so now with no luck in finding something to do what I have described above.

Thanks.

MasterGberry
  • 2,800
  • 6
  • 37
  • 56
  • 4
    If I understand your goals, you should look at the [`ncurses` library](https://www.gnu.org/software/ncurses/) and the [NCURSES Programming HOWTO](http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/) – David C. Rankin Mar 30 '19 at 08:35
  • A (nearly) C++ only approach: [SO: I/O in concurrent program](https://stackoverflow.com/a/48097134/7478597). – Scheff's Cat Mar 30 '19 at 08:51
  • @DavidC.Rankin is it impossible to do this without creating a "window" for the application? – MasterGberry Mar 30 '19 at 10:11

0 Answers0