diff options
author | Aaron Seigo <aseigo@kde.org> | 2015-12-23 12:00:00 +0100 |
---|---|---|
committer | Aaron Seigo <aseigo@kde.org> | 2015-12-23 12:00:00 +0100 |
commit | a4d438b7f5780e865c824f6e7f892bb3f0ee2b61 (patch) | |
tree | 622374f9628b8a0113ce40eaa3bc1eb0e604a80a | |
parent | 6f720eaf111826a1588b37870bf1af638b1420a0 (diff) | |
download | sink-a4d438b7f5780e865c824f6e7f892bb3f0ee2b61.tar.gz sink-a4d438b7f5780e865c824f6e7f892bb3f0ee2b61.zip |
help now uses State to push out output
-rw-r--r-- | akonadi2_cli/modules/core_syntax.cpp | 23 | ||||
-rw-r--r-- | akonadi2_cli/state.cpp | 16 | ||||
-rw-r--r-- | 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 &) | |||
40 | return true; | 40 | return true; |
41 | } | 41 | } |
42 | 42 | ||
43 | bool showHelp(const QStringList &commands, State &) | 43 | bool showHelp(const QStringList &commands, State &state) |
44 | { | 44 | { |
45 | Module::Command command = Module::self()->match(commands); | 45 | Module::Command command = Module::self()->match(commands); |
46 | QTextStream stream(stdout); | ||
47 | if (commands.isEmpty()) { | 46 | if (commands.isEmpty()) { |
48 | stream << QObject::tr("Welcome to the Akonadi2 command line tool!") << "\n"; | 47 | state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!")); |
49 | stream << QObject::tr("Top-level commands:") << "\n"; | 48 | state.printLine(QObject::tr("Top-level commands:")); |
49 | |||
50 | QSet<QString> sorted; | 50 | QSet<QString> sorted; |
51 | for (auto syntax: Module::self()->syntax()) { | 51 | for (auto syntax: Module::self()->syntax()) { |
52 | sorted.insert(syntax.keyword); | 52 | sorted.insert(syntax.keyword); |
53 | } | 53 | } |
54 | 54 | ||
55 | for (auto keyword: sorted) { | 55 | for (auto keyword: sorted) { |
56 | stream << "\t" << keyword << "\n"; | 56 | state.printLine(keyword, 1); |
57 | } | 57 | } |
58 | } else if (const Module::Syntax *syntax = command.first) { | 58 | } else if (const Module::Syntax *syntax = command.first) { |
59 | //TODO: get parent! | 59 | //TODO: get parent! |
60 | stream << QObject::tr("Command `%1`").arg(syntax->keyword); | 60 | state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); |
61 | 61 | ||
62 | if (!syntax->help.isEmpty()) { | 62 | if (!syntax->help.isEmpty()) { |
63 | stream << ": " << syntax->help; | 63 | state.print(": " + syntax->help); |
64 | } | 64 | } |
65 | stream << "\n"; | 65 | state.printLine(); |
66 | 66 | ||
67 | if (!syntax->children.isEmpty()) { | 67 | if (!syntax->children.isEmpty()) { |
68 | stream << "\tSub-commands:\n"; | 68 | state.printLine("Sub-commands:", 1); |
69 | QSet<QString> sorted; | 69 | QSet<QString> sorted; |
70 | for (auto childSyntax: syntax->children) { | 70 | for (auto childSyntax: syntax->children) { |
71 | sorted.insert(childSyntax.keyword); | 71 | sorted.insert(childSyntax.keyword); |
72 | } | 72 | } |
73 | 73 | ||
74 | for (auto keyword: sorted) { | 74 | for (auto keyword: sorted) { |
75 | stream << "\t" << keyword << "\n"; | 75 | state.printLine(keyword, 1); |
76 | } | 76 | } |
77 | } | 77 | } |
78 | } else { | 78 | } else { |
79 | stream << "Unknown command: " << commands.join(" ") << "\n"; | 79 | state.printError("Unknown command: " + commands.join(" ")); |
80 | } | 80 | } |
81 | |||
81 | return true; | 82 | return true; |
82 | } | 83 | } |
83 | 84 | ||
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() | |||
27 | { | 27 | { |
28 | } | 28 | } |
29 | 29 | ||
30 | void State::print(const QString &message) | 30 | void State::print(const QString &message, unsigned int indentationLevel) |
31 | { | 31 | { |
32 | for (unsigned int i = 0; i < indentationLevel; ++i) { | ||
33 | m_outStream << "\t"; | ||
34 | } | ||
35 | |||
32 | m_outStream << message; | 36 | m_outStream << message; |
33 | } | 37 | } |
34 | 38 | ||
35 | void State::printLine(const QString &message) | 39 | void State::printLine(const QString &message, unsigned int indentationLevel) |
36 | { | 40 | { |
37 | m_outStream << message << "\n"; | 41 | print(message, indentationLevel); |
42 | m_outStream << "\n"; | ||
43 | m_outStream.flush(); | ||
38 | } | 44 | } |
39 | 45 | ||
40 | void State::printError(const QString &error, int code) | 46 | void State::printError(const QString &errorMessage, const QString &errorCode) |
41 | { | 47 | { |
42 | m_outStream << "ERROR " << code << ": " << error << "\n"; | 48 | printLine("ERROR" + (errorCode.isEmpty() ? "" : " " + errorCode) + ": " + errorMessage); |
43 | } | 49 | } |
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 | |||
26 | public: | 26 | public: |
27 | State(); | 27 | State(); |
28 | 28 | ||
29 | void print(const QString &string); | 29 | void print(const QString &message, unsigned int indentationLevel = 0); |
30 | void printLine(const QString &string); | 30 | void printLine(const QString &message = QString(), unsigned int indentationLevel = 0); |
31 | void printError(const QString &string, int errorCode = 0); | 31 | void printError(const QString &errorMessage, const QString &errorCode = QString()); |
32 | 32 | ||
33 | private: | 33 | private: |
34 | QTextStream m_outStream; | 34 | QTextStream m_outStream; |