32 lines
54 KiB
JavaScript
32 lines
54 KiB
JavaScript
|
/**
|
|||
|
* term.js - an xterm emulator
|
|||
|
* Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
|
|||
|
* https://github.com/chjj/term.js
|
|||
|
*
|
|||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|||
|
* of this software and associated documentation files (the "Software"), to deal
|
|||
|
* in the Software without restriction, including without limitation the rights
|
|||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|||
|
* copies of the Software, and to permit persons to whom the Software is
|
|||
|
* furnished to do so, subject to the following conditions:
|
|||
|
*
|
|||
|
* The above copyright notice and this permission notice shall be included in
|
|||
|
* all copies or substantial portions of the Software.
|
|||
|
*
|
|||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|||
|
* THE SOFTWARE.
|
|||
|
*
|
|||
|
* Originally forked from (with the author's permission):
|
|||
|
* Fabrice Bellard's javascript vt100 for jslinux:
|
|||
|
* http://bellard.org/jslinux/
|
|||
|
* Copyright (c) 2011 Fabrice Bellard
|
|||
|
* The original design remains. The terminal itself
|
|||
|
* has been extended to include xterm CSI codes, among
|
|||
|
* other features.
|
|||
|
*/(function(){"use strict";function n(){this._events=this._events||{}}function r(){n.call(this)}function h(e){var n=this;if(!(this instanceof h))return new h(arguments[0],arguments[1],arguments[2]);r.call(this),typeof e=="number"&&(e={cols:arguments[0],rows:arguments[1],handler:arguments[2]}),e=e||{},T(N(h.defaults),function(t){e[t]==null&&(e[t]=h.options[t],h[t]!==h.defaults[t]&&(e[t]=h[t])),n[t]=e[t]}),e.colors.length===8?e.colors=e.colors.concat(h._colors.slice(8)):e.colors.length===16?e.colors=e.colors.concat(h._colors.slice(16)):e.colors.length===10?e.colors=e.colors.slice(0,-2).concat(h._colors.slice(8,-2),e.colors.slice(-2)):e.colors.length===18&&(e.colors=e.colors.slice(0,-2).concat(h._colors.slice(16,-2),e.colors.slice(-2))),this.colors=e.colors,this.options=e,this.parent=e.body||e.parent||(t?t.getElementsByTagName("body")[0]:null),this.cols=e.cols||e.geometry[0],this.rows=e.rows||e.geometry[1],this.setRawMode,this.isTTY=!0,this.isRaw=!0,this.columns=this.cols,this.rows=this.rows,e.handler&&this.on("data",e.handler),this.ybase=0,this.ydisp=0,this.x=0,this.y=0,this.cursorState=0,this.cursorHidden=!1,this.convertEol,this.state=0,this.queue="",this.scrollTop=0,this.scrollBottom=this.rows-1,this.applicationKeypad=!1,this.applicationCursor=!1,this.originMode=!1,this.insertMode=!1,this.wraparoundMode=!1,this.normal=null,this.prefixMode=!1,this.selectMode=!1,this.visualMode=!1,this.searchMode=!1,this.searchDown,this.entry="",this.entryPrefix="Search: ",this._real,this._selected,this._textarea,this.charset=null,this.gcharset=null,this.glevel=0,this.charsets=[null],this.decLocator,this.x10Mouse,this.vt200Mouse,this.vt300Mouse,this.normalMouse,this.mouseEvents,this.sendFocus,this.utfMouse,this.sgrMouse,this.urxvtMouse,this.element,this.children,this.refreshStart,this.refreshEnd,this.savedX,this.savedY,this.savedCols,this.readable=!0,this.writable=!0,this.defAttr=131840,this.curAttr=this.defAttr,this.params=[],this.currentParam=0,this.prefix="",this.postfix="",this.lines=[];var i=this.rows;while(i--)this.lines.push(this.blankLine());this.tabs,this.setupStops()}function p(e,t,n,r){e.addEventListener(t,n,r||!1)}function d(e,t,n,r){e.removeEventListener(t,n,r||!1)}function v(e){return e.preventDefault&&e.preventDefault(),e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble=!0,!1}function m(e,t){function n(){this.constructor=e}n.prototype=t.prototype,e.prototype=new n}function g(e){var t=e.getElementsByTagName("body")[0],n=e.createElement("div");n.className="terminal";var r=e.createElement("div"),i=e.createElement("span");i.innerHTML="hello world",r.appendChild(i),n.appendChild(r),t.appendChild(n);var s=i.scrollWidth;i.style.fontWeight="bold";var o=i.scrollWidth;return t.removeChild(n),s!==o}function E(e,t){var n=e.length;while(n--)if(e[n]===t)return n;return-1}function S(e){return e<=""?!1:e>="!"&&e<="ᄒ"||e>="ᅡ"&&e<="ᅦ"||e>="ᅧ"&&e<="ᅬ"||e>="ᅭ"&&e<="ᅲ"||e>="ᅳ"&&e<="ᅵ"||e>="¢"&&e<="₩"||e>="│"&&e<="○"}function x(e,t,n){var r=e<<16|t<<8|n;if(x._cache[r]!=null)return x._cache[r];var i=Infinity,s=-1,o=0,u,a,f,l,c;for(;o<h.vcolors.length;o++){u=h.vcolors[o],a=u[0],f=u[1],l=u[2],c=x.distance(e,t,n,a,f,l);if(c===0){s=o;break}c<i&&(i=c,s=o)}return x._cache[r]=s}function T(e,t,n){if(e.forEach)return e.forEach(t,n);for(var r=0;r<e.length;r++)t.call(n,e[r],r,e)}function N(e){if(Object.keys)return Object.keys(e);var t,n=[];for(t in e)Object.prototype.hasOwnProperty.call(e,t)&&n.push(t);return n}var e=this,t=this.document;n.prototype.addListener=function(e,t){this._events[e]=this._events[e]||[],this._events[e].push(t)},n.prototype.on=n.prototype.addListener,n.prototype.removeListener=function(e,t){if(!this._events[e])return;var n=this._events[e],r=n.length;while(r--)if(n[r]===t||n[r].listener===t){n.splice(r,1);return}},n.prototype.off=n.prototype.removeListener,n.prototype.removeAllListeners=function(e){this._events[e]&&delete this._events[e]},n.prototype.once=function(e,t){function n(){var r=Array.prototype.slice.call(arguments);return this.removeListener(e,n),t.apply(this,r)}return n.lis
|
|||
|
n=e[1]-1:n=0,t<0?t=0:t>=this.rows&&(t=this.rows-1),n<0?n=0:n>=this.cols&&(n=this.cols-1),this.x=n,this.y=t},h.prototype.eraseInDisplay=function(e){var t;switch(e[0]){case 0:this.eraseRight(this.x,this.y),t=this.y+1;for(;t<this.rows;t++)this.eraseLine(t);break;case 1:this.eraseLeft(this.x,this.y),t=this.y;while(t--)this.eraseLine(t);break;case 2:t=this.rows;while(t--)this.eraseLine(t);break;case 3:}},h.prototype.eraseInLine=function(e){switch(e[0]){case 0:this.eraseRight(this.x,this.y);break;case 1:this.eraseLeft(this.x,this.y);break;case 2:this.eraseLine(this.y)}},h.prototype.charAttributes=function(e){if(e.length===1&&e[0]===0){this.curAttr=this.defAttr;return}var t=e.length,n=0,r=this.curAttr>>18,i=this.curAttr>>9&511,s=this.curAttr&511,o;for(;n<t;n++)o=e[n],o>=30&&o<=37?i=o-30:o>=40&&o<=47?s=o-40:o>=90&&o<=97?(o+=8,i=o-90):o>=100&&o<=107?(o+=8,s=o-100):o===0?(r=this.defAttr>>18,i=this.defAttr>>9&511,s=this.defAttr&511):o===1?r|=1:o===4?r|=2:o===5?r|=4:o===7?r|=8:o===8?r|=16:o===22?r&=-2:o===24?r&=-3:o===25?r&=-5:o===27?r&=-9:o===28?r&=-17:o===39?i=this.defAttr>>9&511:o===49?s=this.defAttr&511:o===38?e[n+1]===2?(n+=2,i=x(e[n]&255,e[n+1]&255,e[n+2]&255),i===-1&&(i=511),n+=2):e[n+1]===5&&(n+=2,o=e[n]&255,i=o):o===48?e[n+1]===2?(n+=2,s=x(e[n]&255,e[n+1]&255,e[n+2]&255),s===-1&&(s=511),n+=2):e[n+1]===5&&(n+=2,o=e[n]&255,s=o):o===100?(i=this.defAttr>>9&511,s=this.defAttr&511):this.error("Unknown SGR attribute: %d.",o);this.curAttr=r<<18|i<<9|s},h.prototype.deviceStatus=function(e){if(!this.prefix)switch(e[0]){case 5:this.send("[0n");break;case 6:this.send("["+(this.y+1)+";"+(this.x+1)+"R")}else if(this.prefix==="?")switch(e[0]){case 6:this.send("[?"+(this.y+1)+";"+(this.x+1)+"R");break;case 15:break;case 25:break;case 26:break;case 53:}},h.prototype.insertChars=function(e){var t,n,r,i;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.x,i=[this.eraseAttr()," "];while(t--&&r<this.cols)this.lines[n].splice(r++,0,i),this.lines[n].pop()},h.prototype.cursorNextLine=function(e){var t=e[0];t<1&&(t=1),this.y+=t,this.y>=this.rows&&(this.y=this.rows-1),this.x=0},h.prototype.cursorPrecedingLine=function(e){var t=e[0];t<1&&(t=1),this.y-=t,this.y<0&&(this.y=0),this.x=0},h.prototype.cursorCharAbsolute=function(e){var t=e[0];t<1&&(t=1),this.x=t-1},h.prototype.insertLines=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.rows-1-this.scrollBottom,r=this.rows-1+this.ybase-r+1;while(t--)this.lines.splice(n,0,this.blankLine(!0)),this.lines.splice(r,1);this.updateRange(this.y),this.updateRange(this.scrollBottom)},h.prototype.deleteLines=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.rows-1-this.scrollBottom,r=this.rows-1+this.ybase-r;while(t--)this.lines.splice(r+1,0,this.blankLine(!0)),this.lines.splice(n,1);this.updateRange(this.y),this.updateRange(this.scrollBottom)},h.prototype.deleteChars=function(e){var t,n,r;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=[this.eraseAttr()," "];while(t--)this.lines[n].splice(this.x,1),this.lines[n].push(r)},h.prototype.eraseChars=function(e){var t,n,r,i;t=e[0],t<1&&(t=1),n=this.y+this.ybase,r=this.x,i=[this.eraseAttr()," "];while(t--&&r<this.cols)this.lines[n][r++]=i},h.prototype.charPosAbsolute=function(e){var t=e[0];t<1&&(t=1),this.x=t-1,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.HPositionRelative=function(e){var t=e[0];t<1&&(t=1),this.x+=t,this.x>=this.cols&&(this.x=this.cols-1)},h.prototype.sendDeviceAttributes=function(e){if(e[0]>0)return;this.prefix?this.prefix===">"&&(this.is("xterm")?this.send("[>0;276;0c"):this.is("rxvt-unicode")?this.send("[>85;95;0c"):this.is("linux")?this.send(e[0]+"c"):this.is("screen")&&this.send("[>83;40003;0c")):this.is("xterm")||this.is("rxvt-unicode")||this.is("screen")?this.send("[?1;2c"):this.is("linux")&&this.send("[?6c")},h.prototype.linePosAbsolute=function(e){var t=e[0];t<1&&(t=1),this.y=t-1,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.VPositionRelative=function(e){var t=e[0];t<1&&(t=1),this.y+=t,this.y>=this.rows&&(this.y=this.rows-1)},h.prototype.HVPosition=function(e){e[0]<1&&(e[0]=1),e[1]<1&&(e[1]=1),this.y=e[0
|