diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-01-20 19:07:07 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-01-20 19:07:07 +0100 |
commit | bdb01c2c068df326f5a8328ed1492ab1bea388c5 (patch) | |
tree | 25c2ee1b29bc481b6914c244ed9ca194b1415d16 /sinksh/syntax_modules/core_syntax.cpp | |
parent | 17e7ee40c9185c0505883853345fd6024c675b1a (diff) | |
download | sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.tar.gz sink-bdb01c2c068df326f5a8328ed1492ab1bea388c5.zip |
Renamed Akonadi2 to Sink
(except for documentation).
Diffstat (limited to 'sinksh/syntax_modules/core_syntax.cpp')
-rw-r--r-- | sinksh/syntax_modules/core_syntax.cpp | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/sinksh/syntax_modules/core_syntax.cpp b/sinksh/syntax_modules/core_syntax.cpp new file mode 100644 index 0000000..f5b6274 --- /dev/null +++ b/sinksh/syntax_modules/core_syntax.cpp | |||
@@ -0,0 +1,204 @@ | |||
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 | #include "utils.h" | ||
28 | |||
29 | namespace CoreSyntax | ||
30 | { | ||
31 | |||
32 | bool exit(const QStringList &, State &) | ||
33 | { | ||
34 | ::exit(0); | ||
35 | return true; | ||
36 | } | ||
37 | |||
38 | bool showHelp(const QStringList &commands, State &state) | ||
39 | { | ||
40 | SyntaxTree::Command command = SyntaxTree::self()->match(commands); | ||
41 | if (commands.isEmpty()) { | ||
42 | state.printLine(QObject::tr("Welcome to the Sink command line tool!")); | ||
43 | state.printLine(QObject::tr("Top-level commands:")); | ||
44 | |||
45 | QSet<QString> sorted; | ||
46 | for (auto syntax: SyntaxTree::self()->syntax()) { | ||
47 | sorted.insert(syntax.keyword); | ||
48 | } | ||
49 | |||
50 | for (auto keyword: sorted) { | ||
51 | state.printLine(keyword, 1); | ||
52 | } | ||
53 | } else if (const Syntax *syntax = command.first) { | ||
54 | //TODO: get parent! | ||
55 | state.print(QObject::tr("Command `%1`").arg(syntax->keyword)); | ||
56 | |||
57 | if (!syntax->help.isEmpty()) { | ||
58 | state.print(": " + syntax->help); | ||
59 | } | ||
60 | state.printLine(); | ||
61 | |||
62 | if (!syntax->children.isEmpty()) { | ||
63 | state.printLine("Sub-commands:", 1); | ||
64 | QSet<QString> sorted; | ||
65 | for (auto childSyntax: syntax->children) { | ||
66 | sorted.insert(childSyntax.keyword); | ||
67 | } | ||
68 | |||
69 | for (auto keyword: sorted) { | ||
70 | state.printLine(keyword, 1); | ||
71 | } | ||
72 | } | ||
73 | } else { | ||
74 | state.printError("Unknown command: " + commands.join(" ")); | ||
75 | } | ||
76 | |||
77 | return true; | ||
78 | } | ||
79 | |||
80 | QStringList showHelpCompleter(const QStringList &commands, const QString &fragment, State &) | ||
81 | { | ||
82 | QStringList items; | ||
83 | |||
84 | for (auto syntax: SyntaxTree::self()->syntax()) { | ||
85 | if (syntax.keyword != QObject::tr("help") && | ||
86 | (fragment.isEmpty() || syntax.keyword.startsWith(fragment))) { | ||
87 | items << syntax.keyword; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | qSort(items); | ||
92 | return items; | ||
93 | } | ||
94 | |||
95 | bool setDebugLevel(const QStringList &commands, State &state) | ||
96 | { | ||
97 | if (commands.count() != 1) { | ||
98 | state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count())); | ||
99 | return false; | ||
100 | } | ||
101 | |||
102 | bool ok = false; | ||
103 | int level = commands[0].toUInt(&ok); | ||
104 | |||
105 | if (!ok) { | ||
106 | state.printError(QObject::tr("Expected a number between 0 and 6, got %1").arg(commands[0])); | ||
107 | return false; | ||
108 | } | ||
109 | |||
110 | state.setDebugLevel(level); | ||
111 | return true; | ||
112 | } | ||
113 | |||
114 | bool printDebugLevel(const QStringList &, State &state) | ||
115 | { | ||
116 | state.printLine(QString::number(state.debugLevel())); | ||
117 | return true; | ||
118 | } | ||
119 | |||
120 | bool printCommandTiming(const QStringList &, State &state) | ||
121 | { | ||
122 | state.printLine(state.commandTiming() ? QObject::tr("on") : QObject::tr("off")); | ||
123 | return true; | ||
124 | } | ||
125 | |||
126 | void printSyntaxBranch(State &state, const Syntax::List &list, int depth) | ||
127 | { | ||
128 | if (list.isEmpty()) { | ||
129 | return; | ||
130 | } | ||
131 | |||
132 | if (depth > 0) { | ||
133 | state.printLine("\\", depth); | ||
134 | } | ||
135 | |||
136 | for (auto syntax: list) { | ||
137 | state.print("|-", depth); | ||
138 | state.printLine(syntax.keyword); | ||
139 | printSyntaxBranch(state, syntax.children, depth + 1); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | bool printSyntaxTree(const QStringList &, State &state) | ||
144 | { | ||
145 | printSyntaxBranch(state, SyntaxTree::self()->syntax(), 0); | ||
146 | return true; | ||
147 | } | ||
148 | |||
149 | bool setLoggingLevel(const QStringList &commands, State &state) | ||
150 | { | ||
151 | if (commands.count() != 1) { | ||
152 | state.printError(QObject::tr("Wrong number of arguments; expected 1 got %1").arg(commands.count())); | ||
153 | return false; | ||
154 | } | ||
155 | |||
156 | state.setLoggingLevel(commands.at(0)); | ||
157 | return true; | ||
158 | } | ||
159 | |||
160 | bool printLoggingLevel(const QStringList &commands, State &state) | ||
161 | { | ||
162 | const QString level = state.loggingLevel(); | ||
163 | state.printLine(level); | ||
164 | return true; | ||
165 | } | ||
166 | |||
167 | Syntax::List syntax() | ||
168 | { | ||
169 | Syntax::List syntax; | ||
170 | syntax << Syntax("exit", QObject::tr("Exits the application. Ctrl-d also works!"), &CoreSyntax::exit); | ||
171 | |||
172 | Syntax help("help", QObject::tr("Print command information: help [command]"), &CoreSyntax::showHelp); | ||
173 | help.completer = &CoreSyntax::showHelpCompleter; | ||
174 | syntax << help; | ||
175 | |||
176 | syntax << Syntax("syntaxtree", QString(), &printSyntaxTree); | ||
177 | |||
178 | Syntax set("set", QObject::tr("Sets settings for the session")); | ||
179 | set.children << Syntax("debug", QObject::tr("Set the debug level from 0 to 6"), &CoreSyntax::setDebugLevel); | ||
180 | |||
181 | Syntax setTiming = Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete")); | ||
182 | setTiming.children << Syntax("on", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(true); return true; }); | ||
183 | setTiming.children << Syntax("off", QString(), [](const QStringList &, State &state) -> bool { state.setCommandTiming(false); return true; }); | ||
184 | set.children << setTiming; | ||
185 | |||
186 | Syntax logging("logging", QObject::tr("Set the logging level to one of Trace, Log, Warning or Error"), &CoreSyntax::setLoggingLevel); | ||
187 | logging.completer = [](const QStringList &, const QString &fragment, State &state) -> QStringList { return Utils::filteredCompletions(QStringList() << "trace" << "log" << "warning" << "error", fragment, Qt::CaseInsensitive); }; | ||
188 | set.children << logging; | ||
189 | |||
190 | syntax << set; | ||
191 | |||
192 | Syntax get("get", QObject::tr("Gets settings for the session")); | ||
193 | get.children << Syntax("debug", QObject::tr("The current debug level from 0 to 6"), &CoreSyntax::printDebugLevel); | ||
194 | get.children << Syntax("timing", QObject::tr("Whether or not to print the time commands take to complete"), &CoreSyntax::printCommandTiming); | ||
195 | get.children << Syntax("logging", QObject::tr("The current logging level"), &CoreSyntax::printLoggingLevel); | ||
196 | syntax << get; | ||
197 | |||
198 | return syntax; | ||
199 | } | ||
200 | |||
201 | REGISTER_SYNTAX(CoreSyntax) | ||
202 | |||
203 | } // namespace CoreSyntax | ||
204 | |||