From 314909776db599b2a90fecde6e85c9874768efeb Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sun, 27 May 2018 10:12:29 +0200 Subject: A wrapper for linenoise Because global variables in header files don't work if included from multiple places. --- sinksh/CMakeLists.txt | 1 + sinksh/repl/commandline.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++ sinksh/repl/commandline.h | 36 +++++++++++++++++++++++++++++++++++ sinksh/repl/repl.cpp | 6 +++--- sinksh/repl/replStates.cpp | 8 ++++---- 5 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 sinksh/repl/commandline.cpp create mode 100644 sinksh/repl/commandline.h 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 sinksh_utils.cpp repl/repl.cpp repl/replStates.cpp + repl/commandline.cpp state.cpp utils.cpp) 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 @@ +/* + * Copyright (C) 2018 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#include "commandline.h" + +#include "linenoise.hpp" + +void Commandline::loadHistory(const QString &path) +{ + linenoise::LoadHistory(path.toLocal8Bit()); +} + +void Commandline::saveHistory(const QString &path) +{ + linenoise::SaveHistory(path.toLocal8Bit()); +} + +void Commandline::addHistory(const std::string &line) +{ + linenoise::AddHistory(line.c_str()); +} + +void Commandline::setCompletionCallback(std::function&)> callback) +{ + linenoise::SetCompletionCallback(callback); +} + +bool Commandline::readline(const char *prompt, std::string &line) +{ + return linenoise::Readline(prompt, line); +} 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 @@ +/* + * Copyright (C) 2018 Christian Mollekopf + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ +#pragma once + +#include +#include +#include + +/* + * Wrapper for linenoise. + * + * Because global variables in header files don't work when included from multiple places. + */ +namespace Commandline { + void loadHistory(const QString &); + void saveHistory(const QString &); + void addHistory(const std::string &); + void setCompletionCallback(std::function&)>); + bool readline(const char *prompt, std::string &line); +}; 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 @@ #include "replStates.h" #include "syntaxtree.h" -#include "linenoise.hpp" +#include "commandline.h" Repl::Repl(QObject *parent) : QStateMachine(parent) { - linenoise::LoadHistory(commandHistoryPath().toLocal8Bit()); + Commandline::loadHistory(commandHistoryPath()); // create all states ReadState *read = new ReadState(this); @@ -61,7 +61,7 @@ Repl::Repl(QObject *parent) Repl::~Repl() { - linenoise::SaveHistory(commandHistoryPath().toLocal8Bit()); + Commandline::saveHistory(commandHistoryPath()); } void 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 @@ #include #include #include -#include "linenoise.hpp" +#include "commandline.h" #include "syntaxtree.h" ReadState::ReadState(QState *parent) : QState(parent) { - linenoise::SetCompletionCallback([](const char* editBuffer, std::vector& completions) { + Commandline::setCompletionCallback([](const char* editBuffer, std::vector& completions) { QStringList words = QString(editBuffer).split(" ", QString::SkipEmptyParts); const QString fragment = words.takeLast(); Syntax::List nearest = SyntaxTree::self()->nearestSyntax(words, fragment); @@ -56,7 +56,7 @@ void ReadState::onEntry(QEvent *event) Q_UNUSED(event) std::string line; - if (linenoise::Readline(prompt(), line)) { + if (Commandline::readline(prompt(), line)) { std::cout << std::endl; emit exitRequested(); return; @@ -66,7 +66,7 @@ void ReadState::onEntry(QEvent *event) const QString text = QString::fromStdString(line).simplified(); if (text.length() > 0) { - linenoise::AddHistory(line.c_str()); + Commandline::addHistory(line); } emit command(text); -- cgit v1.2.3