2015-05-16 20:39:14 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var View = require('./view.js').View;
|
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var strUtil = require('./string_util.js');
|
|
|
|
var ansi = require('./ansi_term.js');
|
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
2015-05-21 05:07:36 +00:00
|
|
|
var GapBuffer = require('gapbuffer').GapBuffer;
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-20 19:24:39 +00:00
|
|
|
//
|
|
|
|
// Notes
|
|
|
|
// * options.tabSize can be used to resolve \t
|
|
|
|
// * See https://github.com/dominictarr/hipster/issues/15 about insert/delete lines
|
|
|
|
//
|
|
|
|
// Blessed
|
|
|
|
// insertLine: CSR(top, bottom) + CUP(y, 0) + IL(1) + CSR(0, height)
|
|
|
|
// deleteLine: CSR(top, bottom) + CUP(y, 0) + DL(1) + CSR(0, height)
|
|
|
|
// Quick Ansi -- update only what was changed:
|
|
|
|
// https://github.com/dominictarr/quickansi
|
|
|
|
//
|
|
|
|
// This thread is awesome:
|
|
|
|
// https://github.com/dominictarr/hipster/issues/15
|
|
|
|
//
|
|
|
|
// See Atom's implementations
|
|
|
|
// Newer TextDocument
|
|
|
|
// https://github.com/atom/text-document
|
|
|
|
//
|
|
|
|
// Older TextBuffer
|
|
|
|
// http://www.oscon.com/oscon2014/public/schedule/detail/37593
|
|
|
|
//
|
|
|
|
// Span Skip List could be used for mappings of rows/cols (display) to
|
|
|
|
// character offsets in a buffer
|
|
|
|
// https://github.com/atom/span-skip-list
|
|
|
|
|
|
|
|
//
|
|
|
|
// Buffer: Actual text buffer
|
|
|
|
// Transform: Display of soft wrap & tab expansion (e.g. tab -> ' ' * tabWidth)
|
|
|
|
//
|
2015-05-18 03:33:27 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// General Design
|
|
|
|
//
|
|
|
|
// * Take any existing input & word wrap into lines[] preserving
|
|
|
|
// formatting characters.
|
|
|
|
// * When drawing, formatting characters are processed but not shown
|
|
|
|
// or processed directly in many cases. E.g., \n is processed but simply
|
|
|
|
// causes us to go to our "next line" visibly.
|
|
|
|
// * Empty/blank lines = \n
|
|
|
|
//
|
2015-05-16 20:39:14 +00:00
|
|
|
exports.MultiLineEditTextView = MultiLineEditTextView;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Some resources & comparisons
|
|
|
|
//
|
2015-05-18 03:33:27 +00:00
|
|
|
// Enthral @ https://github.com/M-griffin/Enthral/blob/master/src/msg_fse.cpp
|
|
|
|
// * Tabs are ignored
|
|
|
|
// * Preview/reading mode processes colors, otherwise just text (e.g. editor)
|
2015-05-16 20:39:14 +00:00
|
|
|
//
|
2015-05-18 03:33:27 +00:00
|
|
|
// x84 @ https://github.com/jquast/x84/blob/master/x84/bbs/editor.py
|
|
|
|
//
|
|
|
|
// Syncronet
|
2015-05-16 20:39:14 +00:00
|
|
|
//
|
2015-05-20 19:24:39 +00:00
|
|
|
//
|
|
|
|
// Projects of use/interest:
|
|
|
|
//
|
|
|
|
// https://github.com/atom/text-buffer
|
|
|
|
// http://danieltao.com/lazy.js/
|
|
|
|
// http://www.jbox.dk/downloads/edit.c
|
|
|
|
// https://github.com/slap-editor/slap
|
|
|
|
// https://github.com/chjj/blessed
|
|
|
|
//
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-28 04:53:21 +00:00
|
|
|
// need self.skipTabs(dir): if pos='\t', skip ahead (in dir) until reg char. This can be used @ up, left, right, down
|
|
|
|
|
2015-05-16 20:39:14 +00:00
|
|
|
function MultiLineEditTextView(options) {
|
2015-05-18 03:33:27 +00:00
|
|
|
|
|
|
|
if(!_.isBoolean(options.acceptsFocus)) {
|
|
|
|
options.acceptsFocus = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isBoolean(this.acceptsInput)) {
|
|
|
|
options.acceptsInput = true;
|
|
|
|
}
|
|
|
|
|
2015-05-16 20:39:14 +00:00
|
|
|
View.call(this, options);
|
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
//
|
2015-06-01 03:18:23 +00:00
|
|
|
// defualt tabWidth is 8
|
2015-05-21 05:07:36 +00:00
|
|
|
// See the following:
|
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs2/control_chars/
|
|
|
|
// * http://www.bbsdocumentary.com/library/PROGRAMS/GRAPHICS/ANSI/bansi.txt
|
|
|
|
//
|
|
|
|
this.tabWidth = _.isNumber(options.tabWidth) ? options.tabWidth : 8;
|
|
|
|
|
2015-05-19 04:29:33 +00:00
|
|
|
|
2015-05-16 20:39:14 +00:00
|
|
|
var self = this;
|
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
this.renderBuffer = [];
|
|
|
|
this.textBuffer = new GapBuffer(1024);
|
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
this.lines = []; // a given line is text...until EOL
|
2015-05-16 20:39:14 +00:00
|
|
|
this.topLineIndex = 0;
|
2015-05-27 04:17:57 +00:00
|
|
|
this.cursorPos = { row : 0, col : 0 }; // relative to view window
|
2015-05-22 04:06:35 +00:00
|
|
|
this.renderStartIndex = 0;
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-22 04:06:35 +00:00
|
|
|
this.getTabString = function() {
|
|
|
|
return new Array(self.tabWidth).join(' ');
|
|
|
|
};
|
|
|
|
|
2015-05-31 05:45:22 +00:00
|
|
|
this.getRenderLine = function(line) {
|
|
|
|
// :TODO: fix tabbing here
|
|
|
|
line = line.replace(self.getReplaceTabsRegExp(), self.getTabString()).replace(/\n/g, '');
|
|
|
|
var remain = self.dimens.width - line.length;
|
|
|
|
if(remain > 0) {
|
|
|
|
line += new Array(remain).join(' ');
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
};
|
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
this.redrawViewableText = function() {
|
2015-05-21 05:07:36 +00:00
|
|
|
var row = self.position.row;
|
|
|
|
var bottom = row + self.dimens.height;
|
|
|
|
var i = self.topLineIndex;
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
self.client.term.write(self.getSGR());
|
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
while(i < self.renderBuffer.length && row < bottom) {
|
|
|
|
self.client.term.write(ansi.goto(row, this.position.col));
|
2015-05-27 04:17:57 +00:00
|
|
|
self.client.term.write(self.getRenderLine(self.renderBuffer[i]));
|
2015-05-21 05:07:36 +00:00
|
|
|
++row;
|
|
|
|
++i;
|
2015-05-18 03:33:27 +00:00
|
|
|
}
|
2015-05-16 20:39:14 +00:00
|
|
|
};
|
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
this.wordWrap = function(line) {
|
2015-05-16 20:39:14 +00:00
|
|
|
//
|
2015-05-18 03:33:27 +00:00
|
|
|
// Other implementations:
|
2015-05-16 20:39:14 +00:00
|
|
|
// * http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
|
|
|
|
// * http://james.padolsey.com/snippets/wordwrap-for-javascript/
|
|
|
|
// * http://phpjs.org/functions/wordwrap/
|
|
|
|
// * https://github.com/jonschlinkert/word-wrap
|
|
|
|
//
|
|
|
|
var re = new RegExp(
|
2015-05-18 03:33:27 +00:00
|
|
|
'.{1,' + self.dimens.width + '}(\\s+|$)|\\S+?(\\s+|$)', 'g');
|
|
|
|
return line.match(re) || [];
|
|
|
|
};
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-27 04:17:57 +00:00
|
|
|
this.wordWrap3 = function(line, width) {
|
|
|
|
var re = new RegExp('.{1,' + width + '}(\\s+|$)|\\S+?(\\s+|$)', 'g');
|
|
|
|
return line.replace(/\t/g, new Array(self.tabWidth).join('\t')).match(re) || [];
|
2015-05-22 04:06:35 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 04:17:57 +00:00
|
|
|
this.updateRenderBuffer = function() {
|
2015-05-29 22:59:21 +00:00
|
|
|
//
|
|
|
|
// We can estimate what is visible:
|
|
|
|
// * Starting point = start of buffer or previous LF from where we were previously
|
|
|
|
// * Ending point = start + width * height (max chars possible)
|
|
|
|
// If this system is kept, this can be optimized as per above
|
|
|
|
//
|
|
|
|
|
2015-05-27 04:17:57 +00:00
|
|
|
self.renderBuffer = [];
|
|
|
|
// :TODO: optimize this with asArray() taking the slice information
|
2015-05-29 22:59:21 +00:00
|
|
|
var lines = self.textBuffer.asArray()//.slice(self.renderStartIndex, self.renderStartIndex + self.dimens.width * self.dimens.height)
|
2015-05-21 05:07:36 +00:00
|
|
|
.join('')
|
|
|
|
.split(/\r\n|\n|\r/g);
|
|
|
|
|
|
|
|
var maxLines = self.dimens.height - self.position.row;
|
|
|
|
|
|
|
|
for(var i = 0; i < lines.length && self.renderBuffer.length < maxLines; ++i) {
|
|
|
|
if(0 === lines[i].length) {
|
|
|
|
self.renderBuffer.push('');
|
|
|
|
} else {
|
2015-05-27 04:17:57 +00:00
|
|
|
Array.prototype.push.apply(self.renderBuffer, self.wordWrap3(lines[i] + '\n', self.dimens.width));
|
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-18 03:33:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 23:27:36 +00:00
|
|
|
this.getReplaceTabsRegExp = function() {
|
|
|
|
return new RegExp('\\t{' + (self.tabWidth - 1) + '}', 'g');
|
|
|
|
};
|
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
this.getTextBufferPosition = function(row, col) {
|
2015-05-27 23:27:36 +00:00
|
|
|
var replaceTabsRe = self.getReplaceTabsRegExp();
|
2015-05-27 04:17:57 +00:00
|
|
|
var pos = 0;
|
|
|
|
for(var r = 0; r < row; ++r) {
|
|
|
|
if(self.renderBuffer[r].length > 0) {
|
|
|
|
pos += self.renderBuffer[r].replace(replaceTabsRe, '\t').length;
|
|
|
|
} else {
|
|
|
|
pos += 1;
|
|
|
|
}
|
|
|
|
}
|
2015-05-28 17:03:20 +00:00
|
|
|
|
2015-05-27 04:17:57 +00:00
|
|
|
pos += self.renderBuffer[row]
|
|
|
|
.slice(0, Math.min(col, self.dimens.width))
|
2015-05-28 17:03:20 +00:00
|
|
|
.replace(replaceTabsRe, '\t')
|
2015-05-27 04:17:57 +00:00
|
|
|
.length;
|
2015-05-28 17:03:20 +00:00
|
|
|
|
2015-05-27 04:17:57 +00:00
|
|
|
return pos;
|
2015-05-21 05:07:36 +00:00
|
|
|
};
|
2015-05-18 03:33:27 +00:00
|
|
|
|
2015-05-28 04:11:32 +00:00
|
|
|
this.getLineTextLength = function(row) {
|
|
|
|
return self.renderBuffer[row].replace(self.getReplaceTabsRegExp(), '\t').replace(/\n/g, '').length;
|
|
|
|
//return self.renderBuffer[row].replace(/\n/g, '').length;
|
|
|
|
};
|
|
|
|
|
2015-05-29 22:59:21 +00:00
|
|
|
this.getRenderTextLength = function(row) {
|
|
|
|
return self.renderBuffer[row].replace(/\n/g, '').length;
|
|
|
|
};
|
|
|
|
|
2015-05-31 04:03:33 +00:00
|
|
|
// :TODO: this name makes no sense...
|
2015-05-28 04:53:21 +00:00
|
|
|
this.getLineTextLengthToColumn = function(row, col) {
|
|
|
|
return self.renderBuffer[row].replace(self.getReplaceTabsRegExp(), '\t').replace(/\n/g, '').slice(0, col).length;
|
|
|
|
};
|
|
|
|
|
2015-05-31 04:03:33 +00:00
|
|
|
this.getEndOfLineColumn = function(row) {
|
|
|
|
if(!_.isNumber(row)) {
|
|
|
|
row = self.cursorPos.row;
|
|
|
|
}
|
|
|
|
return self.getLineTextLength(row);
|
2015-05-18 03:33:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 23:27:36 +00:00
|
|
|
this.getAbsolutePosition = function(row, col) {
|
2015-05-31 04:03:33 +00:00
|
|
|
if(!_.isNumber(row)) {
|
|
|
|
row = self.cursorPos.row;
|
|
|
|
}
|
|
|
|
if(!_.isNumber(col)) {
|
|
|
|
col = self.cursorPos.col;
|
|
|
|
}
|
2015-05-27 23:27:36 +00:00
|
|
|
return { row : self.position.row + row, col : self.position.col + col };
|
2015-05-18 03:33:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-31 04:03:33 +00:00
|
|
|
// :TODO: rename this to show it is the *buffer*
|
2015-05-27 04:57:34 +00:00
|
|
|
this.getCharAtCursorPosition = function() {
|
|
|
|
var pos = self.getTextBufferPosition(self.cursorPos.row, self.cursorPos.col);
|
|
|
|
return self.textBuffer.get(pos);
|
|
|
|
};
|
|
|
|
|
2015-05-31 04:03:33 +00:00
|
|
|
this.getRenderCharAtRowAndColumn = function(row, col) {
|
|
|
|
return self.renderBuffer[row][col];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getRenderCharAtCursorPosition = function() {
|
|
|
|
return self.getRenderCharAtRowAndColumn(self.cursorPos.row, self.cursorPos.col);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getRemainingRowsFromCurrent = function() {
|
|
|
|
return Math.min(self.dimens.height, self.renderBuffer.length) - (self.cursorPos.row + 1);
|
|
|
|
};
|
|
|
|
|
2015-05-27 23:27:36 +00:00
|
|
|
this.moveCursorTo = function(row, col) {
|
|
|
|
var absPos = self.getAbsolutePosition(row, col);
|
|
|
|
self.client.term.write(ansi.goto(absPos.row, absPos.col));
|
|
|
|
};
|
|
|
|
|
|
|
|
this.scrollUp = function(count) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this.scrollDown = function(count) {
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
|
2015-05-29 22:59:21 +00:00
|
|
|
this.cursorMoveJumpTab = function(cursorDir) {
|
2015-05-31 04:03:33 +00:00
|
|
|
if('\t' !== self.getRenderCharAtCursorPosition()) {
|
|
|
|
return; // nothing to do
|
|
|
|
}
|
2015-05-29 22:59:21 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// A few scenarios:
|
2015-05-31 04:03:33 +00:00
|
|
|
// * Cursor just moved up or down and we got dumped in the middle of a tab sequence.
|
|
|
|
// :TODO: document: Jump to nearest tab right/left. This needs some more research
|
2015-05-29 22:59:21 +00:00
|
|
|
// * Cursor moved left or right: We should be on the first \t in either direction & need to jump
|
|
|
|
// * Tabs may expand to start/end of line -- in this case we should move to the next line
|
|
|
|
//
|
|
|
|
// Example tab sequence when up/down (tabSize=8)
|
|
|
|
// Actual: Hello\tWorld!
|
|
|
|
// Render: Hello\t\t\t\t\t\t\t\tWorld!
|
|
|
|
// ^-- cursor up from here
|
|
|
|
//
|
|
|
|
switch(cursorDir) {
|
|
|
|
case 'left' :
|
|
|
|
self.cursorPos.col -= (self.tabWidth - 1);
|
|
|
|
if(self.cursorPos.col <= 0) {
|
|
|
|
self.cursorToEndOfPreviousLine();
|
|
|
|
} else {
|
|
|
|
self.client.term.write(ansi.left(self.tabWidth - 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'right' :
|
|
|
|
self.cursorPos.col += (self.tabWidth - 1);
|
|
|
|
if(self.cursorPos.col >= self.dimens.width) {
|
|
|
|
self.cursorToStartOfNextLine();
|
|
|
|
} else {
|
|
|
|
self.client.term.write(ansi.right(self.tabWidth - 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'up' :
|
|
|
|
case 'down' :
|
|
|
|
//
|
|
|
|
// We're going to move right, but we need to know where we're at in
|
|
|
|
// in the render buffer expanded tabs
|
|
|
|
//
|
2015-05-31 04:03:33 +00:00
|
|
|
// :TODO: This is not right -- we need to move to *nearest*. Research how
|
|
|
|
// sublime/etc. treat this
|
|
|
|
var col = self.cursorPos.col;
|
2015-05-29 22:59:21 +00:00
|
|
|
var prevTabs = 0;
|
2015-05-31 04:03:33 +00:00
|
|
|
while('\t' === self.getRenderCharAtRowAndColumn(self.cursorPos.row, col--)) {
|
2015-05-29 22:59:21 +00:00
|
|
|
prevTabs++;
|
|
|
|
}
|
2015-05-31 04:03:33 +00:00
|
|
|
|
|
|
|
//console.log('prevTabs: ' + prevTabs)
|
|
|
|
var adjust = self.tabWidth - prevTabs;
|
|
|
|
self.cursorPos.col += adjust;
|
|
|
|
self.client.term.write(ansi.right(adjust));
|
2015-05-29 22:59:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-31 04:03:33 +00:00
|
|
|
this.adjustColumnToEndOfLine = function() {
|
|
|
|
var eolColumn = self.getEndOfLineColumn();
|
|
|
|
if(self.cursorPos.col > eolColumn) {
|
|
|
|
self.cursorPos.col = eolColumn;
|
|
|
|
var absPos = self.getAbsolutePosition(self.cursorPos.row, eolColumn);
|
|
|
|
self.client.term.write(ansi.goto(absPos.row, absPos.col));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.cursorUp = function() {
|
|
|
|
if(self.cursorPos.row > 0) {
|
|
|
|
self.cursorPos.row--;
|
|
|
|
self.client.term.write(ansi.up());
|
|
|
|
|
|
|
|
self.cursorMoveJumpTab('up');
|
|
|
|
} else if(self.topLineIndex > 0) {
|
|
|
|
// :TODO: scroll up if possible to do so
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust to EOL position if needed
|
|
|
|
self.adjustColumnToEndOfLine();
|
|
|
|
};
|
|
|
|
|
2015-05-29 04:17:28 +00:00
|
|
|
this.cursorDown = function() {
|
2015-05-31 04:03:33 +00:00
|
|
|
if(self.getRemainingRowsFromCurrent() > 0) {
|
2015-05-29 04:17:28 +00:00
|
|
|
self.cursorPos.row++;
|
|
|
|
self.client.term.write(ansi.down());
|
2015-05-31 04:03:33 +00:00
|
|
|
|
|
|
|
self.cursorMoveJumpTab('down');
|
|
|
|
} else {
|
|
|
|
// :TODO: scroll if possible
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust to EOL position if needed
|
|
|
|
self.adjustColumnToEndOfLine();
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cursorLeft = function() {
|
|
|
|
if(self.cursorPos.col > 0) {
|
|
|
|
self.cursorPos.col--;
|
|
|
|
self.client.term.write(ansi.left());
|
|
|
|
|
|
|
|
self.cursorMoveJumpTab('left');
|
|
|
|
} else {
|
|
|
|
self.cursorToEndOfPreviousLine();
|
2015-05-29 04:17:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-27 04:57:34 +00:00
|
|
|
this.cursorRight = function() {
|
2015-05-28 17:03:20 +00:00
|
|
|
var rowVisibleLen = self.renderBuffer[self.cursorPos.row].replace(/\n/g, '').length;
|
|
|
|
var max = Math.min(self.dimens.width, rowVisibleLen - 1);//selfself.getLineTextLength(self.cursorPos.row) - 1);
|
|
|
|
console.log('self.dimens.width: ' + self.dimens.width + ' / lineLength: ' + (self.getLineTextLength(self.cursorPos.row) - 1))
|
2015-05-27 04:57:34 +00:00
|
|
|
if(self.cursorPos.col < max) {
|
|
|
|
self.cursorPos.col++;
|
2015-05-27 23:27:36 +00:00
|
|
|
self.client.term.write(ansi.right());
|
2015-05-28 04:11:32 +00:00
|
|
|
|
|
|
|
// make tab adjustment if necessary
|
2015-05-31 04:03:33 +00:00
|
|
|
self.cursorMoveJumpTab('right');
|
2015-05-29 04:17:28 +00:00
|
|
|
} else {
|
2015-05-31 04:03:33 +00:00
|
|
|
self.cursorToStartOfNextLine();
|
2015-05-29 04:17:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-29 22:59:21 +00:00
|
|
|
this.cursorToEndOfPreviousLine = function() {
|
|
|
|
if(self.cursorPos.row > 0) {
|
|
|
|
self.cursorPos.row--;
|
|
|
|
self.cursorPos.col = self.getRenderTextLength(self.cursorPos.row) - 1;
|
|
|
|
self.moveCursorTo(self.cursorPos.row, self.cursorPos.col);
|
|
|
|
} else {
|
|
|
|
// can we scroll??!!!
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cursorToStartOfNextLine = function() {
|
2015-05-31 04:03:33 +00:00
|
|
|
if(self.getRemainingRowsFromCurrent() > 0) {
|
|
|
|
self.cursorPos.row++;
|
|
|
|
self.cursorPos.col = 0;
|
|
|
|
self.moveCursorTo(self.cursorPos.row, self.cursorPos.col);
|
2015-05-27 04:57:34 +00:00
|
|
|
} else {
|
2015-05-31 04:03:33 +00:00
|
|
|
// :TODO: can we scroll??
|
2015-05-27 04:57:34 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-31 04:03:33 +00:00
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
this.getLineIndex = function() {
|
2015-05-18 17:31:35 +00:00
|
|
|
return self.topLineIndex + self.cursorPos.row;
|
2015-05-16 20:39:14 +00:00
|
|
|
};
|
2015-05-19 04:29:33 +00:00
|
|
|
|
2015-05-31 05:45:22 +00:00
|
|
|
this.insertCharacterAtCurrentPosition = function(c) {
|
|
|
|
var pos = self.getTextBufferPosition(self.cursorPos.row, self.cursorPos.col);
|
|
|
|
self.cursorPos.col++;
|
2015-06-01 03:18:23 +00:00
|
|
|
if(self.cursorPos.col >= self.dimens.width) {
|
|
|
|
self.cursorToStartOfNextLine();
|
|
|
|
|
|
|
|
}
|
2015-05-31 05:45:22 +00:00
|
|
|
self.client.term.write(c);
|
|
|
|
self.textBuffer.insert(pos, c);
|
|
|
|
};
|
|
|
|
|
2015-05-16 20:39:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(MultiLineEditTextView, View);
|
|
|
|
|
2015-05-19 04:29:33 +00:00
|
|
|
MultiLineEditTextView.prototype.setPosition = function(pos) {
|
|
|
|
MultiLineEditTextView.super_.prototype.setPosition.call(this, pos);
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-05-16 20:39:14 +00:00
|
|
|
MultiLineEditTextView.prototype.redraw = function() {
|
|
|
|
MultiLineEditTextView.super_.prototype.redraw.call(this);
|
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
this.redrawViewableText();
|
|
|
|
//this.client.term.write(this.text);
|
2015-05-18 03:33:27 +00:00
|
|
|
};
|
|
|
|
|
2015-05-27 23:27:36 +00:00
|
|
|
MultiLineEditTextView.prototype.setFocus = function(focused) {
|
2015-05-18 03:33:27 +00:00
|
|
|
|
|
|
|
MultiLineEditTextView.super_.prototype.setFocus.call(this, focused);
|
2015-05-27 23:27:36 +00:00
|
|
|
|
|
|
|
this.moveCursorTo(this.cursorPos.row, this.cursorPos.col);
|
2015-05-31 05:45:22 +00:00
|
|
|
this.client.term.write(this.getSGR());
|
2015-05-18 03:33:27 +00:00
|
|
|
};
|
2015-05-27 23:27:36 +00:00
|
|
|
|
2015-05-16 20:39:14 +00:00
|
|
|
|
|
|
|
MultiLineEditTextView.prototype.setText = function(text) {
|
2015-05-18 17:31:35 +00:00
|
|
|
//this.cursorPos.row = this.position.row + this.dimens.height;
|
2015-05-21 05:07:36 +00:00
|
|
|
//this.lines = this.wordWrap(text);
|
|
|
|
|
|
|
|
if(this.textBuffer.length > 0) { // :TODO: work around GapBuffer bug: if it's already empty this will cause gapEnd to be undefined
|
|
|
|
this.textBuffer.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//this.textBuffer.insertAll(0, text);
|
|
|
|
text = text.replace(/\b/g, '');
|
|
|
|
|
2015-05-22 04:06:35 +00:00
|
|
|
this.textBuffer.insertAll(0, text);
|
|
|
|
|
2015-05-27 04:17:57 +00:00
|
|
|
this.updateRenderBuffer();
|
2015-05-19 04:29:33 +00:00
|
|
|
|
2015-05-21 05:07:36 +00:00
|
|
|
console.log(this.renderBuffer)
|
2015-05-27 23:27:36 +00:00
|
|
|
/*
|
2015-05-27 04:17:57 +00:00
|
|
|
var idx = this.getTextBufferPosition(4, 0);
|
|
|
|
for(var i = idx; i < idx + 4; ++i) {
|
|
|
|
console.log(i + ' = "' + this.textBuffer.asArray()[i] + '"');
|
|
|
|
}
|
2015-05-27 04:57:34 +00:00
|
|
|
this.cursorPos.row = 15;
|
|
|
|
this.cursorPos.col = 0;
|
2015-05-27 23:27:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
this.cursorPos.row = 14;
|
|
|
|
this.cursorPos.col = 0;
|
|
|
|
};
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-31 05:45:22 +00:00
|
|
|
MultiLineEditTextView.prototype.onKeyPress = function(key, isSpecial) {
|
|
|
|
if(isSpecial) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(1 === key.length);
|
|
|
|
|
|
|
|
this.insertCharacterAtCurrentPosition(key);
|
|
|
|
this.updateRenderBuffer();
|
|
|
|
|
|
|
|
// :TODO: is save/restore supported enough? Should we do it ourselves?
|
|
|
|
this.client.term.write(ansi.savePos());
|
|
|
|
// :TODO: Just draw from position onward
|
|
|
|
this.redraw();
|
|
|
|
this.client.term.write(ansi.restorePos());
|
|
|
|
|
|
|
|
|
|
|
|
MultiLineEditTextView.super_.prototype.onKeyPress.call(this, key, isSpecial);
|
|
|
|
};
|
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
MultiLineEditTextView.prototype.onSpecialKeyPress = function(keyName) {
|
|
|
|
if(this.isSpecialKeyMapped('up', keyName)) {
|
2015-05-21 05:07:36 +00:00
|
|
|
this.cursorUp();
|
2015-05-18 03:33:27 +00:00
|
|
|
} else if(this.isSpecialKeyMapped('down', keyName)) {
|
2015-05-29 04:17:28 +00:00
|
|
|
this.cursorDown();
|
2015-05-18 03:33:27 +00:00
|
|
|
} else if(this.isSpecialKeyMapped('left', keyName)) {
|
2015-05-27 04:57:34 +00:00
|
|
|
this.cursorLeft();
|
2015-05-18 03:33:27 +00:00
|
|
|
} else if(this.isSpecialKeyMapped('right', keyName)) {
|
2015-05-27 04:57:34 +00:00
|
|
|
this.cursorRight();
|
2015-05-18 03:33:27 +00:00
|
|
|
}
|
2015-05-16 20:39:14 +00:00
|
|
|
|
2015-05-28 04:11:32 +00:00
|
|
|
console.log(
|
|
|
|
'row=' + this.cursorPos.row + ' / col=' + this.cursorPos.col +
|
|
|
|
' / abs=' + JSON.stringify(this.getAbsolutePosition()) +
|
|
|
|
': ' + this.getCharAtCursorPosition() + '( ' + this.getCharAtCursorPosition().charCodeAt(0) + ')')
|
2015-05-27 04:57:34 +00:00
|
|
|
|
2015-05-18 03:33:27 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.onSpecialKeyPress.call(this, keyName);
|
2015-05-27 23:27:36 +00:00
|
|
|
};
|