crconsole icon indicating copy to clipboard operation
crconsole copied to clipboard

Support multi line commands (was: Input a scope in crconsole)

Open ShawnHuang opened this issue 12 years ago • 9 comments

I think it would be better if we can type a scope in crconsole. Just like typing in chrome dev tool and chrome-remote-interface's repl.

02

03

ShawnHuang avatar Oct 28 '13 03:10 ShawnHuang

not sure if I get the idea. Are you talking about multi line input?

sidorares avatar Oct 28 '13 04:10 sidorares

Chrome dev tool's rule is multi line input. I think it's difficult to implement. Chrome-remote-interface's repl used repl module, and its rule that I guess is to detect some keywords (function, if, while) and to let users to complete code scope. I can't differ crconsole which also use repl module from that, but I try to write a sample with the repl module that also has the same feature.

ShawnHuang avatar Oct 28 '13 06:10 ShawnHuang

multiline input would be tricky with repl/readline. Node starts 'multiline mode' when there is syntax error - see https://github.com/joyent/node/blob/master/lib/repl.js#L296

Not sure if that is desired behaviour at all. Maybe we should trigger this only by some errors, like "Unexpected end of input"

sidorares avatar Oct 28 '13 07:10 sidorares

OK, I got it. Thanks for your reply

ShawnHuang avatar Oct 28 '13 08:10 ShawnHuang

Feel free to try to implement it yourself :)

sidorares avatar Oct 28 '13 08:10 sidorares

I will try it:)

ShawnHuang avatar Oct 28 '13 15:10 ShawnHuang

I try to replace the eval function with the following code, but I am not sure that there is no bugs at that code. :)

https://github.com/sidorares/crconsole/blob/master/index.js#L272

    var err, result;                                                                                                            
    try{                                                                                                                        
      var script = require("vm").createScript(cmd.slice(1, -2), {                                                                            
        filename: filename,                                                                                                     
        displayErrors: false                                                                                                    
      });                                                                                                                       
    } catch (e){                                                                                                                
      err = e;                                                                                                                  
    }                                                                                                                           
    var mess = !!err;                                                                                                           
    if(mess) cb(err, result);                                                                                                   
    else{                                                                                                                       
      this.client.Runtime.evaluate({expression: cmd.slice(1, -2), generatePreview: true}, function(err, resp) {                              
        return cb(null, resp);                                                                                                  
      });                                                                                                                       
    }

ShawnHuang avatar Oct 29 '13 04:10 ShawnHuang

It will throw exceptions for some valid code, for example if you reference on the right hand side variable which is defined in remote context but not defined locally. I suggest to use esprima and check if it can build complete AST as a flag

sidorares avatar Oct 29 '13 04:10 sidorares

Oh!! Let me think about it. Thank you!

ShawnHuang avatar Oct 29 '13 04:10 ShawnHuang