QuasarAppLib/helpdata.cpp

84 lines
2.3 KiB
C++
Raw Normal View History

2020-02-18 22:28:17 +03:00
#include "helpdata.h"
#include <iostream>
#ifdef Q_OS_WIN32
#include <windows.h>
#else
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#endif
static int MAX_LENGTH = -1;
2020-02-19 13:32:22 +03:00
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())
2020-02-19 13:32:22 +03:00
2020-02-18 22:28:17 +03:00
void QuasarAppUtils::Help::print(const QuasarAppUtils::Help::Options &charter) {
2020-02-19 13:32:22 +03:00
int maxLength = 0;
for (auto line = charter.begin(); line != charter.end(); ++line) {
if (line.key().size() > maxLength)
maxLength = line.key().size();
}
2020-02-18 22:28:17 +03:00
for (auto line = charter.begin(); line != charter.end(); ++line) {
2020-02-19 13:32:22 +03:00
print(line.key(), line.value(), maxLength + SectionMargin);
std::cout << std::endl;
2020-02-18 22:28:17 +03:00
}
}
void QuasarAppUtils::Help::print(const QuasarAppUtils::Help::Charters &help) {
for (auto line = help.begin(); line != help.end(); ++line) {
QString expander(WIDTH, '-');
2020-02-18 22:28:17 +03:00
std::cout << line.key().toStdString()<< std::endl;
2020-02-19 13:32:22 +03:00
std::cout << expander.toStdString() << std::endl;
2020-02-18 22:28:17 +03:00
print(line.value());
2020-02-19 13:32:22 +03:00
std::cout << std::endl << expander.toStdString() << std::endl;
2020-02-18 22:28:17 +03:00
}
}
2020-02-19 13:32:22 +03:00
void QuasarAppUtils::Help::print(const QString &key, const QString &value, int keyLength) {
2020-02-18 22:28:17 +03:00
2020-02-19 13:32:22 +03:00
auto diffExpander = QString(keyLength - key.size(), ' ');
std::cout << SECTION_MARGIN << key.toStdString() << diffExpander.toStdString() << ":";
2020-02-18 22:28:17 +03:00
2020-02-19 13:32:22 +03:00
QString expander(keyLength + SectionMargin, ' ');
2020-02-18 22:28:17 +03:00
auto words = value.split(" ");
int currentLength = keyLength;
for (const auto& word : words) {
if (currentLength + 2 + word.size() < WIDTH) {
2020-02-19 19:25:22 +03:00
std::cout << " " << word.toStdString();
2020-02-18 22:28:17 +03:00
currentLength += 2 + word.size();
} else {
2020-02-19 13:32:22 +03:00
std::cout << std::endl << expander.toStdString() << ":";
2020-02-18 22:28:17 +03:00
currentLength = keyLength;
}
}
}
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
}