70 lines
1.2 KiB
C++
70 lines
1.2 KiB
C++
#include <chrono>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
#include <signal.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "../Progress.h"
|
|
#include "../StatusBarManager.h"
|
|
|
|
using namespace std;
|
|
|
|
StatusBarManager* g_manager = nullptr;
|
|
|
|
// This ensures your terminal is left in normal state even after Ctrl+C
|
|
void INT_handler(int sig)
|
|
{
|
|
if (nullptr != g_manager)
|
|
{
|
|
g_manager->stop();
|
|
}
|
|
signal(sig, SIG_DFL);
|
|
raise(sig);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
signal(SIGINT, INT_handler);
|
|
Progress p("Normal", 10);
|
|
Progress p2("Test", 10);
|
|
StatusBarManager manager;
|
|
g_manager = &manager;
|
|
manager.add_progress_message(&p);
|
|
manager.add_progress_message(&p2);
|
|
if (isatty(STDOUT_FILENO))
|
|
{
|
|
manager.start();
|
|
}
|
|
else
|
|
{
|
|
cerr << "NOT A TTY" << endl;
|
|
}
|
|
|
|
string test[2] = {"Message 1", "Message 2"};
|
|
|
|
srand(time(nullptr));
|
|
|
|
manager.draw_status_line();
|
|
this_thread::sleep_for(chrono::seconds(1));
|
|
|
|
for (int i = 0; i < 10; ++i)
|
|
{
|
|
int random = rand() % 2;
|
|
if (random != 2)
|
|
{
|
|
cout << test[random] << endl;
|
|
}
|
|
p.increment_progress();
|
|
p2.increment_progress();
|
|
|
|
manager.draw_status_line();
|
|
this_thread::sleep_for(chrono::seconds(1));
|
|
}
|
|
|
|
manager.stop();
|
|
return 0;
|
|
}
|