Skip to content

Commit

Permalink
Reenamed inspect.js to console.js.
Browse files Browse the repository at this point in the history
Added propper console.
Adapt web languages to changes.
  • Loading branch information
amasad committed Dec 16, 2011
1 parent f3f9edb commit 1812486
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 26 deletions.
19 changes: 9 additions & 10 deletions langs/coffee-script/jsrepl_coffee.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ class self.JSREPLEngine
constructor: (input, output, @result, @error, @sandbox, ready) ->
# Cache sandboxed objects and functions used by the engine in case sandbox
# bindings hide them.
@inspect = @sandbox._inspect
@inspect = @sandbox.console.inspect
@CoffeeScript = @sandbox.CoffeeScript
@sandbox.__eval = @sandbox.eval

# Define custom I/O handlers.
@sandbox.console.log = (obj) => output obj + '\n'
@sandbox.console.dir = (obj) => output @inspect(obj) + '\n'
@sandbox.console.read = input

ready()

Eval: (command) ->
Expand All @@ -25,10 +20,14 @@ class self.JSREPLEngine
@result if result == undefined then '' else @inspect result
catch e
@error e

EvalSync: (command) ->
compiled = @CoffeeScript.compile command, globals: on, bare: on
return @sandbox.__eval compiled, globals: on, bare: on

RawEval: (command) ->
try
compiled = @CoffeeScript.compile command, globals: on, bare: on
result = @sandbox.__eval compiled, globals: on, bare: on
@result if result == undefined then '' else @inspect result
catch e
@error e

GetNextLineIndent: (command) ->
last_line = command.split('\n')[-1..][0]
Expand Down
7 changes: 1 addition & 6 deletions langs/javascript/jsrepl_js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ class self.JSREPLEngine
constructor: (input, output, @result, @error, @sandbox, ready) ->
# Cache sandboxed objects and functions used by the engine in case sandbox
# bindings hide them.
@inspect = @sandbox._inspect
@inspect = @sandbox.console.inspect
@functionClass = @sandbox.Function
@sandbox.__eval = @sandbox.eval

# Define custom I/O handlers.
@sandbox.console.log = (obj) => output obj + '\n'
@sandbox.console.dir = (obj) => output @inspect(obj) + '\n'
@sandbox.console.read = input

ready()

Eval: (command) ->
Expand Down
7 changes: 1 addition & 6 deletions langs/kaffeine/jsrepl_kaffeine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class self.JSREPLEngine
constructor: (input, output, @result, @error, @sandbox, ready) ->
# Cache sandboxed objects and functions used by the engine in case sandbox
# bindings hide them.
@inspect = @sandbox._inspect
@inspect = @sandbox.console.inspect
@functionClass = @sandbox.Function
@sandbox.__eval = @sandbox.eval

Expand All @@ -11,11 +11,6 @@ class self.JSREPLEngine
Kaffeine = @sandbox.require 'kaffeine'
@kaffeine = new Kaffeine

# Define custom I/O handlers.
@sandbox.console.log = (obj) => output obj + '\n'
@sandbox.console.dir = (obj) => output @inspect(obj) + '\n'
@sandbox.console.read = input

ready()

Compile: (command) ->
Expand Down
2 changes: 1 addition & 1 deletion langs/move/jsrepl_move.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class self.JSREPLEngine
constructor: (input, output, @result, @error, @sandbox, ready) ->
# Cache sandboxed objects and functions used by the engine in case sandbox
# bindings hide them.
@inspect = @sandbox._inspect
@inspect = @sandbox.console.inspect
@functionClass = @sandbox.Function
@sandbox.__eval = @sandbox.eval
@compile = @sandbox.move.compile
Expand Down
61 changes: 58 additions & 3 deletions util/inspect.js → util/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@
*/

(function(){

// The maximum length of a line in the stylized output.
var MAX_COLUMNS = 80;

/**
* Echos the value of a value. Tries to print the value out in the best way
* possible given the different types.
Expand All @@ -40,7 +38,7 @@ var MAX_COLUMNS = 80;
* @param {Boolean} colors Flag to turn on ANSI escape codes to color the
* output. Default is false (no coloring).
*/
_inspect = function(obj, showHidden, depth, colors) {
var inspect = function(obj, showHidden, depth, colors) {
var seen = [];

var stylize = function(str, styleType) {
Expand Down Expand Up @@ -280,4 +278,61 @@ function timestamp() {
return [d.getDate(), months[d.getMonth()], time].join(' ');
}


var formatRegExp = /%[sdj%]/g;
var format = function(f) {
if (typeof f !== 'string') {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(inspect(arguments[i]));
}
return objects.join(' ');
}

var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
if (i >= len) return x;
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j': return JSON.stringify(args[i++]);
case '%%': return '%';
default:
return x;
}
});
for (var x = args[i]; i < len; x = args[++i]) {
if (x === null || typeof x !== 'object') {
str += ' ' + x;
} else {
str += ' ' + inspect(x);
}
}
return str;
};

var times = {};
self.console = {
log: function () {
Sandboss.out(format.apply(this, arguments) + '\n');
},
dir: function (obj) {
Sandboss.out(inspect(obj) + '\n');
},
time: function (label) {
times[label] = Date.now();
},
timeEnd: function (label) {
var duration = Date.now() - times[label];
self.console.log('%s: %dms', label, duration);
},
read: function (cb) {
cb = cb || function () {};
Sandboss.input(cb);
},
inspect: inspect
};

})();

0 comments on commit 1812486

Please sign in to comment.