QuasarAppLib
1.0.530.c9fba11
Toggle main menu visibility
helpdata.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2018-2026 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
18
namespace
QuasarAppUtils
{
19
namespace
Help
{
20
21
static
int
MAX_LENGTH = -1;
22
static
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
*/
37
void
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
61
void
print
(
const
QuasarAppUtils::Help::Options
&optionsList) {
62
int
maxLength = 0;
63
for
(
auto
line = optionsList.begin(); line != optionsList.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 = optionsList.begin(); line != optionsList.end(); ++line) {
71
print
(line.key(), line.value(), maxLength + SectionMargin);
72
std::cout << std::endl;
73
}
74
}
75
76
void
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
87
void
setLineLength
(
int
newLength) {
88
MAX_LENGTH = newLength;
89
}
90
91
int
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
}
WIDTH
#define WIDTH
Definition
helpdata.cpp:27
SECTION_MARGIN
#define SECTION_MARGIN
Definition
helpdata.cpp:26
helpdata.h
QuasarAppUtils::Help
Help namespace contains functions for printing help in to console. All Print functions automatically ...
Definition
helpdata.cpp:19
QuasarAppUtils::Help::setLineLength
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
QuasarAppUtils::Help::Options
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
QuasarAppUtils::Help::print
void print(const QString &key, const QString &value, int keyLength)
Definition
helpdata.cpp:37
QuasarAppUtils::Help::Section
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
QuasarAppUtils::Help::width
int width()
width This method return current width of the console window.
Definition
helpdata.cpp:91
QuasarAppUtils
The QuasarAppUtils namespace contains the base functions for all applications of the QuasarApp group....
Definition
crc32constexper.h:16
helpdata.cpp
Generated by
1.18.0