From dc69428563fe637c9d3d2c859e8597508232c693 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Thu, 18 Jun 2015 22:17:51 -0600 Subject: [PATCH] * Some code cleanup * WIP additional terminal types * Minor updates to deleting lines in multi line edit - much to go still --- core/client_term.js | 23 ++++++++++++++--- core/multi_line_edit_text_view2.js | 40 +++++++++++++++++++++++++++--- core/servers/telnet.js | 12 +++++---- 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/core/client_term.js b/core/client_term.js index 1da28b5c..cbeb3639 100644 --- a/core/client_term.js +++ b/core/client_term.js @@ -6,6 +6,7 @@ var Log = require('./logger.js').log; var iconv = require('iconv-lite'); var assert = require('assert'); +var _ = require('lodash'); iconv.extendNodeEncodings(); @@ -56,12 +57,25 @@ function ClientTerminal(output) { // // ANSI terminals should be encoded to CP437 // - if('ansi' == termType) { + // Some terminal types provided by Mercyful Fate / Enthral: + // ANSI-BBS + // PC-ANSI + // QANSI + // SCOANSI + // VT100 + // XTERM + // LINUX + // QNX + // SCREEN + // + if(this.isANSI()) { this.outputEncoding = 'cp437'; } else { // :TODO: See how x84 does this -- only set if local/remote are binary this.outputEncoding = 'utf8'; } + + Log.debug( { encoding : this.outputEncoding }, 'Set output encoding due to terminal type change'); } }); @@ -89,12 +103,13 @@ function ClientTerminal(output) { } ClientTerminal.prototype.isANSI = function() { - return 'ansi' === this.termType; + // :TODO: Others?? + return [ 'ansi', 'pc-ansi', 'qansi', 'scoansi' ].indexOf(this.termType) > -1; }; ClientTerminal.prototype.write = function(s, convertLineFeeds) { - convertLineFeeds = typeof convertLineFeeds === 'undefined' ? this.convertLF : convertLineFeeds; - if(convertLineFeeds && typeof s === 'string') { + convertLineFeeds = _.isUndefined(convertLineFeeds) ? this.convertLF : convertLineFeeds; + if(convertLineFeeds && _.isString(s)) { s = s.replace(/\n/g, '\r\n'); } this.output.write(iconv.encode(s, this.outputEncoding)); diff --git a/core/multi_line_edit_text_view2.js b/core/multi_line_edit_text_view2.js index b7adca47..94c3b79a 100644 --- a/core/multi_line_edit_text_view2.js +++ b/core/multi_line_edit_text_view2.js @@ -146,11 +146,42 @@ function MultiLineEditTextView2(options) { } self.client.term.write(ansi.showCursor()); + + return absPos.row - self.position.row; // row we ended on + }; + + this.eraseRows = function(startRow, endRow) { + self.client.term.write(self.getSGRFor('text') + ansi.hideCursor()); + + var absPos = self.getAbsolutePosition(startRow, 0); + var absPosEnd = self.getAbsolutePosition(endRow, 0); + var eraseFiller = new Array(self.dimens.width).join(' '); + + while(absPos.row < absPosEnd.row) { + self.client.term.write(ansi.goto(absPos.row++, absPos.col)); + self.client.term.write(eraseFiller); + } + + self.client.term.write(ansi.showCursor()); }; this.redrawVisibleArea = function() { assert(self.topVisibleIndex <= self.textLines.length); - self.redrawRows(0, self.dimens.height); + var lastRow = self.redrawRows(0, self.dimens.height); + + self.eraseRows(lastRow, self.dimens.height); + /* + + // :TOOD: create eraseRows(startRow, endRow) + if(lastRow < self.dimens.height) { + var absPos = self.getAbsolutePosition(lastRow, 0); + var empty = new Array(self.dimens.width).join(' '); + while(lastRow++ < self.dimens.height) { + self.client.term.write(ansi.goto(absPos.row++, absPos.col)); + self.client.term.write(empty); + } + } + */ }; this.getVisibleText = function(index) { @@ -164,7 +195,7 @@ function MultiLineEditTextView2(options) { if(!_.isNumber(index)) { index = self.getTextLinesIndex(); } - return self.textLines.length > index ? self.textLines[index].text : '' + return self.textLines.length > index ? self.textLines[index].text : ''; }; this.getCharacter = function(index, col) { @@ -322,8 +353,10 @@ function MultiLineEditTextView2(options) { var remove = (endIndex - startIndex) + 1; console.log('remove=' + remove) + console.log('lenBefore=' + self.textLines.length) self.textLines.splice(startIndex, remove); console.log(self.textLines) + console.log('lenAfter=' + self.textLines.length) self.cursorPos.col = 0; @@ -336,6 +369,7 @@ function MultiLineEditTextView2(options) { } else { self.cursorPos.row -= (index - startIndex); + console.log('self.cursorPos.row=' + self.cursorPos.row) } self.redrawVisibleArea(); @@ -979,7 +1013,7 @@ MultiLineEditTextView2.prototype.setFocus = function(focused) { MultiLineEditTextView2.prototype.setText = function(text) { this.textLines = [ ]; //text = "Tab:\r\n\tA\tB\tC\tD\tE\tF\tG\r\n reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally long word!!!"; - text = require('fs').readFileSync('/home/bashby/Downloads/test_text.txt', { encoding : 'utf-8'}); + text = require('fs').readFileSync('/home/nuskooler/Downloads/test_text.txt', { encoding : 'utf-8'}); this.insertRawText(text);//, 0, 0); this.cursorEndOfDocument(); diff --git a/core/servers/telnet.js b/core/servers/telnet.js index 6aab0a0b..4b51ce33 100644 --- a/core/servers/telnet.js +++ b/core/servers/telnet.js @@ -143,7 +143,7 @@ var OPTIONS = { //PRAGMA_HEARTBEAT : 140 EXTENDED_OPTIONS_LIST : 255, // RFC 861 (STD 32) -} +}; // Commands used within NEW_ENVIRONMENT[_DEP] var NEW_ENVIRONMENT_COMMANDS = { @@ -171,7 +171,7 @@ var COMMAND_IMPLS = {}; return MORE_DATA_REQUIRED; } return parseOption(bufs, i, event); - } + }; }); // :TODO: See TooTallNate's telnet.js: Handle COMMAND_IMPL for IAC in binary mode @@ -342,7 +342,9 @@ OPTION_IMPLS[OPTIONS.NEW_ENVIRONMENT] = function(bufs, i, event) { // :TODO: Currently not supporting ESCaped values (ESC + ). Probably not really in the wild, but we should be compliant var params = []; var p = 0; - for(var j = 0, l = buf.length; j < l; ++j) { + var j; + var l; + for(j = 0, l = buf.length; j < l; ++j) { if(NEW_ENVIRONMENT_DELIMITERS.indexOf(buf[j]) === -1) { continue; } @@ -359,7 +361,7 @@ OPTION_IMPLS[OPTIONS.NEW_ENVIRONMENT] = function(bufs, i, event) { var varName; event.envVars = {}; // :TODO: handle cases where a variable was present in a previous exchange, but missing here...e.g removed - for(var j = 0; j < params.length; ++j) { + for(j = 0; j < params.length; ++j) { if(params[j].length < 2) { continue; } @@ -383,7 +385,7 @@ var MORE_DATA_REQUIRED = 0xfeedface; function parseBufs(bufs) { assert(bufs.length >= 2); - assert(bufs.get(0) === COMMANDS.IAC) + assert(bufs.get(0) === COMMANDS.IAC); return parseCommand(bufs, 1, {}); }