summaryrefslogtreecommitdiffstats
path: root/akonadish/repl/replStates.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-26 19:19:11 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2015-12-26 19:19:11 +0100
commit8ef8b8dee60aa0a81fbb2cbba30c00d3b15bb771 (patch)
tree06be3618505d45e6e2886fb614c96689a5555364 /akonadish/repl/replStates.cpp
parentfc7052f0970465d41dfd67c7e5db080498fd060f (diff)
parent67a19008d078a067ceb3424c00553c33b918970e (diff)
downloadsink-8ef8b8dee60aa0a81fbb2cbba30c00d3b15bb771.tar.gz
sink-8ef8b8dee60aa0a81fbb2cbba30c00d3b15bb771.zip
Merge branch 'feature/new_cli' into develop
Diffstat (limited to 'akonadish/repl/replStates.cpp')
-rw-r--r--akonadish/repl/replStates.cpp171
1 files changed, 171 insertions, 0 deletions
diff --git a/akonadish/repl/replStates.cpp b/akonadish/repl/replStates.cpp
new file mode 100644
index 0000000..62888d0
--- /dev/null
+++ b/akonadish/repl/replStates.cpp
@@ -0,0 +1,171 @@
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 "replStates.h"
21
22#include <unistd.h>
23#include <iostream>
24
25#include <readline/readline.h>
26#include <readline/history.h>
27
28#include <QDebug>
29#include <QEvent>
30#include <QStateMachine>
31
32#include "syntaxtree.h"
33
34static char *akonadi2_cli_next_tab_complete_match(const char *text, int state);
35static char ** akonadi2_cli_tab_completion(const char *text, int start, int end);
36
37ReadState::ReadState(QState *parent)
38 : QState(parent)
39{
40 rl_completion_entry_function = akonadi2_cli_next_tab_complete_match;
41 rl_attempted_completion_function = akonadi2_cli_tab_completion;
42}
43
44void ReadState::onEntry(QEvent *event)
45{
46 Q_UNUSED(event)
47 char *line = readline(prompt());
48
49 if (!line) {
50 std::cout << std::endl;
51 emit exitRequested();
52 return;
53 }
54
55 // we have actual data, so let's wait for a full line of text
56 QByteArray input(line);
57 const QString text = QString(line).simplified();
58 //qDebug() << "Line is ... " << text;
59
60 if (text.length() > 0) {
61 add_history(line);
62 }
63
64 free(line);
65 emit command(text);
66}
67
68const char *ReadState::prompt() const
69{
70 return "> ";
71}
72
73UnfinishedReadState::UnfinishedReadState(QState *parent)
74 : ReadState(parent)
75{
76}
77
78const char *UnfinishedReadState::prompt() const
79{
80 return " ";
81}
82
83EvalState::EvalState(QState *parent)
84 : QState(parent)
85{
86}
87
88void EvalState::onEntry(QEvent *event)
89{
90 QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event);
91
92 const QString command = e ? e->arguments()[0].toString() : QString();
93
94 if (command.isEmpty()) {
95 complete();
96 return;
97 }
98
99 if (command.right(1) == "\\") {
100 m_partial += " " + command.left(command.size() - 1);
101 continueInput();
102 } else {
103 m_partial += " " + command;
104 complete();
105 }
106}
107
108void EvalState::complete()
109{
110 m_partial = m_partial.simplified();
111
112 if (!m_partial.isEmpty()) {
113 //emit output("Processing ... " + command);
114 const QStringList commands = SyntaxTree::tokenize(m_partial);
115 SyntaxTree::self()->run(commands);
116 m_partial.clear();
117 }
118
119 emit completed();
120}
121
122PrintState::PrintState(QState *parent)
123 : QState(parent)
124{
125}
126
127void PrintState::onEntry(QEvent *event)
128{
129 QStateMachine::SignalEvent *e = dynamic_cast<QStateMachine::SignalEvent*>(event);
130
131 if (e && !e->arguments().isEmpty()) {
132 const QString command = e->arguments()[0].toString();
133 QTextStream stream(stdout);
134 stream << command << "\n";
135 }
136
137 emit completed();
138}
139
140static QStringList tab_completion_full_state;
141static bool tab_completion_at_root = false;
142
143static char **akonadi2_cli_tab_completion(const char *text, int start, int end)
144{
145 tab_completion_at_root = start == 0;
146 tab_completion_full_state = QString(rl_line_buffer).remove(start, end - start).split(" ", QString::SkipEmptyParts);
147 return NULL;
148}
149
150static char *akonadi2_cli_next_tab_complete_match(const char *text, int state)
151{
152 const QString fragment(text);
153 Syntax::List nearest = SyntaxTree::self()->nearestSyntax(tab_completion_full_state, fragment);
154 //for (auto syntax: nearest) { qDebug() << "Nearest: " << syntax.keyword; }
155
156 if (nearest.isEmpty()) {
157 SyntaxTree::Command command = SyntaxTree::self()->match(tab_completion_full_state);
158 if (command.first && command.first->completer) {
159 QStringList commandCompletions = command.first->completer(tab_completion_full_state, fragment);
160 if (commandCompletions.size() > state) {
161 return qstrdup(commandCompletions[state].toUtf8());
162 }
163 }
164 } else if (nearest.size() > state) {
165 return qstrdup(nearest[state].keyword.toUtf8());
166 }
167
168 return rl_filename_completion_function(text, state);
169}
170
171#include "moc_replStates.cpp"