added sorting of help and dynamic risize

This commit is contained in:
Andrei Yankovich 2020-02-20 10:33:45 +03:00
parent 105f019aa7
commit a5aca0e487
2 changed files with 34 additions and 6 deletions

View File

@ -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
}

View File

@ -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