diff options
Diffstat (limited to 'akonadish/syntax_modules/core_syntax.cpp')
-rw-r--r-- | akonadish/syntax_modules/core_syntax.cpp | 178 |
1 files changed, 178 insertions, 0 deletions
diff --git a/akonadish/syntax_modules/core_syntax.cpp b/akonadish/syntax_modules/core_syntax.cpp new file mode 100644 index 0000000..31b824a --- /dev/null +++ b/akonadish/syntax_modules/core_syntax.cpp | |||
@@ -0,0 +1,178 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify | ||
5 | * it under the terms of the GNU General Public License as published by | ||
6 | * the Free Software Foundation; either version 2 of the License, or | ||
7 | * (at your option) any later version. | ||
8 | * | ||
9 | * This program is distributed in the hope that it will be useful, | ||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | * GNU General Public License for more details. | ||
13 | * | ||
14 | * You should have received a copy of the GNU General Public License | ||
15 | * along with this program; if not, write to the | ||
16 | * Free Software Foundation, Inc., | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
18 | */ | ||
19 | |||
20 | #include <QDebug> | ||
21 | #include <QObject> // tr() | ||
22 | #include <QSet> | ||
23 | #include <QTextStream> | ||
24 | |||
25 | #include "state.h" | ||
26 | #include "syntaxtree.h" | ||
27 | |||
28 | namespace CoreSyntax | ||
29 | { | ||
30 | |||
31 | bool exit(const QStringList &, State &) | ||
32 | { | ||
33 | ::exit(0); | ||
34 | return true; | ||
35 | } | ||
36 | |||
37 | bool showHelp(const QStringList &commands, State &state) | ||
38 | { | ||
39 | SyntaxTree::Command command = SyntaxTree::self()->match(commands); | ||
40 | if (commands.isEmpty()) { | ||
41 | state.printLine(QObject::tr("Welcome to the Akonadi2 command line tool!")); | ||
42 | state.printLine(QObject::tr("Top-level commands:")); | ||
43 | |||
44 | QSet<QString> sorted; | ||
45 | for (auto syntax: SyntaxTree::self()->syntax()) { | ||
46 | sorted.insert(syntax.keyword); | ||
47 | } | ||
48 | |||
49 | for (auto keyword: sorted) { | ||
50 | state.printLine(keyword, 1); | ||
51 | } | ||
52 | } else if (const Syntax *syntax = command.first) { | ||
53 | //TODO: get parent! | ||
54 | state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); | ||
55 | |||
56 | if (!syntax->help.isEmpty()) { | ||
57 | state.print(": " + syntax->help); | ||
58 | } | ||
59 | state.printLine(); | ||
60 | |||
61 | if (!syntax->children.isEmpty()) { | ||
62 | state.printLine("Sub-commands:", 1); | ||
63 | QSet<QString> sorted; | ||
64 | for (auto childSyntax: syntax->children) { | ||
65 | sorted.insert(childSyntax.keyword); | ||
66 | } | ||
67 | |||
68 | for (auto keyword: sorted) { | ||
69 | state.printLine(keyword, 1); | ||
70 | } | ||
71 | } | ||
72 | } else { | ||
73 | state.printError("Unknown command: " + commands.join(" ")); | ||
74 | } | ||
75 | |||
76 | return true; | ||
77 | } | ||
78 | |||
79 | QStringList showHelpCompleter(const QStringList &commands, const QString &fragment) | ||
80 | { | ||
81 | QStringList items; | ||
82 | |||
83 | for (auto syntax: SyntaxTree::self()->syntax()) { | ||
84 | if (syntax.keyword != QObject::tr("help") && | ||
85 | (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) { | ||
86 | items << syntax.keyword; | ||
87 | } | ||
88 | } | ||
89 | |||
90 | qSort(items); | ||
91 | return items; | ||
92 | } | ||
93 | |||
94 | bool setDebugLevel(const QStringList &commands, State &state) | ||
95 | { | ||
96 | if (commands.count() != 1) { | ||
97 | state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count())); | ||
98 | return false; | ||
99 | } | ||
100 | |||
101 | bool ok = false; | ||
102 | int level = commands[0].toUInt(&ok); | ||
103 | |||
104 | if (!ok) { | ||
105 | state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0])); | ||
106 | return false; | ||
107 | } | ||
108 | |||
109 | state.setDebugLevel(level); | ||
110 | return true; | ||
111 | } | ||
112 | |||
113 | bool printDebugLevel(const QStringList &, State &state) | ||
114 | { | ||
115 | state.printLine(QString::number(state.debugLevel())); | ||
116 | return true; | ||
117 | } | ||
118 | |||
119 | bool printCommandTiming(const QStringList &, State &state) | ||
120 | { | ||
121 | state.printLine(state.commandTiming() ? QObject::tr("on") : QObject::tr("off")); | ||
122 | return true; | ||
123 | } | ||
124 | |||
125 | void printSyntaxBranch(State &state, const Syntax::List &list, int depth) | ||
126 | { | ||
127 | if (list.isEmpty()) { | ||
128 | return; | ||
129 | } | ||
130 | |||
131 | if (depth > 0) { | ||
132 | state.printLine("\\", depth); | ||
133 | } | ||
134 | |||
135 | for (auto syntax: list) { | ||
136 | state.print("|-", depth); | ||
137 | state.printLine(syntax.keyword); | ||
138 | printSyntaxBranch(state, syntax.children, depth + 1); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | bool printSyntaxTree(const QStringList &, State &state) | ||
143 | { | ||
144 | printSyntaxBranch(state, SyntaxTree::self()->syntax(), 0); | ||
145 | return true; | ||
146 | } | ||
147 | |||
148 | Syntax::List syntax() | ||
149 | { | ||
150 | Syntax::List syntax; | ||
151 | syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); | ||
152 | |||
153 | Syntax help("help", QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); | ||
154 | help.completer = &CoreSyntax::showHelpCompleter; | ||
155 | syntax << help; | ||
156 | |||
157 | syntax << Syntax("syntaxtree", QString(), &printSyntaxTree); | ||
158 | |||
159 | Syntax set("set", QObject::tr("Sets settings for the session")); | ||
160 | set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); | ||
161 | Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete")); | ||
162 | setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); | ||
163 | setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); | ||
164 | set.children << setTiming; | ||
165 | syntax << set; | ||
166 | |||
167 | Syntax get("get", QObject::tr("Gets settings for the session")); | ||
168 | get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); | ||
169 | get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); | ||
170 | syntax << get; | ||
171 | |||
172 | return syntax; | ||
173 | } | ||
174 | |||
175 | REGISTER_SYNTAX(CoreSyntax) | ||
176 | |||
177 | } // namespace CoreSyntax | ||
178 | |||