Progress: add gui progress capability

This commit is contained in:
zvon 2020-01-17 14:05:30 +01:00
parent 875541b67d
commit af1355ca93
2 changed files with 87 additions and 11 deletions

View File

@ -1,6 +1,10 @@
#include "progress.hpp" #include "progress.hpp"
#ifndef GUI
#include <iostream> #include <iostream>
#include <stdio.h>
#endif
#ifdef _WIN32 #ifdef _WIN32
@ -9,35 +13,39 @@
#include <winuser.h> #include <winuser.h>
#define cout std::wcout #define cout std::wcout
using string = std::wstring;
#else // UNIX #else // UNIX
#ifdef GUI
#include "progresswindow.hpp"
#endif // GUI
#include <sys/ioctl.h> #include <sys/ioctl.h>
#define cout std::cout #define cout std::cout
#define TEXT( a ) a #define TEXT( a ) a
using string = std::string;
#endif // UNIX #endif // UNIX
#ifdef WIN32 #ifndef GUI // Don't need terminal specific functions in GUI
#ifdef _WIN32
size_t getTermWidth() { size_t getTermWidth() {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi ); GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi );
return csbi.srWindow.Right - csbi.srWindow.Left + 1; return csbi.srWindow.Right - csbi.srWindow.Left + 1;
} }
#else #else // UNIX
size_t getTermWidth() { size_t getTermWidth() {
struct winsize w; struct winsize w;
ioctl( 0, TIOCGWINSZ, &w ); ioctl( 0, TIOCGWINSZ, &w );
return w.ws_col; return w.ws_col;
} }
#endif #endif // UNIX
#ifdef WIN32 #ifdef _WIN32
void home( size_t /*UNUSED*/ ) { void home( size_t /*UNUSED*/ ) {
static HANDLE h = NULL; static HANDLE h = NULL;
while ( !h ) while ( !h )
@ -47,16 +55,19 @@ void home( size_t /*UNUSED*/ ) {
COORD c = { 0, info.dwCursorPosition.Y - 1 }; COORD c = { 0, info.dwCursorPosition.Y - 1 };
SetConsoleCursorPosition( h, c ); SetConsoleCursorPosition( h, c );
} }
#else #else // UNIX
void home( size_t width ) { void home( size_t width ) {
cout << "\x1b[" << width << "D"; cout << "\x1b[" << width << "D";
} }
#endif #endif // UNIX
#endif // not GUI
size_t getNum( size_t width, int perc ) { size_t getNum( size_t width, int perc ) {
return ( width * perc ) / 100; return ( width * perc ) / 100;
} }
#ifndef GUI
void ProgressBar::print( int perc ) { void ProgressBar::print( int perc ) {
auto width = getTermWidth(); auto width = getTermWidth();
home( width ); home( width );
@ -75,3 +86,38 @@ void ProgressBar::print( int perc ) {
cout << " "; cout << " ";
cout << perc << "%" << std::flush; cout << perc << "%" << std::flush;
} }
void ProgressBar::print( const string &t ) {
cout << t << std::endl;
#ifdef _WIN32
cout << std::endl;
#endif
}
#else // GUI
void ProgressBar::print( int perc ) {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->setPerc( perc );
#else
PostMessage( ptr, WM_APP, 0, perc );
#endif
}
void ProgressBar::print( const string &t ) {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->setLabel( t );
#else
PostMessage( ptr, WM_APP, 1, ( LPARAM )t.c_str() );
#endif
}
ProgressBar::~ProgressBar() {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->~ProgressWindow();
#else
PostMessageW( ptr, WM_APP, 2, 0 );
#endif
}
#endif // GUI

View File

@ -1,8 +1,38 @@
#ifndef PROGRESS_HPP #ifndef PROGRESS_HPP
#define PROGRESS_HPP #define PROGRESS_HPP
namespace ProgressBar { #include <string>
#ifdef _WIN32
#include <Windows.h>
using string = std::wstring;
#else
using string = std::string;
#endif
class ProgressBar {
public:
#ifndef GUI
ProgressBar() = default;
~ProgressBar() = default;
#else
#ifndef _WIN32
ProgressBar( void *pw ) : ptr( pw ){};
#else
ProgressBasr( void *hwnd_in ) {
while ( *hwnd_in == nullptr )
sleep( 50 );
ptr = *hwnd_in;
};
#endif // _WIN32
~ProgressBar();
#endif // GUI
void print( int perc ); void print( int perc );
void print( const string &t );
#ifdef GUI
private:
void *ptr;
#endif
}; };
#endif #endif