4
0
mirror of https://github.com/QuasarApp/QuasarAppLib.git synced 2025-04-28 10:44:41 +00:00

110 lines
3.0 KiB
C++
Raw Normal View History

2020-05-23 02:29:13 +03:00
/*
2023-12-31 09:23:23 +01:00
* Copyright (C) 2018-2024 QuasarApp.
2020-05-23 02:29:13 +03:00
* Distributed under the lgplv3 software license, see the accompanying
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
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
namespace QuasarAppUtils {
namespace Help {
static int MAX_LENGTH = -1;
2021-07-28 12:38:44 +03:00
static int SectionMargin = 2;
2020-02-19 13:32:22 +03:00
#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
/*
* @brief print This method prints the one line of the help.
* @param key This is Option name.
* @param value This is Description of option.
* @param keyLength This is length of the current line.
* This is private method of the QuasarAppLibrary.
*/
void 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(" ");
2021-07-28 13:23:50 +03:00
int currentLength = std::max(keyLength, static_cast<int>(key.size()));
2020-02-18 22:28:17 +03:00
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();
2020-04-25 14:48:27 +03:00
2020-02-18 22:28:17 +03:00
} 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;
2020-04-25 14:48:27 +03:00
std::cout << " " << word.toStdString();
currentLength += 2 + word.size();
2020-02-18 22:28:17 +03:00
}
}
}
void print(const QuasarAppUtils::Help::Options &oprionsList) {
int maxLength = 0;
for (auto line = oprionsList.begin(); line != oprionsList.end(); ++line) {
if (line.key().size() > maxLength)
maxLength = line.key().size();
}
2021-07-28 12:38:44 +03:00
maxLength = std::min(WIDTH / 3, maxLength);
for (auto line = oprionsList.begin(); line != oprionsList.end(); ++line) {
print(line.key(), line.value(), maxLength + SectionMargin);
std::cout << std::endl;
}
}
void print(const Section &help) {
for (auto line = help.begin(); line != help.end(); ++line) {
QString expander(WIDTH, '-');
std::cout << line.key().toStdString() << std::endl;
std::cout << expander.toStdString() << std::endl;
print(line.value());
std::cout << std::endl << expander.toStdString() << std::endl;
}
}
void setLineLength(int newLength) {
2020-02-18 22:28:17 +03:00
MAX_LENGTH = newLength;
}
int 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
}
}
}