diff options
Diffstat (limited to 'akonadi2_cli/module.cpp')
-rw-r--r-- | akonadi2_cli/module.cpp | 116 |
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 | ||
30 | QList<Module> Module::s_modules; | 29 | Module *Module::s_module = 0; |
31 | State Module::s_state; | ||
32 | 30 | ||
33 | Module::Syntax::Syntax() | 31 | Module::Syntax::Syntax() |
34 | { | 32 | { |
35 | } | 33 | } |
36 | 34 | ||
37 | Module::Syntax::Syntax(const QString &k, std::function<bool(const QStringList &, State &)> l, const QString &helpText, bool e) | 35 | Module::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 | ||
45 | Module::Module() | 43 | Module::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 | ||
49 | void Module::loadModules() | 52 | Module *Module::self() |
50 | { | ||
51 | addModule(CLI::Exit()); | ||
52 | addModule(CLI::Help()); | ||
53 | } | ||
54 | |||
55 | void Module::addModule(const Module &module) | ||
56 | { | ||
57 | s_modules.append(module); | ||
58 | } | ||
59 | |||
60 | QList<Module> Module::modules() | ||
61 | { | ||
62 | return s_modules; | ||
63 | } | ||
64 | |||
65 | Module::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 | ||
78 | Module::Syntax Module::syntax() const | 61 | Module::SyntaxList Module::syntax() const |
79 | { | 62 | { |
80 | return m_syntax; | 63 | return m_syntax; |
81 | } | 64 | } |
82 | 65 | ||
83 | void Module::setSyntax(const Syntax &syntax) | ||
84 | { | ||
85 | m_syntax = syntax; | ||
86 | } | ||
87 | |||
88 | bool Module::run(const QStringList &commands) | 66 | bool 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 | ||
103 | Module::Command Module::matches(const QStringList &commandLine) const | 81 | Module::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 | ||
145 | QVector<Module::Syntax> Module::nearestSyntax(const QStringList &words, const QString &fragment) | 119 | Module::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)) { |