summaryrefslogtreecommitdiffstats
path: root/akonadi2_cli/module.cpp
diff options
context:
space:
mode:
authorAaron Seigo <aseigo@kde.org>2015-12-23 11:43:37 +0100
committerAaron Seigo <aseigo@kde.org>2015-12-23 11:43:37 +0100
commit071f4ef0122a8bfceeda9a10b41e85ad9a34a28d (patch)
treee5d1cb7efcac5eb16f4848619ff06142906b6cea /akonadi2_cli/module.cpp
parent255d73af197faf8437343abc10bd98cca2057a1e (diff)
downloadsink-071f4ef0122a8bfceeda9a10b41e85ad9a34a28d.tar.gz
sink-071f4ef0122a8bfceeda9a10b41e85ad9a34a28d.zip
vastly simplify by getting rid of Module as a base class
just a move slightly more towards functional
Diffstat (limited to 'akonadi2_cli/module.cpp')
-rw-r--r--akonadi2_cli/module.cpp116
1 files changed, 41 insertions, 75 deletions
diff --git a/akonadi2_cli/module.cpp b/akonadi2_cli/module.cpp
index 0b83d98..dfc58c8 100644
--- a/akonadi2_cli/module.cpp
+++ b/akonadi2_cli/module.cpp
@@ -24,83 +24,61 @@
24 24
25// TODO: needs a proper registry; making "core" modules plugins is 25// TODO: needs a proper registry; making "core" modules plugins is
26// almost certainly overkill, but this is not the way either 26// almost certainly overkill, but this is not the way either
27#include "modules/exit/exit.h" 27#include "modules/core_syntax.h"
28#include "modules/help/help.h"
29 28
30QList<Module> Module::s_modules; 29Module *Module::s_module = 0;
31State Module::s_state;
32 30
33Module::Syntax::Syntax() 31Module::Syntax::Syntax()
34{ 32{
35} 33}
36 34
37Module::Syntax::Syntax(const QString &k, std::function<bool(const QStringList &, State &)> l, const QString &helpText, bool e) 35Module::Syntax::Syntax(const QString &k, const QString &helpText, std::function<bool(const QStringList &, State &)> l, Interactivity inter)
38 : keyword(k), 36 : keyword(k),
39 lambda(l),
40 help(helpText), 37 help(helpText),
41 eventDriven(e) 38 interactivity(inter),
39 lambda(l)
42{ 40{
43} 41}
44 42
45Module::Module() 43Module::Module()
46{ 44{
45 QVector<std::function<SyntaxList()> > syntaxModules;
46 syntaxModules << &CoreSyntax::syntax;
47 for (auto syntaxModule: syntaxModules) {
48 m_syntax += syntaxModule();
49 }
47} 50}
48 51
49void Module::loadModules() 52Module *Module::self()
50{
51 addModule(CLI::Exit());
52 addModule(CLI::Help());
53}
54
55void Module::addModule(const Module &module)
56{
57 s_modules.append(module);
58}
59
60QList<Module> Module::modules()
61{
62 return s_modules;
63}
64
65Module::Command Module::match(const QStringList &commands)
66{ 53{
67 Command command; 54 if (!s_module) {
68 for (const Module &module: s_modules) { 55 s_module = new Module;
69 command = module.matches(commands);
70 if (command.first) {
71 break;
72 }
73 } 56 }
74 57
75 return command; 58 return s_module;
76} 59}
77 60
78Module::Syntax Module::syntax() const 61Module::SyntaxList Module::syntax() const
79{ 62{
80 return m_syntax; 63 return m_syntax;
81} 64}
82 65
83void Module::setSyntax(const Syntax &syntax)
84{
85 m_syntax = syntax;
86}
87
88bool Module::run(const QStringList &commands) 66bool Module::run(const QStringList &commands)
89{ 67{
90 Command command = match(commands); 68 Command command = match(commands);
91 if (command.first && command.first->lambda) { 69 if (command.first && command.first->lambda) {
92 bool rv = command.first->lambda(command.second, s_state); 70 command.first->lambda(command.second, m_state);
93 if (rv && command.first->eventDriven) { 71 if (command.first->interactivity == Syntax::EventDriven) {
94 return QCoreApplication::instance()->exec(); 72 return QCoreApplication::instance()->exec();
95 } 73 }
96 74
97 return rv; 75 return true;
98 } 76 }
99 77
100 return false; 78 return false;
101} 79}
102 80
103Module::Command Module::matches(const QStringList &commandLine) const 81Module::Command Module::match(const QStringList &commandLine) const
104{ 82{
105 if (commandLine.isEmpty()) { 83 if (commandLine.isEmpty()) {
106 return Command(); 84 return Command();
@@ -108,20 +86,16 @@ Module::Command Module::matches(const QStringList &commandLine) const
108 86
109 QStringListIterator commandLineIt(commandLine); 87 QStringListIterator commandLineIt(commandLine);
110 88
111 if (commandLineIt.next() != m_syntax.keyword) { 89 QVectorIterator<Syntax> syntaxIt(m_syntax);
112 return Command(); 90 const Syntax *lastFullSyntax = 0;
113 }
114
115 QListIterator<Syntax> syntaxIt(m_syntax.children);
116 const Syntax *syntax = &m_syntax;
117 QStringList tailCommands; 91 QStringList tailCommands;
118 while (commandLineIt.hasNext() && syntaxIt.hasNext()) { 92 while (commandLineIt.hasNext() && syntaxIt.hasNext()) {
119 const QString word = commandLineIt.next(); 93 const QString word = commandLineIt.next();
120 while (syntaxIt.hasNext()) { 94 while (syntaxIt.hasNext()) {
121 const Syntax &child = syntaxIt.next(); 95 const Syntax &syntax = syntaxIt.next();
122 if (word == child.keyword) { 96 if (word == syntax.keyword) {
123 syntax = &child; 97 lastFullSyntax = &syntax;
124 syntaxIt = child.children; 98 syntaxIt = syntax.children;
125 } 99 }
126 } 100 }
127 101
@@ -131,55 +105,47 @@ Module::Command Module::matches(const QStringList &commandLine) const
131 } 105 }
132 } 106 }
133 107
134 if (syntax && syntax->lambda) { 108 if (lastFullSyntax && lastFullSyntax->lambda) {
135 while (commandLineIt.hasNext()) { 109 while (commandLineIt.hasNext()) {
136 tailCommands << commandLineIt.next(); 110 tailCommands << commandLineIt.next();
137 } 111 }
138 112
139 return std::make_pair(syntax, tailCommands); 113 return std::make_pair(lastFullSyntax, tailCommands);
140 } 114 }
141 115
142 return Command(); 116 return Command();
143} 117}
144 118
145QVector<Module::Syntax> Module::nearestSyntax(const QStringList &words, const QString &fragment) 119Module::SyntaxList Module::nearestSyntax(const QStringList &words, const QString &fragment) const
146{ 120{
147 QVector<Module::Syntax> matches; 121 SyntaxList matches;
148 122
149 //qDebug() << "words are" << words; 123 //qDebug() << "words are" << words;
150 if (words.isEmpty()) { 124 if (words.isEmpty()) {
151 for (const Module &module: s_modules) { 125 for (const Syntax &syntax: m_syntax) {
152 if (module.syntax().keyword.startsWith(fragment)) { 126 if (syntax.keyword.startsWith(fragment)) {
153 matches.push_back(module.syntax()); 127 matches.push_back(syntax);
154 } 128 }
155 } 129 }
156 } else { 130 } else {
157 QStringListIterator wordIt(words); 131 QStringListIterator wordIt(words);
158 QString word = wordIt.next(); 132 QVectorIterator<Syntax> syntaxIt(m_syntax);
159 Syntax lastFullSyntax; 133 Syntax lastFullSyntax;
160 134
161 for (const Module &module: s_modules) { 135 while (wordIt.hasNext()) {
162 if (module.syntax().keyword == word) { 136 QString word = wordIt.next();
163 lastFullSyntax = module.syntax(); 137 while (syntaxIt.hasNext()) {
164 QListIterator<Syntax> syntaxIt(module.syntax().children); 138 const Syntax &syntax = syntaxIt.next();
165 while (wordIt.hasNext()) { 139 if (word == syntax.keyword) {
166 word = wordIt.next(); 140 lastFullSyntax = syntax;
167 while (syntaxIt.hasNext()) { 141 syntaxIt = syntax.children;
168 const Syntax &child = syntaxIt.next();
169 if (word == child.keyword) {
170 lastFullSyntax = child;
171 syntaxIt = child.children;
172 }
173 }
174 } 142 }
175
176 break;
177 } 143 }
178 } 144 }
179 145
180 //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last(); 146 //qDebug() << "exiting with" << lastFullSyntax.keyword << words.last();
181 if (lastFullSyntax.keyword == words.last()) { 147 if (lastFullSyntax.keyword == words.last()) {
182 QListIterator<Syntax> syntaxIt(lastFullSyntax.children); 148 syntaxIt = lastFullSyntax.children;
183 while (syntaxIt.hasNext()) { 149 while (syntaxIt.hasNext()) {
184 Syntax syntax = syntaxIt.next(); 150 Syntax syntax = syntaxIt.next();
185 if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) { 151 if (fragment.isEmpty() || syntax.keyword.startsWith(fragment)) {