Browse Source

jsconsole read nested objects

cl-refactor
Marek Kotewicz 10 years ago
parent
commit
31e5c13de4
  1. 5
      eth/main.cpp
  2. 36
      libjsconsole/JSConsole.cpp
  3. 4
      libjsconsole/JSConsole.h

5
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;

36
libjsconsole/JSConsole.cpp

@ -3,6 +3,7 @@
//
#include <iostream>
#include <algorithm>
#include <libdevcore/Log.h>
#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, ' ');
}

4
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;
};

Loading…
Cancel
Save