QuasarAppLib
helpdata.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2025 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "helpdata.h"
9#include <iostream>
10
11#ifdef Q_OS_WIN32
12#include <windows.h>
13#else
14#include <sys/ioctl.h>
15#include <unistd.h>
16#endif
17
18namespace QuasarAppUtils {
19namespace Help {
20
21static int MAX_LENGTH = -1;
22static int SectionMargin = 2;
23
24#define EXPANDER(COUNT, ITEM) QString(COUNT, ITEM).toStdString()
25#define SPACES(X) EXPANDER(X, ' ')
26#define SECTION_MARGIN SPACES(SectionMargin)
27#define WIDTH ((MAX_LENGTH > 10)? MAX_LENGTH: width())
28
29
30/*
31 * @brief print This method prints the one line of the help.
32 * @param key This is Option name.
33 * @param value This is Description of option.
34 * @param keyLength This is length of the current line.
35 * This is private method of the QuasarAppLibrary.
36 */
37void print(const QString& key, const QString& value, int keyLength) {
38
39 auto diffExpander = QString(keyLength - key.size(), ' ');
40 std::cout << SECTION_MARGIN << key.toStdString() << diffExpander.toStdString() << ":";
41
42 QString expander(keyLength + SectionMargin, ' ');
43 auto words = value.split(" ");
44
45 int currentLength = std::max(keyLength, static_cast<int>(key.size()));
46 for (const auto& word : words) {
47 if (currentLength + 2 + word.size() < WIDTH) {
48 std::cout << " " << word.toStdString();
49 currentLength += 2 + word.size();
50
51 } else {
52 std::cout << std::endl << expander.toStdString() << ":";
53 currentLength = keyLength;
54
55 std::cout << " " << word.toStdString();
56 currentLength += 2 + word.size();
57 }
58 }
59}
60
61void print(const QuasarAppUtils::Help::Options &oprionsList) {
62 int maxLength = 0;
63 for (auto line = oprionsList.begin(); line != oprionsList.end(); ++line) {
64 if (line.key().size() > maxLength)
65 maxLength = line.key().size();
66 }
67
68 maxLength = std::min(WIDTH / 3, maxLength);
69
70 for (auto line = oprionsList.begin(); line != oprionsList.end(); ++line) {
71 print(line.key(), line.value(), maxLength + SectionMargin);
72 std::cout << std::endl;
73 }
74}
75
76void print(const Section &help) {
77 for (auto line = help.begin(); line != help.end(); ++line) {
78 QString expander(WIDTH, '-');
79
80 std::cout << line.key().toStdString() << std::endl;
81 std::cout << expander.toStdString() << std::endl;
82 print(line.value());
83 std::cout << std::endl << expander.toStdString() << std::endl;
84 }
85}
86
87void setLineLength(int newLength) {
88 MAX_LENGTH = newLength;
89}
90
91int width() {
92
93#ifdef Q_OS_WIN32
94 CONSOLE_SCREEN_BUFFER_INFO csbi;
95
96 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
97 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
98#else
99 struct winsize w;
100 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
101 return w.ws_col;
102#endif
103}
104
105
106}
107
108}
#define WIDTH
Definition helpdata.cpp:27
#define SECTION_MARGIN
Definition helpdata.cpp:26
void setLineLength(int newLength)
setLineLength sets new length of the help line (width of the console window). If you set this into -1...
Definition helpdata.cpp:87
QMultiMap< QString, QString > Options
Options this is list of key-descriptions pairs of help. The key is name of the available argument and...
Definition helpdata.h:32
void print(const QString &key, const QString &value, int keyLength)
Definition helpdata.cpp:37
QMultiMap< QString, Options > Section
Section This is list of the help Sections. The one section it is Title of the section and Help::Optio...
Definition helpdata.h:45
int width()
width This method return current width of the cosole window.
Definition helpdata.cpp:91
The QuasaraAppUtils class This lib include base functions for the all applications of QuasarApp group...
Definition helpdata.cpp:18