Browse Source

readline optional for jsconsole

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
19199e47d6
  1. 4
      libjsconsole/CMakeLists.txt
  2. 28
      libjsconsole/JSConsole.h

4
libjsconsole/CMakeLists.txt

@ -24,7 +24,9 @@ add_library(${EXECUTABLE} ${SRC_LIST} ${HEADERS})
target_link_libraries(${EXECUTABLE} jsengine)
target_link_libraries(${EXECUTABLE} devcore)
target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES})
if (READLINE_FOUND)
target_link_libraries(${EXECUTABLE} ${READLINE_LIBRARIES})
endif()
target_link_libraries(${EXECUTABLE} ${JSON_RPC_CPP_SERVER_LIBRARIES})
target_link_libraries(${EXECUTABLE} ${CURL_LIBRARIES})

28
libjsconsole/JSConsole.h

@ -23,9 +23,11 @@
#pragma once
#include <libdevcore/Log.h>
// TODO! make readline optional!
#if ETH_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
namespace dev
{
@ -42,18 +44,34 @@ public:
void readExpression() const
{
std::string cmd = "";
g_logPost = [](std::string const& a, char const*) { std::cout << "\r \r" << a << std::endl << std::flush; rl_forced_update_display(); };
g_logPost = [](std::string const& a, char const*)
{
std::cout << "\r \r" << a << std::endl << std::flush;
#if ETH_READLINE
rl_forced_update_display();
#endif
};
bool isEmpty = true;
int openBrackets = 0;
do {
std::string rl;
#if ETH_READLINE
char* buff = readline(promptForIndentionLevel(openBrackets).c_str());
isEmpty = !(buff && *buff);
if (!isEmpty)
{
cmd += std::string(buff);
cmd += " ";
rl = std::string(buff);
free(buff);
}
#else
std::cout << propmtForIndentionLevel(openBrackets) << std::endl << std::flush;
std::getline(std::cin, rl);
#endif
if (rl.length() > 0)
{
cmd += rl;
cmd += " ";
int open = std::count(cmd.begin(), cmd.end(), '{');
open += std::count(cmd.begin(), cmd.end(), '(');
int closed = std::count(cmd.begin(), cmd.end(), '}');
@ -64,7 +82,9 @@ public:
if (!isEmpty)
{
#if ETH_READLINE
add_history(cmd.c_str());
#endif
auto value = m_engine.eval(cmd.c_str());
std::string result = m_printer.prettyPrint(value).cstr();
std::cout << result << std::endl;

Loading…
Cancel
Save