From a4d438b7f5780e865c824f6e7f892bb3f0ee2b61 Mon Sep 17 00:00:00 2001 From: Aaron Seigo Date: Wed, 23 Dec 2015 12:00:00 +0100 Subject: help now uses State to push out output --- akonadi2_cli/modules/core_syntax.cpp | 23 ++++++++++++----------- akonadi2_cli/state.cpp | 16 +++++++++++----- akonadi2_cli/state.h | 6 +++--- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/akonadi2_cli/modules/core_syntax.cpp b/akonadi2_cli/modules/core_syntax.cpp index 8324c31..944abbe 100644 --- a/akonadi2_cli/modules/core_syntax.cpp +++ b/akonadi2_cli/modules/core_syntax.cpp @@ -40,44 +40,45 @@ bool exit(const QStringList &, State &) return true; } -bool showHelp(const QStringList &commands, State &) +bool showHelp(const QStringList &commands, State &state) { Module::Command command = Module::self()->match(commands); - QTextStream stream(stdout); if (commands.isEmpty()) { - stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; - stream << QObject::tr("Top-level commands:") << "\n"; + state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!")); + state.printLine(QObject::tr("Top-level commands:")); + QSet sorted; for (auto syntax: Module::self()->syntax()) { sorted.insert(syntax.keyword); } for (auto keyword: sorted) { - stream << "\t" << keyword << "\n"; + state.printLine(keyword, 1); } } else if (const Module::Syntax *syntax = command.first) { //TODO: get parent! - stream << QObject::tr("Command `%1`").arg(syntax->keyword); + state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); if (!syntax->help.isEmpty()) { - stream << ": " << syntax->help; + state.print(": " + syntax->help); } - stream << "\n"; + state.printLine(); if (!syntax->children.isEmpty()) { - stream << "\tSub-commands:\n"; + state.printLine("Sub-commands:", 1); QSet sorted; for (auto childSyntax: syntax->children) { sorted.insert(childSyntax.keyword); } for (auto keyword: sorted) { - stream << "\t" << keyword << "\n"; + state.printLine(keyword, 1); } } } else { - stream << "Unknown command: " << commands.join(" ") << "\n"; + state.printError("Unknown command: " + commands.join(" ")); } + return true; } diff --git a/akonadi2_cli/state.cpp b/akonadi2_cli/state.cpp index 80a2d3a..9beba7e 100644 --- a/akonadi2_cli/state.cpp +++ b/akonadi2_cli/state.cpp @@ -27,17 +27,23 @@ State::State() { } -void State::print(const QString &message) +void State::print(const QString &message, unsigned int indentationLevel) { + for (unsigned int i = 0; i < indentationLevel; ++i) { + m_outStream << "\t"; + } + m_outStream << message; } -void State::printLine(const QString &message) +void State::printLine(const QString &message, unsigned int indentationLevel) { - m_outStream << message << "\n"; + print(message, indentationLevel); + m_outStream << "\n"; + m_outStream.flush(); } -void State::printError(const QString &error, int code) +void State::printError(const QString &errorMessage, const QString &errorCode) { - m_outStream << "ERROR " << code << ": " << error << "\n"; + printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); } diff --git a/akonadi2_cli/state.h b/akonadi2_cli/state.h index 4fd2935..a343e51 100644 --- a/akonadi2_cli/state.h +++ b/akonadi2_cli/state.h @@ -26,9 +26,9 @@ class State public: State(); - void print(const QString &string); - void printLine(const QString &string); - void printError(const QString &string, int errorCode = 0); + void print(const QString &message, unsigned int indentationLevel = 0); + void printLine(const QString &message = QString(), unsigned int indentationLevel = 0); + void printError(const QString &errorMessage, const QString &errorCode = QString()); private: QTextStream m_outStream; -- cgit v1.2.3