Permalink
Browse files

Change exposed api function to accept configuration. Change ScopeModu…

…le to localize the cache to each context and to reset when the context resets. Add event for output writing which can be tapped into for piping to multiple destinations
  • Loading branch information...
1 parent 3bc0656 commit 00c34a765b8ddded7506cd36b9eff4f2c7208047 @Benvie committed Apr 3, 2012
Showing with 73 additions and 38 deletions.
  1. BIN bin/PuTTY.reg
  2. +6 −1 bin/putty.js
  3. +2 −2 index.js
  4. +1 −1 lib/Context.js
  5. +24 −7 lib/ScopedModule.js
  6. +5 −4 lib/UltraREPL.js
  7. +31 −20 lib/UltraRLI.js
  8. +1 −1 lib/classes/Dict.js
  9. +3 −2 settings/styling.js
View
Binary file not shown.
View
@@ -19,7 +19,12 @@ if (fs.existsSync('PuTTY.reg')) {
function putty(){
net.createServer(function(socket){
- UltraREPL(socket, socket);
+ var repl = UltraREPL({
+ input: socket,
+ output: socket,
+ width: 100,
+ height: 35
+ });
}).listen(1337);
if (process.cwd() === __dirname) {
if (!fs.existsSync('../workspace')) {
View
@@ -2,6 +2,6 @@ var path = require('path');
var ScopedModule = require('./lib/ScopedModule');
var UltraREPL = ScopedModule._load(__dirname + '/lib/UltraREPL.js', null, true);
-module.exports = function(input, output){
- return new UltraREPL({ input: input || process.stdin, output: output || process.stdout });
+module.exports = function(options){
+ return new UltraREPL(options);
}
View
@@ -87,7 +87,7 @@ Context.prototype = {
return ScopedModule._resolveFilename(request, _module);
}
_module.paths = ScopedModule._nodeModulePaths(fakename);
-
+ _module._cache = [];
_module.filename = fakename;
_module.loaded = true;
_module.locals = this.locals = {
View
@@ -17,7 +17,7 @@ var vm, NativeModule;
})();
// Hide the pages and pages of sourcecode in case inspected in the REPL
-Object.defineProperty(NativeModule, '_source', { enumerable: false });
+try { Object.defineProperty(NativeModule, '_source', { enumerable: false }); } catch(e){console.log(NativeModule)}
var util = require('util');
@@ -92,10 +92,27 @@ Object.defineProperties(ScopedModule, {
var resolved = ScopedModule._resolveFilename(request, parent);
var filename = resolved;
- if (filename in ScopedModule._cache) return ScopedModule._cache[filename].exports;
-
- var _module = ScopedModule._cache[filename] = new ScopedModule(filename, parent);
+ if (parent == null || !parent._cache) {
+ var _module = new ScopedModule(filename, null);
+ Object.defineProperty(_module, '_cache', {
+ writable: true,
+ configurable: true,
+ value: []
+ });
+ } else {
+ if (parent && parent._cache && filename in parent._cache) {
+ return parent._cache[filename].exports;
+ } else {
+ var _module = new ScopedModule(filename, parent);
+ Object.defineProperty(_module, '_cache', {
+ writable: true,
+ configurable: true,
+ value: parent._cache
+ });
+ }
+ }
+ _module._cache[filename] = _module;
if (isMain) {
Object.defineProperty(process, 'mainModule', {
@@ -107,6 +124,7 @@ Object.defineProperties(ScopedModule, {
_module.id = '.';
}
+
var info = explorePath(filename);
if (!info.isDir) info = info.parent;
@@ -140,7 +158,7 @@ Object.defineProperties(ScopedModule, {
try { _module.load(resolved) }
- catch (err) { delete ScopedModule._cache[filename]; throw err; }
+ catch (err) { _module._cache[filename]; throw err; }
return _module.exports;
}),
@@ -162,7 +180,7 @@ Object.defineProperties(ScopedModule, {
names = names.filter(jsIdentifier);
var wrapper = ScopedModule.wrapper[0]+names+ScopedModule.wrapper[1]+code+ScopedModule.wrapper[2];
var wrapped = vm.runInThisContext(wrapper, name);
-
+
function run(scope){
return function(){
"use strict";
@@ -219,7 +237,6 @@ ScopedModule.prototype = Object.create(Module.prototype, {
}
require.extensions = Module._extensions;
- require.cache = Module._cache;
require.main = ScopedModule.main;
var scopeArgs = Object.create(this.locals);
View
@@ -23,7 +23,7 @@ function UltraREPL(options){
width: process.stdout.columns || 100,
height: process.stdout.rows || 40,
colors: true,
- prompt: 'js',
+ prompt: '',
input: process.stdin,
output: process.stdout,
};
@@ -67,7 +67,7 @@ function UltraREPL(options){
rli.on('keybind', function(key){
self.keydisplay && rli.timedWrite('topright', key.bind, styling.info.keydisplay);
if (output !== process.stdout) {
- console.log(key);
+ //console.log(key);
}
});
rli.on('line', function(cmd){
@@ -174,7 +174,7 @@ UltraREPL.prototype = {
updatePrompt: function updatePrompt(){
var prompt = [this.prompt];
if (this.context.count) {
- prompt.push((this.context.index + 1 + '').color(styling.prompt.number) + ' ' + this.context.displayName);
+ prompt.push((this.context.index + 1 + '').color(styling.prompt.number));
}
if (this.messages) {
@@ -278,11 +278,12 @@ UltraREPL.prototype = {
var page = this.pages.length;
page = page > 1 ? (this.pages.index + 1) + '/' + page : '';
this.rli.writeMount('topcenter', page.color(styling.info.page), styling.info.header);
+ this.rli.writeMount('topleft', this.context.displayName.color(styling.info.context), styling.info.header);
},
header: function header(){
this.rli.cursorTo(0, 0);
- this.output.write(' '.repeat(this.width).color(styling.info.header));
+ this.rli.print(' '.repeat(this.width).color(styling.info.header));
this.pageLabel();
this.rli.toCursor();
},
View
@@ -98,11 +98,11 @@ UltraRLI.prototype = {
this.emit('resume');
},
- loadCursor: function loadCursor(){ this.output.write('\x1b[1u') },
- saveCursor: function saveCursor(){ this.output.write('\x1b[1s') },
- clearScreen: function clearScreen(){ this.output.write('\x1b[1J') },
+ loadCursor: function loadCursor(){ this.print('\x1b[1u') },
+ saveCursor: function saveCursor(){ this.print('\x1b[1s') },
+ clearScreen: function clearScreen(){ this.print('\x1b[1J') },
setWindowSize: function setWindowSize(width, height){
- this.output.write('\x1b[8'+width+';'+height+'t');
+ this.print('\x1b[8'+width+';'+height+'t');
this.width = width;
this.height = height;
this.emit('resize');
@@ -148,7 +148,7 @@ UltraRLI.prototype = {
fill: function fill(minus){
this.cursorTo(0, 0);
- this.output.write('\n'.repeat(this.height - minus));
+ this.print('\n'.repeat(this.height - minus));
this.toCursor();
},
@@ -172,7 +172,7 @@ UltraRLI.prototype = {
refreshLine: function refreshLine(){
this.eraseInput();
- this.output.write(this._prompt + this.line);
+ this.print(this._prompt + this.line);
this.toCursor();
},
@@ -209,7 +209,7 @@ UltraRLI.prototype = {
this.cursorTo(left, top + 1);
for (var i = 0, len = output.length; i + top < this.height - 1 && i < output.length; i++) {
this.moveCursor(0, 1);
- this.output.write(output[i] );
+ this.print(output[i] );
this.cursorTo(left);
}
this.toCursor();
@@ -218,7 +218,7 @@ UltraRLI.prototype = {
writePage: function writePage(lines){
for (var i = 0; i < this.height - 2 && i < lines.length; i++) {
this.cursorTo(0, i + 1);
- this.output.write(lines[i]);
+ this.print(lines[i]);
this.___clearLine(1);
};
this.toCursor();
@@ -242,37 +242,37 @@ UltraRLI.prototype = {
throw new Error("Can't set cursor row without also setting it's column");
if (typeof y !== 'number') {
- this.output.write('\x1b[' + (x + 1) + 'G');
+ this.print('\x1b[' + (x + 1) + 'G');
} else {
- this.output.write('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
+ this.print('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
}
},
moveCursor: function moveCursor(dx, dy) {
if (dx < 0) {
- this.output.write('\x1b[' + (-dx) + 'D');
+ this.print('\x1b[' + (-dx) + 'D');
} else if (dx > 0) {
- this.output.write('\x1b[' + dx + 'C');
+ this.print('\x1b[' + dx + 'C');
}
if (dy < 0) {
- this.output.write('\x1b[' + (-dy) + 'A');
+ this.print('\x1b[' + (-dy) + 'A');
} else if (dy > 0) {
- this.output.write('\x1b[' + dy + 'B');
+ this.print('\x1b[' + dy + 'B');
}
},
___clearLine: function clearLine(dir) {
if (dir < 0) {
// to the beginning
- this.output.write('\x1b[1K');
+ this.print('\x1b[1K');
} else if (dir > 0) {
// to the end
- this.output.write('\x1b[0K');
+ this.print('\x1b[0K');
} else {
// entire line
- this.output.write('\x1b[2K');
+ this.print('\x1b[2K');
}
},
@@ -290,7 +290,7 @@ UltraRLI.prototype = {
if (typeof mount === 'string') mount = mounts[mount];
if (!mount.contents) return;
this.cursorTo(mount.left, mount.top);
- this.output.write(' '.repeat(mount.width).color(mount.bg));
+ this.print(' '.repeat(mount.width).color(mount.bg));
mount.width = 0;
mount.contents = '';
mount.left = null;
@@ -309,7 +309,7 @@ UltraRLI.prototype = {
mount.left -= mount.width / 2 | 0;
}
this.cursorTo(mount.left, mount.top);
- this.output.write(message.color(bg || mount.bg));
+ this.print(message.color(bg || mount.bg));
this.toCursor();
},
@@ -346,7 +346,7 @@ UltraRLI.prototype = {
} else {
this.line += c;
this.cursor += c.length;
- this.output.write(c);
+ this.print(c);
}
},
@@ -452,6 +452,11 @@ UltraRLI.prototype = {
return key;
},
+ print: function print(v){
+ this.emit('print', v);
+ this.output.write(v);
+ },
+
write: function write(d, key) {
this._ttyWrite(d, key);
},
@@ -503,6 +508,12 @@ var mounts = {
y: 'top',
align: 'center',
bg: 'bgcyan'
+ },
+ topleft: {
+ x: 'left',
+ y: 'top',
+ align: 'left',
+ bg: 'bgcyan'
}
};
View
@@ -41,7 +41,7 @@ Dict.prototype = {
if (typeof value === 'undefined') {
return typeof key === 'undefined' ? data[this.id] : data[this.id][key];
}
- data[this.id] && data[this.id][key] = value;
+ data[this.id] && (data[this.id][key] = value);
},
get keys(){ return Object.keys(this); },
get values(){ return this.keys.map(function(k){ return this[k] }, this); },
View
@@ -72,13 +72,14 @@ var styling = function(){
},
intro: [ yellow2, red2 ],
prompt: {
- separator: ['', green1],
- end: [' » ', cyan1],
+ separator: ['', green1],
+ end: ['»', cyan1],
'--': yellow1,
'++': yellow2,
number: yellow1
},
info: {
+ context: [bg_cyan1, black2],
header: [bg_cyan1, white2],
page: [bg_cyan1, white2],
keydisplay: [bg_cyan1, white2],

0 comments on commit 00c34a7

Please sign in to comment.