From c1ea126efe5aca9f45fc125f28c8fe92bc39bcc4 Mon Sep 17 00:00:00 2001 From: Christian Hohnstaedt Date: Tue, 18 Feb 2020 06:37:01 +0100 Subject: [PATCH] linewrap in help --- lib/arguments.cpp | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/arguments.cpp b/lib/arguments.cpp index e091f35d..d9d5fae6 100644 --- a/lib/arguments.cpp +++ b/lib/arguments.cpp @@ -6,6 +6,9 @@ */ #include +#include +#include + #include "arguments.h" #include @@ -58,10 +61,34 @@ void arg_option::fillOption(struct option *opt) const opt->val = 0; } +static QString splitQstring(int offset, int width, const QString &text) +{ + QStringList lines; + QString line; + + foreach(const QString &word, text.split(" ")) { + if (line.size() + word.size() < width - offset) { + line += " " + word; + continue; + } + lines += line; + line.clear(); + } + lines += line; + return lines.join(QString("\n") +QString().fill(' ', offset)); +} + QString arguments::help() { QString s; size_t len = 0; + int width = 80, offset; + struct winsize w; + + ioctl(0, TIOCGWINSZ, &w); + if (w.ws_col > 20) + width = w.ws_col; + foreach(const arg_option &a, opts) { size_t l = strlen(a.long_opt) + 1; if (a.arg) @@ -69,11 +96,13 @@ QString arguments::help() if (l > len) len = l; } + offset = len + 5; for (auto i = opts.begin(); i != opts.end(); ++i) { QString longopt = i->long_opt; if (i->arg) longopt += QString("=%1").arg(i->arg); - s += QString(" --%1 %2\n").arg(longopt, len*-1).arg(i->help); + QString help = splitQstring(offset, width, i->help); + s += QString(" --%1 %2\n").arg(longopt, len*-1).arg(help); } return s; }