From a5aca0e487db11cb938649cbdf184dcc2b659d6c Mon Sep 17 00:00:00 2001 From: "a.yankovich" <EndrIIMail@gmail.com> Date: Thu, 20 Feb 2020 10:33:45 +0300 Subject: [PATCH] added sorting of help and dynamic risize --- helpdata.cpp | 32 +++++++++++++++++++++++++++++--- helpdata.h | 8 +++++--- 2 files changed, 34 insertions(+), 6 deletions(-) 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 <iostream> -static int MAX_LENGTH = 80; +#ifdef Q_OS_WIN32 +#include <windows.h> +#else +#include <sys/ioctl.h> +#include <stdio.h> +#include <unistd.h> +#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 <QHash> +#include <QMap> namespace QuasarAppUtils{ namespace Help { -typedef QHash<QString, QString> Options; -typedef QHash<QString, Options> Charters; +typedef QMap<QString, QString> Options; +typedef QMap<QString, Options> Charters; + +int width(); /** * @brief print - line of help