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.