Skip to content

Commit cff1bff

Browse files
authored
Wrap text also for --cache edit help text (#233)
1 parent 0e933b9 commit cff1bff

4 files changed

Lines changed: 65 additions & 49 deletions

File tree

src/cache.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "pathtools.h"
3333
#include "queue.h"
3434
#include "skyscraper.h"
35+
#include "strtools.h"
3536

3637
#include <QBuffer>
3738
#include <QDateTime>
@@ -383,14 +384,16 @@ int Cache::editResources(QSharedPointer<Queue> queue, const QString &command,
383384

384385
int retVal = 0;
385386
int queueLength = queue->length();
386-
printf("\033[1mEntering resource cache editing mode.\n\nThis mode allows "
387-
"you to edit textual resources for your files. To add media "
388-
"resources use the 'import' scraping module instead.\nYou "
389-
"can provide one or more file names on command line to edit "
390-
"resources for just those specific files. You can also use the "
391-
"'--startat' and '--endat' command line options to narrow down the "
392-
"span of the roms you wish to edit. Otherwise Skyscraper will edit "
393-
"ALL files found in the input folder one by one.\033[0m\n\n");
387+
const QString cacheEditHint =
388+
"\033[1m\nEntering resource cache editing mode.\033[0m\nThis mode "
389+
"allows you to edit textual resources for your files. To add media and "
390+
"text resources use the 'import' scraping module instead.\nIn the "
391+
"cache editing mode you can provide one or more file names on command "
392+
"line to edit resources for just those specific files. You can also "
393+
"use the '--startat' and '--endat' command line options to narrow down "
394+
"the span of the roms you wish to edit. Otherwise Skyscraper will edit "
395+
"ALL files found in the input folder one by one.\n";
396+
printf("%s", StrTools::wrapText(cacheEditHint).toStdString().c_str());
394397
while (queue->hasEntry()) {
395398
QFileInfo info = queue->takeEntry();
396399
QString cacheId = getQuickId(info);

src/cli.cpp

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -557,27 +557,10 @@ void Cli::showHint() {
557557
#ifdef Q_OS_LINUX
558558
hint = hint.replace(QDir::homePath(), "~");
559559
#endif
560-
hint = "\033[1;33mDID YOU KNOW\033[0m: " + hint;
561-
562-
// TODO: put into StrTools
563-
QStringList hintWrapped;
564-
int ptr = 0;
565-
QString line;
566-
for (auto const &w : hint.split(' ')) {
567-
if (ptr + w.length() >= 80) {
568-
ptr = 0;
569-
line.chop(1);
570-
hintWrapped.append(line);
571-
line = "";
572-
}
573-
ptr += w.length() + 1;
574-
line.append(w);
575-
line.append(' ');
576-
}
577-
line.chop(1);
578-
hintWrapped.append(line);
579-
580-
printf("%s\n\n", hintWrapped.join("\n").toStdString().c_str());
560+
hint = "DID YOU KNOW: " + hint;
561+
QString hintWrapped = StrTools::wrapText(hint);
562+
hintWrapped = hintWrapped.replace(" KNOW: "," KNOW\033[0m: ");
563+
printf("\033[1;33m%s\n\n", hintWrapped.toStdString().c_str());
581564
}
582565

583566
void Cli::showBuildinfo() {

src/strtools.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,3 +407,33 @@ QString StrTools::shortenText(QString text, int maxLength) {
407407
return text.left(maxLength - 5) % "[...]";
408408
return text;
409409
}
410+
411+
QString StrTools::wrapText(const QString &inText, int width) {
412+
QStringList wrappedLines;
413+
int ptr = 0;
414+
QString line;
415+
for (auto const &ws : inText.split(' ', Qt::SkipEmptyParts)) {
416+
for (auto const &wn : ws.split('\n')) {
417+
bool nl = wn.isEmpty() || (wn != ws && !ws.endsWith(wn));
418+
if (nl || ptr + wn.length() >= width) {
419+
ptr = 0;
420+
line.chop(1);
421+
if (nl) {
422+
line.append(' ');
423+
line.append(wn);
424+
}
425+
wrappedLines.append(line);
426+
line = "";
427+
if (nl) {
428+
continue;
429+
}
430+
}
431+
ptr += wn.length() + 1;
432+
line.append(wn);
433+
line.append(' ');
434+
}
435+
}
436+
line.chop(1);
437+
wrappedLines.append(line);
438+
return wrappedLines.join("\n");
439+
}

src/strtools.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@
2626
#ifndef STRTOOLS_H
2727
#define STRTOOLS_H
2828

29-
#include <QObject>
30-
31-
class StrTools : public QObject {
32-
public:
33-
static QString xmlUnescape(QString str);
34-
static QString xmlEscape(QString str);
35-
static QByteArray magic(const QByteArray str);
36-
static QByteArray unMagic(const QByteArray str);
37-
static QString conformPlayers(const QString str);
38-
static QString conformAges(QString str);
39-
static QString conformReleaseDate(QString str);
40-
static QString conformTags(const QString str);
41-
static QString getVersionHeader();
42-
static QString getVersionBanner();
43-
static QString stripBrackets(const QString str);
44-
static QString stripHtmlTags(QString str);
45-
static QString getMd5Sum(const QByteArray &data);
46-
static QString tidyText(QString text, bool ignoreBangs);
47-
static QString shortenText(QString text, int maxLength);
48-
};
29+
#include <QByteArray>
30+
#include <QString>
4931

32+
namespace StrTools {
33+
QString xmlUnescape(QString str);
34+
QString xmlEscape(QString str);
35+
QByteArray magic(const QByteArray str);
36+
QByteArray unMagic(const QByteArray str);
37+
QString conformPlayers(const QString str);
38+
QString conformAges(QString str);
39+
QString conformReleaseDate(QString str);
40+
QString conformTags(const QString str);
41+
QString getVersionHeader();
42+
QString getVersionBanner();
43+
QString stripBrackets(const QString str);
44+
QString stripHtmlTags(QString str);
45+
QString getMd5Sum(const QByteArray &data);
46+
QString tidyText(QString text, bool ignoreBangs);
47+
QString shortenText(QString text, int maxLength);
48+
QString wrapText(const QString &inText, int width = 80);
49+
}; // namespace StrTools
5050
#endif // STRTOOLS_H

0 commit comments

Comments
 (0)