diff --git a/helpdata.cpp b/helpdata.cpp index 8933ab6..8ebf904 100644 --- a/helpdata.cpp +++ b/helpdata.cpp @@ -1,12 +1,23 @@ #include "helpdata.h" #include -static int MAX_LENGTH = 80; +#ifdef Q_OS_WIN32 +#include +#else +#include +#include +#include +#endif + + + +static int MAX_LENGTH = -1; static int SectionMargin = 3; #define EXPANDER(COUNT, ITEM) QString(COUNT, ITEM).toStdString() #define SPACES(X) EXPANDER(X, ' ') #define SECTION_MARGIN SPACES(SectionMargin) +#define WIDTH ((MAX_LENGTH > 10)? MAX_LENGTH: width()) void QuasarAppUtils::Help::print(const QuasarAppUtils::Help::Options &charter) { int maxLength = 0; @@ -23,7 +34,7 @@ void QuasarAppUtils::Help::print(const QuasarAppUtils::Help::Options &charter) { void QuasarAppUtils::Help::print(const QuasarAppUtils::Help::Charters &help) { for (auto line = help.begin(); line != help.end(); ++line) { - QString expander(MAX_LENGTH, '-'); + QString expander(WIDTH, '-'); std::cout << line.key().toStdString()<< std::endl; std::cout << expander.toStdString() << std::endl; @@ -42,7 +53,7 @@ void QuasarAppUtils::Help::print(const QString &key, const QString &value, int k int currentLength = keyLength; for (const auto& word : words) { - if (currentLength + 2 + word.size() < MAX_LENGTH) { + if (currentLength + 2 + word.size() < WIDTH) { std::cout << " " << word.toStdString(); currentLength += 2 + word.size(); } else { @@ -55,3 +66,18 @@ void QuasarAppUtils::Help::print(const QString &key, const QString &value, int k void QuasarAppUtils::Help::setLineLength(int newLength) { MAX_LENGTH = newLength; } + +int QuasarAppUtils::Help::width() +{ + +#ifdef Q_OS_WIN32 + CONSOLE_SCREEN_BUFFER_INFO csbi; + + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + return csbi.srWindow.Right - csbi.srWindow.Left + 1; +#else + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + return w.ws_col; +#endif +} diff --git a/helpdata.h b/helpdata.h index 3de3c0c..28a5417 100644 --- a/helpdata.h +++ b/helpdata.h @@ -1,12 +1,14 @@ #ifndef HELPDATA_H #define HELPDATA_H -#include +#include namespace QuasarAppUtils{ namespace Help { -typedef QHash Options; -typedef QHash Charters; +typedef QMap Options; +typedef QMap Charters; + +int width(); /** * @brief print - line of help