Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move all keys to bindings, but deleting is broken and I can't figure …

…out why
  • Loading branch information...
commit f9ded9b6e926e4161023f9589b9ca8a7d5294e3c 1 parent faa18ec
@Benvie authored
Showing with 176 additions and 65 deletions.
  1. +74 −40 lib/UltraRLI.js
  2. +57 −1 lib/commands.js
  3. +45 −24 settings/controls.js
View
114 lib/UltraRLI.js
@@ -40,11 +40,12 @@ function UltraRLI(stream, completer){
this.line = '';
this.enabled = output.isTTY && !parseInt(process.env['NODE_NO_READLINE'], 10);
+ var self = this;
if (!this.enabled) {
- input.on('data', this._normalWrite.bind(this));
+ input.on('data', function(){ self._normalWrite.apply(self, arguments) });
} else {
tty.setRawMode(true);
- input.on('keypress', this._ttyWrite.bind(this));
+ input.on('keypress', function(){ self._ttyWrite.apply(self, arguments) });
this.cursor = 0;
this.history = [];
this.historyIndex = -1;
@@ -246,9 +247,66 @@ UltraRLI.prototype = {
mount.timer.expires = Date.now() + (time || 5000);
},
+ _insertString: function _insertString(c){
+ if (this.cursor < this.line.length) {
+ var beg = this.line.slice(0, this.cursor);
+ var end = this.line.slice(this.cursor, this.line.length);
+ this.line = beg + c + end;
+ this.cursor += c.length;
+ this.refreshLine();
+ } else {
+ this.line += c;
+ this.cursor += c.length;
+ this.output.write(c);
+ }
+ },
+
+ _moveLeft: function _moveLeft(count){
+ if (this.cursor > 0) {
+ this.cursor--;
+ this.output.moveCursor(-1);
+ }
+ this.refreshLine();
+ },
+
+ _moveRight: function _moveRight(){
+ if (this.cursor !== this.line.alength) {
+ this.cursor++;
+ this.output.moveCursor(1);
+ }
+ this.refreshLine();
+ },
+
+ _lineLeft: function _lineLeft(){
+ this.cursor = 0;
+ this.output.moveCursor(0);
+ this.refreshLine();
+ },
+
+ _lineRight: function _lineRight(){
+ this.cursor = this.line.length;
+ this.output.moveCursor(this.line.length);
+ this.refreshLine();
+ },
+
+ _deleteLeft: function _deleteLeft() {
+ if (this.cursor > 0 && this.line.length > 0) {
+ this.cursor--;
+ this.output.moveCursor(-1);
+ this.line = this.line.slice(0, this.cursor) + this.line.slice(this.cursor + 1);
+ this.refreshLine();
+ }
+ },
+
+ _deleteRight: function _deleteRight() {
+ if (this.cursor < this.line.length) {
+ this.line = this.line.slice(0, this.cursor) + this.line.slice(this.cursor + 1);
+ this.refreshLine();
+ }
+ },
translate: (function(namemap, mods){
- return function translateKey(val, key){
+ return function translate(val, key){
if (!key) key = {};
'ctrl' in key || (key.ctrl = false);
'meta' in key || (key.meta = false);
@@ -268,44 +326,20 @@ UltraRLI.prototype = {
'ctrl+shift+', 'alt+shift+', 'ctrl+alt+shift+' ]),
_ttyWrite: function _ttyWrite(s, key){
- var next_word, next_non_word, previous_word, previous_non_word;
-
- key = this.translate(s, key);
-
- if (key.ctrl && key.shift) switch (key.name) {
-
- case 'bksp': this._deleteLineLeft(); break;
- case 'del': this._deleteLineRight(); break;
-
- } else if (key.ctrl) switch (key.name) {
-
- case 'del': this._deleteWordRight(); break;
- case 'bksp': this._deleteWordLeft(); break;
- case 'left': this._wordLeft(); break;
- case 'right': this._wordRight();
-
- } else switch (key.name) {
-
- case 'enter': this._line(); break;
- case 'bksp': this._deleteLeft(); break;
- case 'del': this._deleteRight(); break;
- case 'tab': this._tabComplete(); break;
- case 'left': this.cursor > 0 && this.cursor-- && this.output.moveCursor(-1, 0); break;
- case 'right': this.cursor != this.line.alength && this.cursor++ && this.output.moveCursor(1, 0); break;
- case 'home': this.cursor = 0; this.refreshLine(); break;
- case 'end': this.cursor = this.line.alength; this.refreshLine(); break;
- case 'up': this._historyPrev(); break;
- case 'down': this._historyNext(); break;
- default:
- if (Buffer.isBuffer(s)) s = s.toString('utf-8');
- if (s) {
- var lines = s.split(/\r\n|\n|\r/);
- for (var i = 0, len = lines.length; i < len; i++) {
- if (i > 0) this._line();
- this._insertString(lines[i]);
- }
+ if (Buffer.isBuffer(s))
+ s = s.toString('utf-8');
+ if (s) {
+ var lines = s.split(/\r\n|\n|\r/);
+ for (var i = 0, len = lines.length; i < len; i++) {
+ if (i > 0) {
+ this._line();
}
+ this._insertString(lines[i]);
+ }
}
- return key;
+ this.translate(s, key);
}
};
+
+
+UltraRLI.prototype._refreshLine = UltraRLI.prototype.refreshLine;
View
58 lib/commands.js
@@ -1,12 +1,20 @@
var util = require('util');
var fs = require('fs');
var path = require('path');
+
var Dict = require('./Dict');
+
var isError = require('./object-utils').is('Error');
+
var controls = require('../settings/controls');
var style = require('../settings/styling');
var builtins = require('../settings/builtins');
+
+var esprima = require('esprima');
+
+
+
function contexCommand(action){
return function(){
var result = this.context[action]();
@@ -220,8 +228,54 @@ var commands = {
this.inspector(this.context._);
}
},
+
+ 'Generate AST': {
+ help: 'In progress. Use Esprima to generate AST for code to further debug and modift it.',
+ action: function(cmd, target){
+ var source = this.context.current.scripts.reduce(function(r, t){
+ if (~t.globals.indexOf(target)) r.push(t.code);
+ return r;
+ }, []);
+ if (source.length === 0) {
+ if (target in this.context.ctx && typeof this.context.ctx[target] === 'function') {
+ source = this.context.ctx[target] + '';
+ } else if (~builtins.libs.indexOf(target)) {
+ source = process.binding('natives')[target];
+ } else if (path.existsSync(target)) {
+ source = fs.readFileSync(target, 'utf-8');
+ } else {
+ source = target;
+ }
+ } else {
+ source = source.join('\n\n');
+ }
+ this.context._ = this.context.ctx.ast = esprima.parse(source).body;
+ this.inspector(this.context._);
+ }
+ },
+
+ 'Delete Left' : { action: function(){ this.rli._deleteLeft() } },
+ 'Delete Right' : { action: function(){ this.rli._deleteRight() } },
+ 'Delete Word Left' : { action: function(){ this.rli._deleteWordLeft() } },
+ 'Delete Word Right' : { action: function(){ this.rli._deleteWordRight() } },
+ 'Delete Line Left' : { action: function(){ this.rli._deleteLineLeft() } },
+ 'Delete Line Right' : { action: function(){ this.rli._deleteLineRight() } },
+
+ 'Line Left' : { action: function(){ this.rli._lineLeft() } },
+ 'Line Right' : { action: function(){ this.rli._lineRight() } },
+ 'Word Left' : { action: function(){ this.rli._wordLeft() } },
+ 'Word Right' : { action: function(){ this.rli._wordRight() } },
+ 'Move Left' : { action: function(){ this.rli._moveLeft() } },
+ 'Move Right' : { action: function(){ this.rli._moveRight() } },
+
+ 'History Prev' : { action: function(){ this.rli._historyPrev() } },
+ 'History Next' : { action: function(){ this.rli._historyNext() } },
+ 'Line' : { action: function(){ this.rli._line() } },
+ 'Tab Complete' : { action: function(){ this.rli._tabComplete() } },
};
+
+
function toggle(obj, prop){
return function(){
if (typeof prop === 'undefined') {
@@ -307,8 +361,10 @@ module.exports = function(target){
handlers[control.type](control.trigger, commands[name].action);
}
+ if (!('help' in commands[name])) return;
+
if (control.type === 'keybind' && process.platform === 'darwin') {
- control.trigger = control.trigger.replace('ctrl+', 'command+');
+ control && control.trigger = control.trigger.replace('ctrl+', 'command+');
}
target.help.push({
View
69 settings/controls.js
@@ -1,37 +1,58 @@
module.exports = {
- 'Command List' : key('f1'),
+ 'Command List' : key('f1'),
+ 'Next Page' : key('pgdn'),
+ 'Previous Page' : key('pgup'),
+ 'Clear Input/Screen' : key('esc'),
+ 'Clear Screen' : key('esc esc'),
+ 'Exit' : key('esc esc esc'),
- 'Create Context' : ctrl_shift('up'),
- 'Delete Context' : ctrl_shift('down'),
- 'Next Context' : ctrl('up'),
- 'Previous Context' : ctrl('down'),
- 'Reset Context' : ctrl('r'),
- 'Label Context' : dot('label'),
+ 'Toggle Hiddens' : key('f2'),
+ 'Toggle Builtins' : key('f3'),
+ 'Toggle __proto__' : key('f4'),
+ 'Inspect Depth++' : key('f5'),
+ 'Inspect Depth--' : key('f6'),
+ 'Set Inspect Depth' : dot('depth'),
+ 'Toggle Colors' : key('f10'),
- 'Next Page' : key('pgdn'),
- 'Previous Page' : key('pgup'),
+ 'Create Context' : ctrl_shift('up'),
+ 'Delete Context' : ctrl_shift('down'),
+ 'Next Context' : ctrl('up'),
+ 'Previous Context' : ctrl('down'),
+ 'Reset Context' : ctrl('r'),
+ 'Label Context' : dot('label'),
- 'Toggle Hiddens' : key('f2'),
- 'Toggle Builtins' : key('f3'),
- 'Toggle __proto__' : key('f4'),
- 'Inspect Depth++' : key('f5'),
- 'Inspect Depth--' : key('f6'),
- 'Set Inspect Depth' : dot('depth'),
- 'Toggle Colors' : key('f10'),
+ 'Inject REPL' : key('f11'),
+ 'Toggle Key Display' : key('f12'),
+ 'Require' : dot('r'),
+ 'Generate AST' : dot('ast'),
- 'Inject REPL' : key('f11'),
- 'Toggle Key Display': key('f12'),
+ 'Color Test' : key('alt+1 alt+2'),
- 'Require' : dot('r'),
+ //'Save Session' : dot('save'),
- 'Clear Input/Screen': key('esc'),
- 'Clear Screen' : key('esc esc'),
- 'Exit' : key('esc esc esc'),
- 'Color Test' : key('alt+1 alt+2'),
+ 'Delete Right' : key('del'),
+ 'Delete Left' : key('bksp'),
+ 'Delete Word Right' : ctrl('del'),
+ 'Delete Word Left' : ctrl('bksp'),
+ 'Delete Line Right' : ctrl_shift('del'),
+ 'Delete Line Left' : ctrl_shift('bksp'),
- 'Save Session' : dot('save'),
+ 'Move Left' : key('left'),
+ 'Move Right' : key('right'),
+ 'Word Left' : ctrl('left'),
+ 'Word Right' : ctrl('right'),
+ 'Line Left' : key('home'),
+ 'Line Right' : key('end'),
+
+ 'History Prev' : key('up'),
+ 'History Next' : key('down'),
+
+ 'Line' : key('enter'),
+ //'Tab Complete' : key('tab'),
};
+
+
// ctrl = command on mac
// alt = meta on linux
Please sign in to comment.
Something went wrong with that request. Please try again.