90 lines
2.2 KiB
C++
90 lines
2.2 KiB
C++
//compile with "g++ -pthread threadncurses.cpp -o main -lncurses"
|
|
//the same as thread.cpp but it uses ncurses instead. This proves that ncurses can work even if its in a seperate thread
|
|
//this is a very functional example of ncurses and asynchronous threading working in the same program
|
|
#include <iostream>
|
|
#include <thread>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <ncurses.h>
|
|
using namespace std;
|
|
|
|
#ifdef _WIN32
|
|
//this part has never been tested and is unlikely to work
|
|
#include <windows.h>;
|
|
void fnsleep(unsigned milliseconds)
|
|
{
|
|
Sleep(milliseconds);
|
|
}
|
|
|
|
#else
|
|
#include <unistd.h>
|
|
void fnsleep(unsigned milliseconds)
|
|
{
|
|
usleep(milliseconds * 1000);
|
|
}
|
|
#endif
|
|
|
|
void helloWorld(int y)
|
|
{
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
//store the cursor positions here
|
|
int y, x;
|
|
int yy, xx;
|
|
int xBefore;
|
|
int yBefore;
|
|
start_color();
|
|
init_pair(1, COLOR_RED, COLOR_BLUE);
|
|
if (i != 0)
|
|
{
|
|
//print the hello world lines
|
|
getyx(stdscr, yBefore, xBefore);
|
|
move(2, 0);
|
|
clrtoeol();
|
|
mvprintw(2, 0, "Hello world #");
|
|
int icantthinkofanameforthis = i + 1;
|
|
printw(to_string(icantthinkofanameforthis).c_str());//an annoying but reliable way to get this to work
|
|
printw("\n");
|
|
move(yBefore, xBefore); //move the cursor back to where is was before to allow for a fluid typing experience (i.e. its possible to mess this part up if you do it wrong)
|
|
}
|
|
else
|
|
{
|
|
attron(COLOR_PAIR(1));
|
|
attron(A_BOLD);
|
|
getyx(stdscr, yy, xx);
|
|
printw("bold red text\n");
|
|
attroff(COLOR_PAIR(1));
|
|
printw("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
|
|
attroff(A_BOLD);
|
|
printw("Hello world #");
|
|
int icantthinkofanameforthis = i + 1;
|
|
printw(to_string(icantthinkofanameforthis).c_str());//an annoying but reliable way to get this to work
|
|
printw("\n");
|
|
move(3, 25); //the default text-entry position
|
|
}
|
|
|
|
refresh();
|
|
fnsleep(1000);
|
|
}
|
|
}
|
|
|
|
int main()
|
|
{
|
|
initscr();
|
|
use_default_colors();
|
|
move(3, 0);
|
|
printw("Please enter your name: \n");
|
|
move(0, 0);
|
|
|
|
thread thread_object(helloWorld, 1);
|
|
thread_object.detach();
|
|
|
|
char *name = new char[99];
|
|
getstr(name);
|
|
printw(name);
|
|
printw(" is a [redacted for academic purposes]\n");
|
|
printw("Press any key to continue");
|
|
getch();
|
|
endwin();
|
|
return 0;
|
|
} |