summaryrefslogtreecommitdiffstats
path: root/sinksh
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2018-05-27 10:12:29 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-05-27 10:12:29 +0200
commit314909776db599b2a90fecde6e85c9874768efeb (patch)
tree4e6c1f16be1271fba4b267bfbff4e6c8a31cbeed /sinksh
parent01002ca7602b8d8a569c6d7191a1ba1ad03f3e65 (diff)
downloadsink-314909776db599b2a90fecde6e85c9874768efeb.tar.gz
sink-314909776db599b2a90fecde6e85c9874768efeb.zip
A wrapper for linenoise
Because global variables in header files don't work if included from multiple places.
Diffstat (limited to 'sinksh')
-rw-r--r--sinksh/CMakeLists.txt1
-rw-r--r--sinksh/repl/commandline.cpp46
-rw-r--r--sinksh/repl/commandline.h36
-rw-r--r--sinksh/repl/repl.cpp6
-rw-r--r--sinksh/repl/replStates.cpp8
5 files changed, 90 insertions, 7 deletions
diff --git a/sinksh/CMakeLists.txt b/sinksh/CMakeLists.txt
index cfff36c..3154b0c 100644
--- a/sinksh/CMakeLists.txt
+++ b/sinksh/CMakeLists.txt
@@ -22,6 +22,7 @@ set(sink_cli_SRCS
22 sinksh_utils.cpp 22 sinksh_utils.cpp
23 repl/repl.cpp 23 repl/repl.cpp
24 repl/replStates.cpp 24 repl/replStates.cpp
25 repl/commandline.cpp
25 state.cpp 26 state.cpp
26 utils.cpp) 27 utils.cpp)
27 28
diff --git a/sinksh/repl/commandline.cpp b/sinksh/repl/commandline.cpp
new file mode 100644
index 0000000..443cf3b
--- /dev/null
+++ b/sinksh/repl/commandline.cpp
@@ -0,0 +1,46 @@
1/*
2 * Copyright (C) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
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#include "commandline.h"
20
21#include "linenoise.hpp"
22
23void Commandline::loadHistory(const QString &path)
24{
25 linenoise::LoadHistory(path.toLocal8Bit());
26}
27
28void Commandline::saveHistory(const QString &path)
29{
30 linenoise::SaveHistory(path.toLocal8Bit());
31}
32
33void Commandline::addHistory(const std::string &line)
34{
35 linenoise::AddHistory(line.c_str());
36}
37
38void Commandline::setCompletionCallback(std::function<void (const char*, std::vector<std::string>&)> callback)
39{
40 linenoise::SetCompletionCallback(callback);
41}
42
43bool Commandline::readline(const char *prompt, std::string &line)
44{
45 return linenoise::Readline(prompt, line);
46}
diff --git a/sinksh/repl/commandline.h b/sinksh/repl/commandline.h
new file mode 100644
index 0000000..dbd6bd4
--- /dev/null
+++ b/sinksh/repl/commandline.h
@@ -0,0 +1,36 @@
1/*
2 * Copyright (C) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
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#pragma once
20
21#include <QString>
22#include <functional>
23#include <vector>
24
25/*
26 * Wrapper for linenoise.
27 *
28 * Because global variables in header files don't work when included from multiple places.
29 */
30namespace Commandline {
31 void loadHistory(const QString &);
32 void saveHistory(const QString &);
33 void addHistory(const std::string &);
34 void setCompletionCallback(std::function<void (const char*, std::vector<std::string>&)>);
35 bool readline(const char *prompt, std::string &line);
36};
diff --git a/sinksh/repl/repl.cpp b/sinksh/repl/repl.cpp
index 32932cb..c8f9b45 100644
--- a/sinksh/repl/repl.cpp
+++ b/sinksh/repl/repl.cpp
@@ -27,12 +27,12 @@
27 27
28#include "replStates.h" 28#include "replStates.h"
29#include "syntaxtree.h" 29#include "syntaxtree.h"
30#include "linenoise.hpp" 30#include "commandline.h"
31 31
32Repl::Repl(QObject *parent) 32Repl::Repl(QObject *parent)
33 : QStateMachine(parent) 33 : QStateMachine(parent)
34{ 34{
35 linenoise::LoadHistory(commandHistoryPath().toLocal8Bit()); 35 Commandline::loadHistory(commandHistoryPath());
36 36
37 // create all states 37 // create all states
38 ReadState *read = new ReadState(this); 38 ReadState *read = new ReadState(this);
@@ -61,7 +61,7 @@ Repl::Repl(QObject *parent)
61 61
62Repl::~Repl() 62Repl::~Repl()
63{ 63{
64 linenoise::SaveHistory(commandHistoryPath().toLocal8Bit()); 64 Commandline::saveHistory(commandHistoryPath());
65} 65}
66 66
67void Repl::printWelcomeBanner() 67void Repl::printWelcomeBanner()
diff --git a/sinksh/repl/replStates.cpp b/sinksh/repl/replStates.cpp
index c4b08b7..38ebdf2 100644
--- a/sinksh/repl/replStates.cpp
+++ b/sinksh/repl/replStates.cpp
@@ -24,14 +24,14 @@
24#include <QDebug> 24#include <QDebug>
25#include <QEvent> 25#include <QEvent>
26#include <QStateMachine> 26#include <QStateMachine>
27#include "linenoise.hpp"
28 27
28#include "commandline.h"
29#include "syntaxtree.h" 29#include "syntaxtree.h"
30 30
31ReadState::ReadState(QState *parent) 31ReadState::ReadState(QState *parent)
32 : QState(parent) 32 : QState(parent)
33{ 33{
34 linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) { 34 Commandline::setCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
35 QStringList words = QString(editBuffer).split(" ", QString::SkipEmptyParts); 35 QStringList words = QString(editBuffer).split(" ", QString::SkipEmptyParts);
36 const QString fragment = words.takeLast(); 36 const QString fragment = words.takeLast();
37 Syntax::List nearest = SyntaxTree::self()->nearestSyntax(words, fragment); 37 Syntax::List nearest = SyntaxTree::self()->nearestSyntax(words, fragment);
@@ -56,7 +56,7 @@ void ReadState::onEntry(QEvent *event)
56 Q_UNUSED(event) 56 Q_UNUSED(event)
57 57
58 std::string line; 58 std::string line;
59 if (linenoise::Readline(prompt(), line)) { 59 if (Commandline::readline(prompt(), line)) {
60 std::cout << std::endl; 60 std::cout << std::endl;
61 emit exitRequested(); 61 emit exitRequested();
62 return; 62 return;
@@ -66,7 +66,7 @@ void ReadState::onEntry(QEvent *event)
66 const QString text = QString::fromStdString(line).simplified(); 66 const QString text = QString::fromStdString(line).simplified();
67 67
68 if (text.length() > 0) { 68 if (text.length() > 0) {
69 linenoise::AddHistory(line.c_str()); 69 Commandline::addHistory(line);
70 } 70 }
71 71
72 emit command(text); 72 emit command(text);