From 31e5c13de4daa6bb9c9a777d098bc37dae0485f9 Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Tue, 28 Apr 2015 15:10:00 +0200 Subject: [PATCH] jsconsole read nested objects --- eth/main.cpp | 5 ----- libjsconsole/JSConsole.cpp | 36 ++++++++++++++++++++++++++++++------ libjsconsole/JSConsole.h | 4 +++- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index 5f1c464f3..a31e7af3c 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -407,11 +407,6 @@ void doFarm(MinerType _m, string const& _remote, unsigned _recheckPeriod) int main(int argc, char** argv) { - JSConsole console; - console.repl(); - console.repl(); - console.repl(); - console.repl(); cout << "\x1b[30mEthBlack\x1b[0m" << endl; cout << "\x1b[90mEthCoal\x1b[0m" << endl; cout << "\x1b[37mEthGray\x1b[0m" << endl; diff --git a/libjsconsole/JSConsole.cpp b/libjsconsole/JSConsole.cpp index 4ed6f6329..8cbc91d54 100644 --- a/libjsconsole/JSConsole.cpp +++ b/libjsconsole/JSConsole.cpp @@ -3,6 +3,7 @@ // #include +#include #include #include "JSConsole.h" @@ -14,19 +15,42 @@ using namespace std; using namespace dev; using namespace dev::eth; -int JSConsole::repl() const +void JSConsole::repl() const { string cmd = ""; g_logPost = [](std::string const& a, char const*) { cout << "\r \r" << a << endl << flush; rl_forced_update_display(); }; - char* c = readline("> "); - if (c && *c) + bool isEmpty = true; + int openBrackets = 0; + do { + char* buff = readline(promptForIndentionLevel(openBrackets).c_str()); + isEmpty = !(buff && *buff); + if (!isEmpty) + { + cmd += string(buff); + cmd += " "; + free(buff); + int open = count(cmd.begin(), cmd.end(), '{'); + open += count(cmd.begin(), cmd.end(), '('); + int closed = count(cmd.begin(), cmd.end(), '}'); + closed += count(cmd.begin(), cmd.end(), ')'); + openBrackets = open - closed; + } + } while (openBrackets > 0); + + if (!isEmpty) { - cmd = string(c); - add_history(c); + add_history(cmd.c_str()); auto value = m_engine.eval(cmd.c_str()); string result = m_printer.print(value); - free(c); cout << result << endl; } } + +std::string JSConsole::promptForIndentionLevel(int _i) const +{ + if (_i == 0) + return "> "; + + return string((_i + 1) * 2, ' '); +} diff --git a/libjsconsole/JSConsole.h b/libjsconsole/JSConsole.h index 20ce986a2..216813a01 100644 --- a/libjsconsole/JSConsole.h +++ b/libjsconsole/JSConsole.h @@ -16,9 +16,11 @@ class JSConsole { public: JSConsole(): m_engine(), m_printer(m_engine) {} - int repl() const; + void repl() const; private: + std::string promptForIndentionLevel(int _i) const; + JSV8Engine m_engine; JSV8Printer m_printer; };