diff --git a/lib/tput.js b/lib/tput.js index 6855b8c..6b44f0c 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -43,7 +43,7 @@ function Tput(options) { this.terminal = options.term || options.terminal || process.env.TERM - || 'xterm'; + || (process.platform === 'win32' ? 'windows-ansi' : 'xterm'); this.debug = options.debug; this.padding = options.padding; @@ -63,25 +63,25 @@ function Tput(options) { Tput.prototype.setup = function() { try { if (this.termcap) { - this.injectTermcap(); + try { + this.injectTermcap(); + } catch (e) { + if (this.debug) throw e; + this._useInternalCap(this.terminal); + } } else { - this.injectTerminfo(); + try { + this.injectTerminfo(); + } catch (e) { + if (this.debug) throw e; + this._useInternalInfo(this.terminal); + } } } catch (e) { // If there was an error, fallback // to an internally stored terminfo/cap. - if (this.debug) { - throw e; - } - - if (process.platform === 'win32') { - try { - this.injectTerminfo(__dirname + '/../usr/' + this.terminal); - return; - } catch (e) {} - } - - this._useXtermI(); + if (this.debug) throw e; + this._useXtermInfo(); } }; @@ -98,18 +98,28 @@ Tput.prototype._debug = function() { * Fallback */ -Tput.prototype._useVt102C = function() { +Tput.prototype._useVt102Cap = function() { return this.injectTermcap('vt102'); }; -Tput.prototype._useXtermC = function() { +Tput.prototype._useXtermCap = function() { return this.injectTermcap(__dirname + '/../usr/xterm.termcap'); }; -Tput.prototype._useXtermI = function() { +Tput.prototype._useXtermInfo = function() { return this.injectTerminfo(__dirname + '/../usr/xterm'); }; +Tput.prototype._useInternalInfo = function(name) { + name = path.basename(name); + return this.injectTerminfo(__dirname + '/../usr/' + name); +}; + +Tput.prototype._useInternalCap = function(name) { + name = path.basename(name); + return this.injectTermcap(__dirname + '/../usr/' + name + '.termcap'); +}; + /** * Terminfo */ @@ -133,7 +143,7 @@ Tput.prototype.readTerminfo = function(term) { Tput._prefix = Tput.prototype._prefix = function(term) { - return (term && (~term.indexOf(path.sep)) && term) + return (term && ~term.indexOf(path.sep) && term) || (term && this.terminfoFile) || this._tprefix(this.terminfoPrefix, term) || this._tprefix(process.env.TERMINFO, term) @@ -1107,7 +1117,7 @@ Tput.prototype.readTermcap = function(term) { // Termcap has a bunch of terminals usually stored in one file/string, // so we need to find the one containing our desired terminal. - if (term[0] === '/' && (terms = this._tryCap(term))) { + if (~term.indexOf(path.sep) && (terms = this._tryCap(path.resolve(term)))) { term_ = path.basename(term).split('.')[0]; if (terms[process.env.TERM]) { term = process.env.TERM; @@ -1145,7 +1155,8 @@ Tput.prototype.readTermcap = function(term) { ? terms[term.strings.tc].names : [term.strings.tc]; - self._debug(term.names.join('/') + ' inherits from ' + names.join('/')); + self._debug('%s inherits from %s.', + term.names.join('/'), names.join('/')); var inherit = tc(terms[term.strings.tc]); if (inherit) { @@ -1178,7 +1189,12 @@ Tput.prototype._tryCap = function(file, term) { return; } - data = file[0] === '/' ? tryRead(file) : file; + // If the termcap string starts with `/`, + // ncurses considers it a filename. + data = file[0] === '/' + ? tryRead(file) + : file; + if (!data) return; terms = this.parseTermcap(data, file); @@ -1246,7 +1262,7 @@ Tput.prototype.parseTermcap = function(data, file) { name: names[0], names: names, desc: names.pop(), - file: file[0] === '/' + file: ~file[0].indexOf(path.sep) ? path.resolve(file) : file, termcap: true