From 04cfd3df65017b298257e633a18510aa731d5a2f Mon Sep 17 00:00:00 2001 From: am0ebe <am0ebe@gmx.de> Date: Tue, 9 Jul 2024 14:34:35 +0200 Subject: [PATCH] progressbar width as var + fix getTerminalWidth + warning symbol --- src/cmd/console.cpp | 6 ++++-- src/core.cpp | 7 ++++--- src/iprinter.cpp | 33 ++++++++++++++++++++------------- src/iprinter.h | 6 +++--- src/utils.cpp | 10 +--------- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/cmd/console.cpp b/src/cmd/console.cpp index 951155b..b4ef2d7 100644 --- a/src/cmd/console.cpp +++ b/src/cmd/console.cpp @@ -62,9 +62,11 @@ void Console::error(const int& errCode ) void Console::error(const QString& str, const int& errCode=0 ) { if( str != "" ) - qDebug().noquote() << "💣 " << str; + qDebug().noquote() << "💩 " << str; + // qDebug().noquote() << "💣 " << str; + // qDebug().noquote() << "💥 " << str; + // qDebug().noquote() << "🔥 " << str; error(errCode); - } void Console::warning(const QString& str ) diff --git a/src/core.cpp b/src/core.cpp index 0d496b6..9a738bc 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -33,9 +33,6 @@ void Core::init() if ( f(_sys.Startup(), "init API") ) qApp->exit(VmbErrorApiNotStarted); - print("threads",QThread::idealThreadCount()); - - f(_sys.RegisterCameraListObserver(ICameraListObserverPtr(_camObserver)), "register cam observer"); connect( _camObserver, SIGNAL(cameraChanged(CameraPtr,UpdateTriggerType)), this, SLOT(onCameraChanged(CameraPtr,UpdateTriggerType)) ); //listcams calls updateCams! @@ -56,6 +53,7 @@ void Core::init() } detectCams(); + showInfo(); // listCams(); } @@ -196,6 +194,9 @@ void Core::showInfo() print("ncam",utils::ncam()); print("-------------------------------"); + warning("caution m8"); + print(progressBar(50)); + //listCams //showStats //checkDiskSpace (show progressbar) diff --git a/src/iprinter.cpp b/src/iprinter.cpp index b51d602..6c1585c 100644 --- a/src/iprinter.cpp +++ b/src/iprinter.cpp @@ -6,6 +6,8 @@ #ifdef CONSOLE #include "cmd/console.h" #include <cstdlib> +#include <sys/ioctl.h> +#include <unistd.h> #endif @@ -112,7 +114,7 @@ void IPrinter::print(const char* name, const seconds& val) emit info(QString::fromLocal8Bit(name) + ": " + QString::number(val.count()) + "s"); } -QString IPrinter::progressBar(QTimer* timer) +QString IPrinter::progressBar(QTimer* timer, int width) { int total_ms = timer->interval(); int remaining_ms = timer->remainingTime(); @@ -120,27 +122,32 @@ QString IPrinter::progressBar(QTimer* timer) int progressInPercent = (double) elapsed_ms / total_ms; //print in minutes and secs auto s1 = QString(QString::number(progressInPercent) + "% | " + formatTime(elapsed_ms) + " < " + formatTime(remaining_ms)); - auto s2 = QString(progressBar(progressInPercent)); + auto s2 = QString(progressBar(progressInPercent,width)); return s1 + s2; } -QString IPrinter::progressBar(int percentage) +QString IPrinter::progressBar(int percentage, int width) { + if (width == 0) + width = getTerminalWidth(); + width-=2; - int cols = 42; - if (const char* env_p = std::getenv("COLUMNS")) - { - cols = atoi(env_p); - } - cols -= 2; - qDebug() << "#col: " << cols; - - int pos_progress = cols*(percentage/100.0); + int pos_progress = width*(percentage/100.0); QString left = QString("=").repeated(pos_progress); - QString right = QString(" ").repeated(cols-pos_progress); + QString right = QString(" ").repeated(width-pos_progress); return QString("[%1%2]").arg(left).arg(right); } +int IPrinter::getTerminalWidth() +{ + struct winsize w; + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) + { + error("ioctl: get terminal width failed"); + return 42; // default fallback + } + return w.ws_col; +} QString IPrinter::formatTime(const int& milliseconds) { diff --git a/src/iprinter.h b/src/iprinter.h index e51b7d8..ec94104 100644 --- a/src/iprinter.h +++ b/src/iprinter.h @@ -40,11 +40,11 @@ public: void print(const char*, const seconds&); - QString progressBar(QTimer*); - QString progressBar(int); + QString progressBar(QTimer*, int width=0); + QString progressBar(int, int width=0); private: QString formatTime(const int&); - + int getTerminalWidth(); }; diff --git a/src/utils.cpp b/src/utils.cpp index c60ffc4..b264156 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -180,7 +180,7 @@ bool parseBool(const QString& key, const QJsonObject& jsonObject) bool parseConfig(QList<QPair<QString,QString>>& parsedCameras) { - qDebug() << "open " << configFile(); + // qDebug() << "open " << configFile(); QFile file(configFile()); if (!file.open(QIODevice::ReadOnly)) { @@ -238,14 +238,6 @@ bool parseConfig(QList<QPair<QString,QString>>& parsedCameras) parsedCameras.append(qMakePair(name, ip)); } ncam(parsedCameras.size()); - - qDebug() << "parsed recDuration:" << recDuration().count(); - qDebug() << "parsed outDir:" << outDir(); - qDebug() << "threads per cam:" << threadsPerCam(); - qDebug() << "parsed frameSize:" << frameSize(); - qDebug() << "parsed fps:" << fps(); - qDebug() << "parsed ncam:" << ncam(); - return true; } -- GitLab