From abaca6c8ef584e4f252d908831fc020d53b60428 Mon Sep 17 00:00:00 2001 From: Nathan Byrd Date: Mon, 28 Mar 2022 14:18:00 -0500 Subject: [PATCH] Fixed changes from code review --- core/ansi_escape_parser.js | 2 +- core/config_default.js | 6 ++++ core/connect.js | 57 ++++++++++++++++++++++++++++++++++++++ core/menu_module.js | 4 +-- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/core/ansi_escape_parser.js b/core/ansi_escape_parser.js index c6d1276c..73c4f377 100644 --- a/core/ansi_escape_parser.js +++ b/core/ansi_escape_parser.js @@ -24,7 +24,7 @@ function ANSIEscapeParser(options) { this.scrollBack = 0; this.graphicRendition = {}; - if(options.startRow != null) { + if(!_.isNil(options.startRow)) { this.row = options.startRow; } else { diff --git a/core/config_default.js b/core/config_default.js index d5b71ed7..c51fb6a1 100644 --- a/core/config_default.js +++ b/core/config_default.js @@ -23,6 +23,12 @@ module.exports = () => { // during the connect process, but provides better autoconfiguration of utf-8 checkUtf8Encoding : true, + // Checking the ANSI home position also requires the use of cursor position reports, which are not + // supported on all terminals. Using this with a terminal that does not support cursor position reports + // results in a 3 second delay during the connect process, but works around positioning problems with + // non-standard terminals. + checkAnsiHomePosition: true, + // List of terms that should be assumed to use cp437 encoding cp437TermList : ['ansi', 'pcansi', 'pc-ansi', 'ansi-bbs', 'qansi', 'scoansi', 'syncterm', 'ansi-256color', 'ansi-256color-rgb'], // List of terms that should be assumed to use utf8 encoding diff --git a/core/connect.js b/core/connect.js index d20aae39..9acf002f 100644 --- a/core/connect.js +++ b/core/connect.js @@ -12,6 +12,58 @@ const async = require('async'); exports.connectEntry = connectEntry; +function ansiDiscoverHomePosition(client, cb) { + // + // We want to find the home position. ANSI-BBS and most terminals + // utilize 1,1 as home. However, some terminals such as ConnectBot + // think of home as 0,0. If this is the case, we need to offset + // our positioning to accommodate for such. + // + + + if( !Config().term.checkAnsiHomePosition ) { + // Skip (and assume 1,1) if the home position check is disabled. + return cb(null); + } + + const done = (err) => { + client.removeListener('cursor position report', cprListener); + clearTimeout(giveUpTimer); + return cb(err); + }; + + const cprListener = function(pos) { + const h = pos[0]; + const w = pos[1]; + + // + // We expect either 0,0, or 1,1. Anything else will be filed as bad data + // + if(h > 1 || w > 1) { + client.log.warn( { height : h, width : w }, 'Ignoring ANSI home position CPR due to unexpected values'); + return done(Errors.UnexpectedState('Home position CPR expected to be 0,0, or 1,1')); + } + + if(0 === h & 0 === w) { + // + // Store a CPR offset in the client. All CPR's from this point on will offset by this amount + // + client.log.info('Setting CPR offset to 1'); + client.cprOffset = 1; + } + + return done(null); + }; + + client.once('cursor position report', cprListener); + + const giveUpTimer = setTimeout( () => { + return done(Errors.General('Giving up on home position CPR')); + }, 3000); // 3s + + client.term.write(`${ansi.goHome()}${ansi.queryPos()}`); // go home, query pos +} + function ansiAttemptDetectUTF8(client, cb) { // // Trick to attempt and detect UTF-8. While there is a lot more than @@ -173,6 +225,11 @@ function connectEntry(client, nextMenu) { term.rawWrite(ansi.queryDeviceAttributes(0)); return callback(null); }, + function discoverHomePosition(callback) { + ansiDiscoverHomePosition(client, () => { + return callback(null); // we try to continue anyway + }); + }, function queryTermSizeByNonStandardAnsi(callback) { ansiQueryTermSizeIfNeeded(client, err => { if(err) { diff --git a/core/menu_module.js b/core/menu_module.js index ef6ad831..54bda2c7 100644 --- a/core/menu_module.js +++ b/core/menu_module.js @@ -105,7 +105,7 @@ exports.MenuModule = class MenuModule extends PluginModule { const options = self.menuConfig.config; - if(artData != null && artData.height != null) { + if(!_.isNil(artData) && _.isNumber(artData.height)) { options.startRow = artData.height + 1; } @@ -127,7 +127,7 @@ exports.MenuModule = class MenuModule extends PluginModule { }, function displayPauseIfRequested(callback) { if(!self.shouldPause()) { - return callback(null); + return callback(null, null); } if(self.client.term.termHeight > 0 && pausePosition.row > self.client.termHeight) {