Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update dependency, add mouse tracking

  • Loading branch information...
commit c6b5e62f968d4c6644856aa5af7ecf66a185c0b1 1 parent b54f6f4
@Benvie authored
View
8 lib/UltraREPL.js
@@ -70,9 +70,11 @@ function UltraREPL(options){
});
rli.on('resize', function(){ self.refresh() });
rli.on('keybind', function(key){
- self.keydisplay && rli.timedWrite('topright', key.bind, styling.info.keydisplay);
- if (output !== process.stdout) {
- //console.log(key);
+ if (self.keydisplay) {
+ rli.timedWrite('topright', key.bind, styling.info.keydisplay);
+ if (output !== process.stdout) {
+ console.log(key);
+ }
}
});
View
244 lib/UltraRLI.js
@@ -97,17 +97,6 @@ UltraRLI.prototype = {
this.paused = false;
this.emit('resume');
},
-
- 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.print('\x1b[8'+width+';'+height+'t');
- this.width = width;
- this.height = height;
- this.emit('resize');
- },
-
_addHistory: function _addHistory() {
if (this.line.length === 0) return '';
@@ -234,48 +223,6 @@ UltraRLI.prototype = {
if (x >= this.width) return this.width;
},
- cursorTo: function cursorTo(x, y) {
- if (typeof x !== 'number' && typeof y !== 'number')
- return;
-
- if (typeof x !== 'number')
- throw new Error("Can't set cursor row without also setting it's column");
-
- if (typeof y !== 'number') {
- this.print('\x1b[' + (x + 1) + 'G');
- } else {
- this.print('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
- }
- },
-
-
- moveCursor: function moveCursor(dx, dy) {
- if (dx < 0) {
- this.print('\x1b[' + (-dx) + 'D');
- } else if (dx > 0) {
- this.print('\x1b[' + dx + 'C');
- }
-
- if (dy < 0) {
- this.print('\x1b[' + (-dy) + 'A');
- } else if (dy > 0) {
- this.print('\x1b[' + dy + 'B');
- }
- },
-
- ___clearLine: function clearLine(dir) {
- if (dir < 0) {
- // to the beginning
- this.print('\x1b[1K');
- } else if (dir > 0) {
- // to the end
- this.print('\x1b[0K');
- } else {
- // entire line
- this.print('\x1b[2K');
- }
- },
-
scaleY: function scaleY(y){
if (y === 'center') return this.height / 2 | 0;
if (y === 'bottom') return this.height - 1;
@@ -443,14 +390,82 @@ UltraRLI.prototype = {
}
},
+ cursorTo: function cursorTo(x, y) {
+ if (typeof x !== 'number' && typeof y !== 'number')
+ return;
+
+ if (typeof x !== 'number')
+ throw new Error("Can't set cursor row without also setting it's column");
+
+ if (typeof y !== 'number') {
+ this.print('\x1b[' + (x + 1) + 'G');
+ } else {
+ this.print('\x1b[' + (y + 1) + ';' + (x + 1) + 'H');
+ }
+ },
+
+
+ moveCursor: function moveCursor(dx, dy) {
+ if (dx < 0) {
+ this.print('\x1b[' + (-dx) + 'D');
+ } else if (dx > 0) {
+ this.print('\x1b[' + dx + 'C');
+ }
+
+ if (dy < 0) {
+ this.print('\x1b[' + (-dy) + 'A');
+ } else if (dy > 0) {
+ this.print('\x1b[' + dy + 'B');
+ }
+ },
+
+ ___clearLine: function clearLine(dir) {
+ if (dir < 0) {
+ // to the beginning
+ this.print('\x1b[1K');
+ } else if (dir > 0) {
+ // to the end
+ this.print('\x1b[0K');
+ } else {
+ // entire line
+ this.print('\x1b[2K');
+ }
+ },
+
+ toggleMouse: function toggleMouse(){
+ if (this.mouse) {
+ this.print('\x1b[?1000h');
+ } else {
+ this.print('\x1b[?1005h');
+ this.print('\x1b[?1003h');
+ }
+ return this.mouse = !this.mouse;
+ },
+
+ 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.print('\x1b[8'+width+';'+height+'t');
+ this.width = width;
+ this.height = height;
+ this.emit('resize');
+ },
+
+
translate: function translate(val, key){
- key = key || {};
- key.shift = !!key.shift;
- key.ctrl = !!key.ctrl;
- key.meta = !!key.meta;
- key.name = namemap[key.name] || key.name || '';
- key.bind = key.bind || mods[key.ctrl | key.meta << 1 | key.shift << 2] + key.name;
- return key;
+ if (!val && key) val = key.sequence || '';
+ if (/^\u001b\[M/.test(val)) {
+ return new MouseEvent(val);
+ } else {
+ return new KeyEvent(val, key);
+ }
},
print: function print(v){
@@ -463,8 +478,13 @@ UltraRLI.prototype = {
},
_ttyWrite: function _ttyWrite(s, key){
key = this.translate(s, key);
- this.emit('keybind', key, s);
- if (key.used || key.ctrl || key.meta) return;
+
+ if (key.type === 'keyboard') {
+ this.emit('keybind', key, s);
+ if (key.used || key.ctrl || key.meta) return;
+ } else if (key.type === 'mouse') {
+ return this.emit(key.name, key);
+ }
if (Buffer.isBuffer(s)) {
s = s.toString('utf8');
@@ -481,21 +501,91 @@ UltraRLI.prototype = {
UltraRLI.prototype._refreshLine = UltraRLI.prototype.refreshLine;
-var namemap = { backspace: 'bksp',
- escape: 'esc',
- delete: 'del',
- pagedown: 'pgdn',
- pageup: 'pgup',
- insert: 'ins' };
-
-var mods = [ '',
- 'ctrl+',
- 'alt+',
- 'ctrl+alt+',
- 'shift+',
- 'ctrl+shift+',
- 'alt+shift+',
- 'ctrl+alt+shift+' ];
+
+var mods =
+[ '',
+ 'ctrl+',
+ 'alt+',
+ 'ctrl+alt+',
+ 'shift+',
+ 'ctrl+shift+',
+ 'alt+shift+',
+ 'ctrl+alt+shift+' ]
+
+var InputEvent = function(mods){
+ function InputEvent(){}
+
+ InputEvent.prototype = {
+ used: false,
+ };
+
+ return InputEvent;
+}();
+
+var KeyEvent = function(namemap){
+ function KeyEvent(s, key){
+ key = key || {};
+ this.type = 'keyboard';
+ this.name = namemap[key.name] || key.name || '';
+ this.shift = !!key.shift;
+ this.ctrl = !!key.ctrl;
+ this.meta = !!key.meta;
+ this.bind = mods[this.ctrl | this.meta << 1 | this.shift << 2] + this.name;
+ this.data = Buffer(s);
+ }
+
+ Function.inherit(InputEvent, KeyEvent, {
+ type: 'keyboard',
+ key: ''
+ });
+
+ return KeyEvent;
+}({ backspace: 'bksp',
+ escape: 'esc',
+ delete: 'del',
+ pagedown: 'pgdn',
+ pageup: 'pgup',
+ insert: 'ins' });
+
+
+var MouseEvent = function(namemap){
+ var button;
+ function MouseEvent(s, k){
+ var data = Buffer(s);
+ this.name = 'mouse';
+ if (data.length === 6) {
+ this.x = data[4] - 32;
+ this.y = data[5] - 32;
+ } else if (data.length == 8) {
+ this.x = data.readUInt16BE(4) - 32;
+ this.y = data.readUInt16BE(6) - 32;
+ }
+ data = data[3];
+ if ((data & 96) === 96) {
+ this.name += 'scroll';
+ this.button = data & 1 ? 'down' : 'up';
+ } else if (data & 64) {
+ this.name += 'move';
+ } else {
+ this.name += data === 35 ? 'up' : 'down';
+ this.button = namemap[3] = namemap[data - 32];
+ }
+ this.shift = !!(data & 4);
+ this.meta = !!(data & 8);
+ this.ctrl = !!(data & 16);
+ this.bind = mods[this.ctrl | this.meta << 1 | this.shift << 2] + this.name;
+ this.data = Buffer(s);
+ }
+
+ Function.inherit(InputEvent, MouseEvent, {
+ type: 'mouse',
+ });
+
+ return MouseEvent;
+}([ 'left', 'middle', 'right', 'none' ]);
+
+
+
var R = require('repl-rainbow');
var deepSkyBlue = R(0,180,255).fg(255,255,255);
View
67 lib/utility/object-utils.js
@@ -1,7 +1,9 @@
-var callbind = Function.prototype.bind.bind(Function.prototype.call);
-var applybind = Function.prototype.bind.bind(Function.prototype.apply);
+var bindbind = Function.prototype.bind.bind(Function.prototype.bind);
+var callbind = bindbind(Function.prototype.call);
+var applybind = bindbind(Function.prototype.apply);
var slice = callbind(Array.prototype.slice);
-
+var has = callbind(Object.prototype.hasOwnProperty);
+var toString = callbind(Object.prototype.toString);
module.exports = {
heritable: heritable,
@@ -12,23 +14,28 @@ module.exports = {
is: is
};
+var props = Object.getOwnPropertyNames;
+var define = Object.defineProperty;
+var describe = Object.getOwnPropertyDescriptor;
+var proto = Object.getPrototypeOf;
+var create = Object.create;
+var keys = Object.keys;
+
function is(compare){
if (typeof compare === 'string') {
compare = '[object ' + compare + ']';
} else {
- compare = Object.prototype.toString.call(compare);
+ compare = toString(compare);
}
if (compare in is) return is[compare];
return is[compare] = function(o){
- return Object.prototype.toString.call(o) === compare;
+ return toString(o) === compare;
}
}
function clone(o){
- var names = Object.getOwnPropertyNames(o);
- return Object.create(Object.getPrototypeOf(o),
- names.map(Object.getOwnPropertyDescriptor.bind(null, o))
- .reduce(function(r,d,i){ r[names[i]] = d; return r; }, {}));
+ var names = props(o);
+ return create(proto(o), names.map(describe.bind(null, o)).reduce(function(r,d,i){ r[names[i]] = d; return r; }, {}));
}
function descriptor(val, h, r){
@@ -49,18 +56,18 @@ function descriptor(val, h, r){
function heritable(definition){
var ctor = definition.constructor;
- Object.defineProperty(ctor, 'super', {
+ define(ctor, 'super', {
value: definition.super,
configurable: true,
writable: true
});
- ctor.prototype = Object.create(ctor.super.prototype);
+ ctor.prototype = create(ctor.super.prototype);
delete definition.super;
- Object.keys(definition).forEach(function(prop){
- var desc = Object.getOwnPropertyDescriptor(definition, prop);
+ keys(definition).forEach(function(prop){
+ var desc = describe(definition, prop);
desc.enumerable = false;
- Object.defineProperty(ctor.prototype, prop, desc);
+ define(ctor.prototype, prop, desc);
});
function construct(){
@@ -82,11 +89,39 @@ function lazyProperty(obj, name){
}
var visible = name[0] === '$';
name = visible ? name.slice(1) : name;
- Object.defineProperty(obj, name, {
+ define(obj, name, {
configurable: true,
enumerable: visible,
get: function(){},
- set: function(v){ Object.defineProperty(this, name, { value: v, writable: true }) }
+ set: function(v){ define(this, name, { value: v, writable: true }) }
+ });
+}
+
+var hidden = { configurable: true, writable: true };
+function defineHidden(o, n, v){
+ hidden.value = v;
+ define(o, n, v);
+ hidden.value = null;
+}
+
+
+function extend(to, from){
+ props(Object(from)).forEach(function(key){
+ if (!has(to, key)) {
+ define(to, key, describe(from, key));
+ }
});
+ return to;
+}
+Object.extend = extend;
+
+
+Object.inherit = function inherit(from, props){
+ return extend(create(from), props);
}
+Function.inherit = function inherit(superctor, ctor, props){
+ ctor.prototype = Object.inherit(superctor.prototype, props);
+ defineHidden(ctor.prototype, 'constructor', ctor);
+ return ctor.prototype;
+}
View
4 package.json
@@ -2,7 +2,7 @@
"author": "Brandon Benvie <brandon@bbenvie.com> (http://bbenvie.com)",
"name": "ultra-repl",
"description": "Starting with Node's built in REPL, add in a completely redone inspect formatter, actual functioning separate V8 contexts, keybindings to create, switch, and delete between them, combine it with a bunch of color. ULTRA REPL.",
- "version": "0.8.1",
+ "version": "0.8.2",
"keywords": ["repl", "context", "colors", "command line", "cli", "editor", "tty", "options", "toggle"],
"homepage": "https://github.com/Benvie/Node.js-Ultra-REPL",
"repository": {
@@ -18,6 +18,6 @@
},
"dependencies": {
"esprima": ">=0.9.7",
- "repl-rainbow": "~0.1.0"
+ "repl-rainbow": "~0.1.1"
}
}
View
8 plugins/toggles.js
@@ -37,6 +37,14 @@ module.exports = [
this.refresh();
}
},
+ { name: 'Mouse',
+ help: 'Toggle mouse reporting.',
+ defaultTrigger: api.keybind('alt+m'),
+ action: function(){
+ var on = this.rli.toggleMouse() ? '++' : '--';
+ this.timedPrompt('mouse' + on, styling.prompt[on]);
+ }
+ },
{ name: 'Depth--',
help: 'Decrease inspector recurse depth',
defaultTrigger: api.keybind('alt+1'),
Please sign in to comment.
Something went wrong with that request. Please try again.