| 1 |
#include <pbcopper/cli2/internal/HelpMetrics.h> |
| 2 |
|
| 3 |
#include <pbcopper/cli2/OptionValue.h> |
| 4 |
#include <pbcopper/utility/StringUtils.h> |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <iomanip> |
| 8 |
#include <ios> |
| 9 |
#include <sstream> |
| 10 |
#include <string> |
| 11 |
|
| 12 |
#include <cassert> |
| 13 |
|
| 14 |
#include <sys/ioctl.h> |
| 15 |
#include <unistd.h> |
| 16 |
|
| 17 |
namespace PacBio { |
| 18 |
namespace CLI_v2 { |
| 19 |
namespace internal { |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
size_t AdjustedMaxColumn(size_t maxColumn) |
| 24 |
{ |
| 25 |
// if column count is explicitly set (i.e. for testing), use that |
| 26 |
if (HelpMetrics::TestingFixedWidth != 0) { |
| 27 |
return HelpMetrics::TestingFixedWidth; |
| 28 |
} |
| 29 |
|
| 30 |
// otherwise determine column count from terminal width (default behavior) |
| 31 |
struct winsize ws; |
| 32 |
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws); |
| 33 |
if (ws.ws_col >= 2) { |
| 34 |
maxColumn = ws.ws_col - 1; |
| 35 |
} |
| 36 |
constexpr size_t MAX_COLUMN = 119; |
| 37 |
return std::min(maxColumn, MAX_COLUMN); |
| 38 |
} |
| 39 |
|
| 40 |
std::string DisplayName(const OptionValueType type) |
| 41 |
{ |
| 42 |
switch (type) { |
| 43 |
case OptionValueType::BOOL: |
| 44 |
return ""; |
| 45 |
case OptionValueType::INT: |
| 46 |
case OptionValueType::UINT: |
| 47 |
return "INT"; |
| 48 |
case OptionValueType::STRING: |
| 49 |
return "STR"; |
| 50 |
case OptionValueType::DIR: |
| 51 |
return "DIR"; |
| 52 |
case OptionValueType::FILE: |
| 53 |
return "FILE"; |
| 54 |
case OptionValueType::FLOAT: |
| 55 |
return "FLOAT"; |
| 56 |
default: |
| 57 |
assert(false); |
| 58 |
} |
| 59 |
return {}; // unreachable |
| 60 |
} |
| 61 |
|
| 62 |
} // namespace |
| 63 |
|
| 64 |
// Some tests are not capabile of calling the printer's ctor that takes an |
| 65 |
// explicit column count (i.e. CLI::Run()). Setting this value beforehand provides |
| 66 |
// this testing hook. The default of 0 leaves auto-detection enabled. |
| 67 |
// |
| 68 |
size_t HelpMetrics::TestingFixedWidth = 0; |
| 69 |
|
| 70 |
HelpMetrics::HelpMetrics(const Interface& interface, HiddenOptionMode hiddenOptionMode) |
| 71 |
: ShowHiddenOptions{hiddenOptionMode == HiddenOptionMode::SHOW} |
| 72 |
{ |
| 73 |
MaxColumn = AdjustedMaxColumn(MaxColumn); |
| 74 |
Calculate(interface); |
| 75 |
} |
| 76 |
|
| 77 |
HelpMetrics::HelpMetrics(const MultiToolInterface& interface, HiddenOptionMode hiddenOptionMode) |
| 78 |
: ShowHiddenOptions{hiddenOptionMode == HiddenOptionMode::SHOW} |
| 79 |
{ |
| 80 |
MaxColumn = AdjustedMaxColumn(MaxColumn); |
| 81 |
Calculate(interface); |
| 82 |
} |
| 83 |
|
| 84 |
HelpMetrics::HelpMetrics(const Interface& interface, const size_t explicitMaxColumn, |
| 85 |
HiddenOptionMode hiddenOptionMode) |
| 86 |
: MaxColumn{explicitMaxColumn}, ShowHiddenOptions{hiddenOptionMode == HiddenOptionMode::SHOW} |
| 87 |
{ |
| 88 |
Calculate(interface); |
| 89 |
} |
| 90 |
|
| 91 |
HelpMetrics::HelpMetrics(const MultiToolInterface& interface, const size_t explicitMaxColumn, |
| 92 |
HiddenOptionMode hiddenOptionMode) |
| 93 |
: MaxColumn{explicitMaxColumn}, ShowHiddenOptions{hiddenOptionMode == HiddenOptionMode::SHOW} |
| 94 |
{ |
| 95 |
Calculate(interface); |
| 96 |
} |
| 97 |
|
| 98 |
void HelpMetrics::Calculate(const Interface& interface) |
| 99 |
{ |
| 100 |
// metrics using client options |
| 101 |
for (const auto& optionGroup : interface.OptionGroups()) { |
| 102 |
for (const auto& option : optionGroup.Options) { |
| 103 |
UpdateForOption(option); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// metrics with builtin options |
| 108 |
UpdateForOption(interface.HelpOption()); |
| 109 |
UpdateForOption(interface.VersionOption()); |
| 110 |
|
| 111 |
if (ShowHiddenOptions) { |
| 112 |
UpdateForOption(interface.AlarmsOption()); |
| 113 |
UpdateForOption(interface.ExceptionsPassthroughOption()); |
| 114 |
UpdateForOption(interface.ShowAllHelpOption()); |
| 115 |
} |
| 116 |
|
| 117 |
if (interface.NumThreadsOption()) { |
| 118 |
UpdateForOption(*interface.NumThreadsOption()); |
| 119 |
} |
| 120 |
if (interface.LogLevelOption()) { |
| 121 |
UpdateForOption(*interface.LogLevelOption()); |
| 122 |
} |
| 123 |
if (interface.LogFileOption()) { |
| 124 |
UpdateForOption(*interface.LogFileOption()); |
| 125 |
} |
| 126 |
if (interface.VerboseOption()) { |
| 127 |
UpdateForOption(*interface.VerboseOption()); |
| 128 |
} |
| 129 |
|
| 130 |
// metrics using pos args |
| 131 |
for (const auto& posArg : interface.PositionalArguments()) { |
| 132 |
auto posArgDisplayText = DisplayName(posArg.Type); |
| 133 |
MaxNameLength = std::max(MaxNameLength, posArg.Name.size()); |
| 134 |
MaxTypeLength = std::max(MaxTypeLength, posArgDisplayText.size()); |
| 135 |
FormattedPosArgNames.emplace(posArg, |
| 136 |
FormattedEntry{posArg.Name, std::move(posArgDisplayText)}); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void HelpMetrics::Calculate(const MultiToolInterface& interface) |
| 141 |
{ |
| 142 |
UpdateForOption(interface.HelpOption()); |
| 143 |
UpdateForOption(interface.VersionOption()); |
| 144 |
if (ShowHiddenOptions) { |
| 145 |
UpdateForOption(interface.ShowAllHelpOption()); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
std::string HelpMetrics::HelpEntry(const OptionData& option) const |
| 150 |
{ |
| 151 |
const auto& formattedOption = FormattedOptionNames.at(option); |
| 152 |
return HelpEntry(formattedOption.nameString, formattedOption.typeString, option.Description); |
| 153 |
} |
| 154 |
|
| 155 |
std::string HelpMetrics::HelpEntry(std::string name, std::string type, |
| 156 |
const std::string& description) const |
| 157 |
{ |
| 158 |
// 2 spaces between left edge and text, and between longest option name |
| 159 |
// and (all) descriptions |
| 160 |
const std::string spacer{" "}; |
| 161 |
|
| 162 |
std::ostringstream out; |
| 163 |
|
| 164 |
// formatted name & type |
| 165 |
name.resize(MaxNameLength, ' '); |
| 166 |
type.resize(MaxTypeLength, ' '); |
| 167 |
out << spacer << std::setw(MaxNameLength) << std::left << name << spacer << type << spacer; |
| 168 |
|
| 169 |
// maybe wrap description |
| 170 |
const auto indent = out.str().length(); |
| 171 |
const size_t max = MaxColumn - indent; |
| 172 |
const auto wrappedLines = PacBio::Utility::WordWrappedLines(description, max); |
| 173 |
if (!wrappedLines.empty()) { |
| 174 |
out << wrappedLines.at(0); |
| 175 |
for (size_t i = 1; i < wrappedLines.size(); ++i) { |
| 176 |
out << '\n' << std::string(indent, ' ') << wrappedLines.at(i); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return out.str(); |
| 181 |
} |
| 182 |
|
| 183 |
std::string HelpMetrics::OptionNames(const OptionData& option) |
| 184 |
{ |
| 185 |
std::ostringstream optionOutput; |
| 186 |
auto first = true; |
| 187 |
for (const auto& name : option.Names) { |
| 188 |
|
| 189 |
if (first) { |
| 190 |
first = false; |
| 191 |
} else { |
| 192 |
optionOutput << ","; |
| 193 |
} |
| 194 |
|
| 195 |
if (name.size() == 1) { |
| 196 |
optionOutput << "-"; |
| 197 |
} else { |
| 198 |
optionOutput << "--"; |
| 199 |
} |
| 200 |
|
| 201 |
optionOutput << name; |
| 202 |
} |
| 203 |
return optionOutput.str(); |
| 204 |
} |
| 205 |
|
| 206 |
void HelpMetrics::UpdateForOption(const OptionData& option) |
| 207 |
{ |
| 208 |
if (option.IsHidden && !ShowHiddenOptions) { |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
auto optionNamesText = OptionNames(option); |
| 213 |
auto optionDisplayText = DisplayName(option.Type); |
| 214 |
MaxNameLength = std::max(MaxNameLength, optionNamesText.size()); |
| 215 |
MaxTypeLength = std::max(MaxTypeLength, optionDisplayText.size()); |
| 216 |
FormattedOptionNames.emplace( |
| 217 |
option, FormattedEntry{std::move(optionNamesText), std::move(optionDisplayText)}); |
| 218 |
} |
| 219 |
|
| 220 |
} // namespace internal |
| 221 |
} // namespace CLI_v2 |
| 222 |
} // namespace PacBio |