diff --git a/core/ansi_term.js b/core/ansi_term.js index a3acf72a..4dd7846d 100644 --- a/core/ansi_term.js +++ b/core/ansi_term.js @@ -41,8 +41,13 @@ var ESC_CSI = '\u001b['; var CONTROL = { up : 'A', down : 'B', + forward : 'C', + right : 'C', + back : 'D', + left : 'D', + nextLine : 'E', prevLine : 'F', horizAbsolute : 'G', diff --git a/core/multi_line_edit_text_view.js b/core/multi_line_edit_text_view.js index b8e8c96e..e07f164e 100644 --- a/core/multi_line_edit_text_view.js +++ b/core/multi_line_edit_text_view.js @@ -210,9 +210,13 @@ function MultiLineEditTextView(options) { } }; + this.getReplaceTabsRegExp = function() { + return new RegExp('\\t{' + (self.tabWidth - 1) + '}', 'g'); + }; + this.getTextBufferPosition = function(row, col) { - var line = self.renderBuffer[row]; - var replaceTabsRe = new RegExp('\\t{' + (self.tabWidth - 1) + '}', 'g'); + var line = self.renderBuffer[row]; + var replaceTabsRe = self.getReplaceTabsRegExp(); var pos = 0; for(var r = 0; r < row; ++r) { if(self.renderBuffer[r].length > 0) { @@ -227,13 +231,16 @@ function MultiLineEditTextView(options) { .length; return pos; }; - - this.scrollUp = function(count) { + this.getEndOfLinePosition = function(row) { + row = row || self.cursorPos.row; + return self.position.col + self.renderBuffer[row].length; }; - this.scrollDown = function(count) { - + this.getAbsolutePosition = function(row, col) { + row = row || self.cursorPos.row; + col = col || self.cursorPos.col; + return { row : self.position.row + row, col : self.position.col + col }; }; this.getCharAtCursorPosition = function() { @@ -241,25 +248,54 @@ function MultiLineEditTextView(options) { return self.textBuffer.get(pos); }; + 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) { + + }; + this.cursorUp = function() { if(self.cursorPos.row > 0) { self.cursorPos.row--; - + self.client.term.write(ansi.up()); } else if(self.topLineIndex > 0) { - // :TODO: scroll + // :TODO: scroll up if possible to do so } + var endOfLinePos = self.getEndOfLinePosition(); + console.log('col=' + self.cursorPos.col + ' / eolPos=' + endOfLinePos) + if(self.cursorPos.col > endOfLinePos) { + self.client.term.write(ansi.right(self.cursorPos.col - endOfLinePos)); + self.cursorPos.col = endOfLinePos; + } + + // :TODO: if there is text @ cursor y position we're ok, otherwise, // jump to the end of the line + + }; this.cursorRight = function() { var max = Math.min(self.dimens.width, self.renderBuffer[self.cursorPos.row].length); if(self.cursorPos.col < max) { self.cursorPos.col++; + self.client.term.write(ansi.right()); } else { - + if(self.cursorPos.row > 0) { + self.cursorPos.row--; + self.cursorPos.col = 0; + self.moveCursorTo(self.cursorPos.row, self.cursorPos.col); + } } + }; this.cursorLeft = function() { @@ -295,11 +331,13 @@ MultiLineEditTextView.prototype.redraw = function() { //this.client.term.write(this.text); }; -/*MultiLineEditTextView.prototype.setFocus = function(focused) { +MultiLineEditTextView.prototype.setFocus = function(focused) { MultiLineEditTextView.super_.prototype.setFocus.call(this, focused); + + this.moveCursorTo(this.cursorPos.row, this.cursorPos.col); }; -*/ + MultiLineEditTextView.prototype.setText = function(text) { //this.cursorPos.row = this.position.row + this.dimens.height; @@ -317,14 +355,18 @@ MultiLineEditTextView.prototype.setText = function(text) { this.updateRenderBuffer(); console.log(this.renderBuffer) - + /* var idx = this.getTextBufferPosition(4, 0); for(var i = idx; i < idx + 4; ++i) { console.log(i + ' = "' + this.textBuffer.asArray()[i] + '"'); } this.cursorPos.row = 15; this.cursorPos.col = 0; -} + */ + + this.cursorPos.row = 14; + this.cursorPos.col = 0; +}; MultiLineEditTextView.prototype.onSpecialKeyPress = function(keyName) { if(this.isSpecialKeyMapped('up', keyName)) { @@ -337,7 +379,7 @@ MultiLineEditTextView.prototype.onSpecialKeyPress = function(keyName) { this.cursorRight(); } - console.log(this.getCharAtCursorPosition()); + console.log(JSON.stringify(this.getAbsolutePosition()) + ': ' + this.getCharAtCursorPosition()) MultiLineEditTextView.super_.prototype.onSpecialKeyPress.call(this, keyName); -} \ No newline at end of file +}; diff --git a/mods/art/demo_multi_line_edit_text_view1.ans b/mods/art/demo_multi_line_edit_text_view1.ans new file mode 100644 index 00000000..b38c0372 Binary files /dev/null and b/mods/art/demo_multi_line_edit_text_view1.ans differ diff --git a/package.json b/package.json index bb784020..1f354273 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,8 @@ "lodash" : "3.7.x", "sqlite3" : "3.0.x", "ssh2" : "0.4.x", - "strip-json-comments" : "1.0.x" + "strip-json-comments" : "1.0.x", + "gapbuffer" : "0.0.2" }, "engine" : "node >= 0.12.2" } \ No newline at end of file