diff --git a/libjsconsole/CMakeLists.txt b/libjsconsole/CMakeLists.txt index 761435fe1..f2bd8ac02 100644 --- a/libjsconsole/CMakeLists.txt +++ b/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}) diff --git a/libjsconsole/JSConsole.h b/libjsconsole/JSConsole.h index 50f6d6ae5..8042dd6bb 100644 --- a/libjsconsole/JSConsole.h +++ b/libjsconsole/JSConsole.h @@ -23,9 +23,11 @@ #pragma once #include -// TODO! make readline optional! + +#if ETH_READLINE #include #include +#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;