tv_rename/progress.cpp

126 lines
2.5 KiB
C++
Raw Permalink Normal View History

2019-06-04 19:54:00 +00:00
#include "progress.hpp"
2020-01-17 13:05:30 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
#include <iostream>
2020-01-17 13:05:30 +00:00
#endif
2019-06-04 19:54:00 +00:00
#ifdef _WIN32
2020-04-01 11:46:28 +00:00
#include <conio.h>
2020-04-01 14:13:39 +00:00
#include <windows.h>
2019-06-04 19:54:00 +00:00
#include <winuser.h>
#define cout std::wcout
#else // UNIX
2020-01-17 13:05:30 +00:00
#ifdef GUI
2020-04-01 14:07:37 +00:00
#include "gtk/progresswindow.hpp"
2020-01-17 13:05:30 +00:00
#endif // GUI
2019-06-04 19:54:00 +00:00
#include <sys/ioctl.h>
#define cout std::cout
#define TEXT( a ) a
#endif // UNIX
2020-01-17 13:05:30 +00:00
#ifndef GUI // Don't need terminal specific functions in GUI
#ifdef _WIN32
int getTermWidth() {
2019-06-04 19:54:00 +00:00
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi );
return csbi.srWindow.Right - csbi.srWindow.Left + 1;
2020-01-15 16:45:29 +00:00
}
2020-01-17 13:05:30 +00:00
#else // UNIX
int getTermWidth() {
2019-06-04 19:54:00 +00:00
struct winsize w;
ioctl( 0, TIOCGWINSZ, &w );
return w.ws_col;
}
2020-01-17 13:05:30 +00:00
#endif // UNIX
2019-06-04 19:54:00 +00:00
2020-01-17 13:05:30 +00:00
#ifdef _WIN32
2019-06-04 19:54:00 +00:00
void home( size_t /*UNUSED*/ ) {
static HANDLE h = NULL;
while ( !h )
h = GetStdHandle( STD_OUTPUT_HANDLE );
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo( h, &info );
COORD c = { 0, info.dwCursorPosition.Y - 1 };
SetConsoleCursorPosition( h, c );
}
2020-01-17 13:05:30 +00:00
#else // UNIX
2019-06-04 19:54:00 +00:00
void home( size_t width ) {
cout << "\x1b[" << width << "D";
}
2020-01-17 13:05:30 +00:00
#endif // UNIX
#endif // not GUI
2019-06-04 19:54:00 +00:00
int getNum( int width, int perc ) {
2019-06-04 19:54:00 +00:00
return ( width * perc ) / 100;
}
2020-01-17 13:05:30 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
void ProgressBar::print( int perc ) {
auto width = getTermWidth();
home( width );
auto count = getNum( width - 7, perc );
if ( count < 0 )
count = 0;
2019-06-04 19:54:00 +00:00
cout << "[" << string( count, TEXT( '=' ) ).c_str();
if ( perc != 100 ) {
int wc = width - 8 - count;
if ( wc < 0 )
wc = 0;
cout << ">" << string( wc, TEXT( ' ' ) ).c_str();
}
cout << "] ";
if ( perc / 10 == 0 )
cout << " ";
else if ( perc / 100 == 0 )
cout << " ";
cout << perc << "%" << std::flush;
}
2020-01-17 13:05:30 +00:00
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
2020-03-12 19:52:23 +00:00
SendMessage( ( HWND )ptr, WM_APP, PROGRESS_PERC, perc );
2020-01-17 13:05:30 +00:00
#endif
}
void ProgressBar::print( const string &t ) {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->setLabel( t );
#else
2020-03-12 19:52:23 +00:00
SendMessage( ( HWND )ptr, WM_APP, PROGRESS_STRING, ( LPARAM )t.c_str() );
2020-01-17 13:05:30 +00:00
#endif
}
ProgressBar::~ProgressBar() {
#ifndef _WIN32
static_cast< ProgressWindow * >( ptr )->hide();
2020-01-17 13:05:30 +00:00
#else
2020-03-12 19:52:23 +00:00
SendMessage( ( HWND )ptr, WM_CLOSE, 0, 0 );
2020-01-17 13:05:30 +00:00
#endif
}
#endif // GUI