diff --git a/static/js/terminal/index.js b/static/js/terminal/index.js index cb291d1..0431e38 100644 --- a/static/js/terminal/index.js +++ b/static/js/terminal/index.js @@ -8,6 +8,12 @@ var states = require('./lib/states'); module.exports = Terminal; +/** + * iterator + * @param from + * @param iterator + * @param context + */ function iterator(from, iterator, context){ for (var key in from) { if (from.hasOwnProperty(key)) { @@ -16,11 +22,18 @@ function iterator(from, iterator, context){ } } +/** + * Terminal + * @param options + * @returns {Terminal} + * @constructor + */ function Terminal(options){ options = options || {}; if (!(this instanceof Terminal)) return new Terminal(options); + // inherits iterator(Terminal.defaults, function (key, value){ if (options.hasOwnProperty(options)) { this[key] = options[key]; @@ -30,6 +43,7 @@ function Terminal(options){ } }, this); + // set colors if (Array.isArray(options.colors)) { if (options.colors.length === 8) { options.colors = options.colors.concat(Terminal.colors.slice(8)); @@ -46,36 +60,43 @@ function Terminal(options){ options.colors = Terminal.colors; } - this.cols = options.cols || Terminal.geometry[0]; - this.rows = options.rows || Terminal.geometry[1]; - this.colors = options.colors; this.bgColor = options.bgColor || Terminal.defaultColors.bgColor; this.fgColor = options.fgColor || Terminal.defaultColors.fgColor; + // set screen size + options.cols = options.cols || Terminal.geometry[0]; + options.rows = options.rows || Terminal.geometry[1]; + this.cols = options.cols; + this.rows = options.rows; + + // set handler options.handler = typeof options.handler === 'function' ? options.handler : function (){}; this.handler = options.handler; + // set handle title options.handleTitle = typeof options.handleTitle === 'function' ? options.handleTitle : function (){}; this.handleTitle = options.handleTitle; + // set convert eol options.convertEol = options.convertEol === true; this.convertEol = options.convertEol; + // set options this.options = options; - this.ybase = 0; - this.ydisp = 0; + // set property this.x = 0; this.y = 0; + this.ybase = 0; + this.ydisp = 0; this.cursorState = 0; - this.cursorHidden = false; this.state = states.normal; this.queue = ''; this.scrollTop = 0; this.scrollBottom = this.rows - 1; - // modes + // Modes this.applicationKeypad = false; this.originMode = false; this.insertMode = false; @@ -101,24 +122,28 @@ function Terminal(options){ this.readable = true; this.writable = true; + // set attr this.defAttr = (257 << 9) | 256; this.curAttr = this.defAttr; + // set params this.params = []; this.currentParam = 0; this.prefix = ''; this.postfix = ''; + // set lines this.lines = []; + // set tabs + this.tabs = null; + var i = this.rows; while (i--) { this.lines.push(this.blankLine()); } - this.tabs = null; - this.setupStops(); } diff --git a/static/js/terminal/lib/blankLine.js b/static/js/terminal/lib/blankLine.js index 739cd24..0612038 100644 --- a/static/js/terminal/lib/blankLine.js +++ b/static/js/terminal/lib/blankLine.js @@ -5,6 +5,11 @@ 'use strict'; module.exports = function (Terminal){ + /** + * blankLine + * @param cur + * @returns {Array} + */ Terminal.prototype.blankLine = function (cur){ var attr = cur ? this.eraseAttr() : this.defAttr; var ch = [attr, ' ']; diff --git a/static/js/terminal/lib/charsets.js b/static/js/terminal/lib/charsets.js index 11ac31c..4587605 100644 --- a/static/js/terminal/lib/charsets.js +++ b/static/js/terminal/lib/charsets.js @@ -48,7 +48,7 @@ module.exports = function (Terminal){ '}': '\u00a3', // '£' '~': '\u00b7' // '·' }; - + Terminal.charsets.UK = null; // (A Terminal.charsets.US = null; // (B (USASCII) Terminal.charsets.Dutch = null; // (4 diff --git a/static/js/terminal/lib/close.js b/static/js/terminal/lib/close.js index 68e5210..f4defa6 100644 --- a/static/js/terminal/lib/close.js +++ b/static/js/terminal/lib/close.js @@ -5,6 +5,9 @@ 'use strict'; module.exports = function (Terminal){ + /** + * close + */ Terminal.prototype.close = function (){ this.lines = []; this.children = []; diff --git a/static/js/terminal/lib/csi/charAttributes.js b/static/js/terminal/lib/csi/charAttributes.js index 3da35ef..602f465 100644 --- a/static/js/terminal/lib/csi/charAttributes.js +++ b/static/js/terminal/lib/csi/charAttributes.js @@ -5,6 +5,13 @@ 'use strict'; module.exports = function (Terminal){ + /** + * matchColor + * @param r1 + * @param g1 + * @param b1 + * @returns {*} + */ function matchColor(r1, g1, b1){ var hash = (r1 << 16) | (g1 << 8) | b1; diff --git a/static/js/terminal/lib/csi/mode.js b/static/js/terminal/lib/csi/mode.js index ff5d3a7..411cb29 100644 --- a/static/js/terminal/lib/csi/mode.js +++ b/static/js/terminal/lib/csi/mode.js @@ -189,7 +189,9 @@ module.exports = function (Terminal){ break; // show cursor case 25: - this.cursorHidden = false; + this.cursor = true; + + this.showCursor(); break; // alt screen buffer cursor case 1049: @@ -382,7 +384,9 @@ module.exports = function (Terminal){ break; // hide cursor case 25: - this.cursorHidden = true; + this.cursor = false; + + this.hideCursor(); break; // alt screen buffer cursor case 1049: diff --git a/static/js/terminal/lib/csi/softReset.js b/static/js/terminal/lib/csi/softReset.js index 4c70573..b4b9e5c 100644 --- a/static/js/terminal/lib/csi/softReset.js +++ b/static/js/terminal/lib/csi/softReset.js @@ -8,7 +8,6 @@ module.exports = function (Terminal){ // CSI ! p Soft terminal reset (DECSTR). // http://vt100.net/docs/vt220-rm/table4-10.html Terminal.prototype.softReset = function (){ - this.cursorHidden = false; this.insertMode = false; this.originMode = false; // autowrap diff --git a/static/js/terminal/lib/cursor.js b/static/js/terminal/lib/cursor.js index 2a6d850..dfbb8fd 100644 --- a/static/js/terminal/lib/cursor.js +++ b/static/js/terminal/lib/cursor.js @@ -5,38 +5,73 @@ 'use strict'; module.exports = function (Terminal){ - Terminal.prototype.blinkCursor = function (){ - if (Terminal.focus !== this) return; - - this.cursorState ^= 1; - this.refresh(this.y, this.y); - }; - + /** + * showCursor + */ Terminal.prototype.showCursor = function (){ - if (!this.cursorState) { + if (this.cursor) { + this._cursor = true; this.cursorState = 1; this.refresh(this.y, this.y); } }; - Terminal.prototype.startBlink = function (){ - if (!this.cursorBlink) return; + /** + * hideCursor + */ + Terminal.prototype.hideCursor = function (){ + if (this._cursor) { + delete this._cursor; - var context = this; + this.cursorState = 0; - this._blinker = function (){ - context.blinkCursor(); - }; + if (this._blink && this._blinker) { + clearInterval(this._blink); - this._blink = setInterval(this._blinker, 500); + delete this._blink; + delete this._blinker; + } + + this.refresh(this.y, this.y); + } }; - Terminal.prototype.refreshBlink = function (){ - if (!this.cursorBlink) return; + /** + * startBlink + */ + Terminal.prototype.startBlink = function (){ + if (this.cursor && this.cursorBlink && Terminal.focus === this) { + var context = this; - clearInterval(this._blink); + this.stopBlink(); + this._blinker = function (){ + context.cursorState ^= 1; - this._blink = setInterval(this._blinker, 500); + context.refresh(this.y, this.y); + }; + + this._blink = setInterval(this._blinker, this.cursorBlinkSpeed); + } + }; + + /** + * stopBlink + */ + Terminal.prototype.stopBlink = function (){ + if (this._blink && this._blinker) { + clearInterval(this._blink); + + delete this._blink; + delete this._blinker; + + if (this.cursor && this._cursor) { + this.cursorState = 1; + } else { + this.cursorState = 0; + } + + this.refresh(this.y, this.y); + } }; }; diff --git a/static/js/terminal/lib/focused.js b/static/js/terminal/lib/focused.js index 00a0c94..0720f1b 100644 --- a/static/js/terminal/lib/focused.js +++ b/static/js/terminal/lib/focused.js @@ -22,8 +22,7 @@ module.exports = function (Terminal){ Terminal.prototype.blur = function (){ if (Terminal.focus !== this) return; - this.cursorState = 0; - this.refresh(this.y, this.y); + this.hideCursor(); Terminal.focus = null; }; diff --git a/static/js/terminal/lib/options.js b/static/js/terminal/lib/options.js index fa5b5a9..0db661c 100644 --- a/static/js/terminal/lib/options.js +++ b/static/js/terminal/lib/options.js @@ -8,7 +8,9 @@ module.exports = function (Terminal){ Terminal.termName = 'xterm'; Terminal.debug = false; Terminal.geometry = [100, 80]; + Terminal.cursor = true; Terminal.cursorBlink = true; + Terminal.cursorBlinkSpeed = 500; Terminal.visualBell = true; Terminal.popOnBell = true; Terminal.scrollback = 640; @@ -19,7 +21,9 @@ module.exports = function (Terminal){ Terminal.defaults = { debug: Terminal.debug, termName: Terminal.termName, + cursor: Terminal.cursor, cursorBlink: Terminal.cursorBlink, + cursorBlinkSpeed: Terminal.cursorBlinkSpeed, visualBell: Terminal.visualBell, popOnBell: Terminal.popOnBell, scrollback: Terminal.scrollback, diff --git a/static/js/terminal/lib/refresh.js b/static/js/terminal/lib/refresh.js index 4a123f6..dbb6bfb 100644 --- a/static/js/terminal/lib/refresh.js +++ b/static/js/terminal/lib/refresh.js @@ -40,7 +40,7 @@ module.exports = function (Terminal){ row = y + this.ydisp; line = this.lines[row]; - if (y === this.y && this.cursorState && this.ydisp === this.ybase && !this.cursorHidden) { + if (y === this.y && this.cursor && this.cursorState && this.ydisp === this.ybase) { x = this.x; } else { x = -1; diff --git a/static/js/terminal/lib/stops.js b/static/js/terminal/lib/stops.js index 5bcd7e7..84dd453 100644 --- a/static/js/terminal/lib/stops.js +++ b/static/js/terminal/lib/stops.js @@ -6,6 +6,10 @@ // ignore warnings regarging == and != (coersion makes things work here appearently) module.exports = function (Terminal){ + /** + * setupStops + * @param i + */ Terminal.prototype.setupStops = function (i){ if (arguments.length) { if (!this.tabs[i]) { @@ -21,6 +25,11 @@ module.exports = function (Terminal){ } }; + /** + * prevStop + * @param x + * @returns {number} + */ Terminal.prototype.prevStop = function (x){ if (!arguments.length) x = this.x; @@ -29,6 +38,11 @@ module.exports = function (Terminal){ return x >= this.cols ? this.cols - 1 : x < 0 ? 0 : x; }; + /** + * nextStop + * @param x + * @returns {number} + */ Terminal.prototype.nextStop = function (x){ if (!arguments.length) x = this.x;