Files
2026-03-24 12:40:25 +01:00

49 lines
84 KiB
HTML

<!doctype html>
<html>
<head>
<title>MicroPythonOS WebREPL</title>
<style>:root{color-scheme:light;--bg:#ffffff;--panel:#f6f7f9;--panel-border:#e1e5ea;--text:#1c1f24;--muted:#5c6572;--accent:#f0a010;--accent-hover:#e08f00;--accent-active:#cc7f00;--shadow:0 10px 30px rgba(19,27,43,0.08);--radius:12px;--focus-ring:0 0 0 3px rgba(240,160,16,0.3);}*{box-sizing:border-box;}html{background:var(--bg);}body{margin:0;padding:24px 28px 32px;font:16px/1.6 "Inter","Segoe UI","Helvetica Neue",Arial,sans-serif;color:var(--text);background:var(--bg);}h1{margin:0 0 20px;font:600 24px/1.3 "Inter","Segoe UI","Helvetica Neue",Arial,sans-serif;color:var(--text);}form{display:flex;flex-wrap:wrap;gap:12px;align-items:center;margin-bottom:16px;}#term-wrapper{height:420px;}#term{height:100%;border-radius:var(--radius);border:1px solid var(--panel-border);background:#0f141b;color:#f1f5f9;padding:8px;box-shadow:var(--shadow);overflow:hidden;}#file-boxes{margin-left:20px;}.file-box{margin:0 0 16px;padding:14px 16px 16px;background:var(--panel);border:1px solid var(--panel-border);border-radius:var(--radius);box-shadow:var(--shadow);display:grid;gap:10px;}.file-box strong{font-size:15px;}#file-status{color:var(--muted);font-size:14px;}input[type="text"],input[type="file"]{width:100%;padding:10px 12px;border-radius:10px;border:1px solid var(--panel-border);background:#ffffff;color:var(--text);font-size:14px;}input[type="text"]:focus,input[type="file"]:focus{outline:none;border-color:var(--accent);box-shadow:var(--focus-ring);}input[type="submit"],input[type="button"]{padding:10px 16px;border-radius:999px;border:none;background:var(--accent);color:#1a1200;font-weight:600;font-size:14px;cursor:pointer;transition:transform 0.08s ease,background 0.15s ease,box-shadow 0.15s ease;box-shadow:0 8px 18px rgba(240,160,16,0.35);}input[type="submit"]:hover,input[type="button"]:hover{background:var(--accent-hover);transform:translateY(-1px);}input[type="submit"]:active,input[type="button"]:active{background:var(--accent-active);transform:translateY(0);}input[type="submit"]:focus,input[type="button"]:focus{outline:none;box-shadow:var(--focus-ring);}i{color:var(--muted);font-size:13px;}</style>
<script>;(function(){'use strict';var window = this,document = this.document;function EventEmitter(){this._events = this._events ||{};}EventEmitter.prototype.addListener = function(type,listener){this._events[type]= this._events[type]||[];this._events[type].push(listener);};EventEmitter.prototype.on = EventEmitter.prototype.addListener;EventEmitter.prototype.removeListener = function(type,listener){if(!this._events[type])return;var obj = this._events[type],i = obj.length;while(i--){if(obj[i]=== listener || obj[i].listener === listener){obj.splice(i,1);return;}}};EventEmitter.prototype.off = EventEmitter.prototype.removeListener;EventEmitter.prototype.removeAllListeners = function(type){if(this._events[type])delete this._events[type];};EventEmitter.prototype.once = function(type,listener){function on(){var args = Array.prototype.slice.call(arguments);this.removeListener(type,on);return listener.apply(this,args);}on.listener = listener;return this.on(type,on);};EventEmitter.prototype.emit = function(type){if(!this._events[type])return;var args = Array.prototype.slice.call(arguments,1),obj = this._events[type],l = obj.length,i = 0;for(;i < l;i++){obj[i].apply(this,args);}};EventEmitter.prototype.listeners = function(type){return this._events[type]= this._events[type]||[];};function Stream(){EventEmitter.call(this);}inherits(Stream,EventEmitter);Stream.prototype.pipe = function(dest,options){var src = this,ondata,onerror,onend;function unbind(){src.removeListener('data',ondata);src.removeListener('error',onerror);src.removeListener('end',onend);dest.removeListener('error',onerror);dest.removeListener('close',unbind);}src.on('data',ondata = function(data){dest.write(data);});src.on('error',onerror = function(err){unbind();if(!this.listeners('error').length){throw err;}});src.on('end',onend = function(){dest.end();unbind();});dest.on('error',onerror);dest.on('close',unbind);dest.emit('pipe',src);return dest;};var normal = 0,escaped = 1,csi = 2,osc = 3,charset = 4,dcs = 5,ignore = 6,UDK ={type: 'udk'};function Terminal(options){var self = this;if(!(this instanceof Terminal)){return new Terminal(arguments[0],arguments[1],arguments[2]);}Stream.call(this);if(typeof options === 'number'){options ={cols: arguments[0],rows: arguments[1],handler: arguments[2]};}options = options ||{};each(keys(Terminal.defaults),function(key){if(options[key]== null){options[key]= Terminal.options[key]; if(Terminal[key]!== Terminal.defaults[key]){options[key]= Terminal[key];}}self[key]= options[key];});if(options.colors.length === 8){options.colors = options.colors.concat(Terminal._colors.slice(8));}else if(options.colors.length === 16){options.colors = options.colors.concat(Terminal._colors.slice(16));}else if(options.colors.length === 10){options.colors = options.colors.slice(0,-2).concat(Terminal._colors.slice(8,-2),options.colors.slice(-2));}else if(options.colors.length === 18){options.colors = options.colors.slice(0,-2).concat(Terminal._colors.slice(16,-2),options.colors.slice(-2));}this.colors = options.colors;this.options = options; this.parent = options.body || options.parent ||(document ? document.getElementsByTagName('body')[0]: null);this.cols = options.cols || options.geometry[0];this.rows = options.rows || options.geometry[1]; this.setRawMode;this.isTTY = true;this.isRaw = true;this.columns = this.cols;this.rows = this.rows;if(options.handler){this.on('data',options.handler);}this.ybase = 0;this.ydisp = 0;this.x = 0;this.y = 0;this.cursorState = 0;this.cursorHidden = false;this.convertEol;this.state = 0;this.queue = '';this.scrollTop = 0;this.scrollBottom = this.rows - 1; this.applicationKeypad = false;this.applicationCursor = false;this.originMode = false;this.insertMode = false;this.wraparoundMode = false;this.normal = null; this.prefixMode = false;this.selectMode = false;this.visualMode = false;this.searchMode = false;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 = true;this.writable = true;this.defAttr =(0 << 18)|(257 << 9)|(256 << 0);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();}inherits(Terminal,Stream); Terminal.tangoColors =[ '#2e3436','#cc0000','#4e9a06','#c4a000','#3465a4','#75507b','#06989a','#d3d7cf', '#555753','#ef2929','#8ae234','#fce94f','#729fcf','#ad7fa8','#34e2e2','#eeeeec'];Terminal.xtermColors =[ '#000000', '#cd0000', '#00cd00', '#cdcd00', '#0000ee', '#cd00cd', '#00cdcd', '#e5e5e5', '#7f7f7f', '#ff0000', '#00ff00', '#ffff00', '#5c5cff', '#ff00ff', '#00ffff', '#ffffff']; Terminal.colors =(function(){var colors = Terminal.tangoColors.slice(),r =[0x00,0x5f,0x87,0xaf,0xd7,0xff],i; i = 0;for(;i < 216;i++){out(r[(i / 36)% 6 | 0],r[(i / 6)% 6 | 0],r[i % 6]);} i = 0;for(;i < 24;i++){r = 8 + i * 10;out(r,r,r);}function out(r,g,b){colors.push('#' + hex(r)+ hex(g)+ hex(b));}function hex(c){c = c.toString(16);return c.length < 2 ? '0' + c : c;}return colors;})(); Terminal.colors[256]= '#000000';Terminal.colors[257]= '#f0f0f0';Terminal._colors = Terminal.colors.slice();Terminal.vcolors =(function(){var out =[],colors = Terminal.colors,i = 0,color;for(;i < 256;i++){color = parseInt(colors[i].substring(1),16);out.push([(color >> 16)& 0xff,(color >> 8)& 0xff,color & 0xff]);}return out;})();Terminal.defaults ={colors: Terminal.colors,convertEol: false,termName: 'xterm',geometry:[80,24],cursorBlink: true,visualBell: false,popOnBell: false,scrollback: 1000,screenKeys: false,debug: false,useStyle: false};Terminal.options ={};each(keys(Terminal.defaults),function(key){Terminal[key]= Terminal.defaults[key];Terminal.options[key]= Terminal.defaults[key];});Terminal.focus = null;Terminal.prototype.focus = function(){if(Terminal.focus === this)return;if(Terminal.focus){Terminal.focus.blur();}if(this.sendFocus)this.send('\x1b[I');this.showCursor(); Terminal.focus = this;};Terminal.prototype.blur = function(){if(Terminal.focus !== this)return;this.cursorState = 0;this.refresh(this.y,this.y);if(this.sendFocus)this.send('\x1b[O'); Terminal.focus = null;};Terminal.prototype.initGlobal = function(){var document = this.document;Terminal._boundDocs = Terminal._boundDocs ||[];if(~indexOf(Terminal._boundDocs,document)){return;}Terminal._boundDocs.push(document);Terminal.bindPaste(document);Terminal.bindKeys(document);Terminal.bindCopy(document);if(this.isMobile){this.fixMobile(document);}if(this.useStyle){Terminal.insertStyle(document,this.colors[256],this.colors[257]);}};Terminal.bindPaste = function(document){ var window = document.defaultView;on(window,'paste',function(ev){var term = Terminal.focus;if(!term)return;if(ev.clipboardData){term.send(ev.clipboardData.getData('text/plain'));}else if(term.context.clipboardData){term.send(term.context.clipboardData.getData('Text'));} term.element.contentEditable = 'inherit';return cancel(ev);});};Terminal.bindKeys = function(document){ on(document,'keydown',function(ev){if(!Terminal.focus)return;var target = ev.target || ev.srcElement;if(!target)return;if(target === Terminal.focus.element || target === Terminal.focus.context || target === Terminal.focus.document || target === Terminal.focus.body || target === Terminal._textarea || target === Terminal.focus.parent){return Terminal.focus.keyDown(ev);}},true);on(document,'keypress',function(ev){if(!Terminal.focus)return;var target = ev.target || ev.srcElement;if(!target)return;if(ev.ctrlKey && ev.key === 'v'){ return;}if(target === Terminal.focus.element || target === Terminal.focus.context || target === Terminal.focus.document || target === Terminal.focus.body || target === Terminal._textarea || target === Terminal.focus.parent){ Terminal.focus.element.contentEditable = 'inherit';return Terminal.focus.keyPress(ev);}},true); on(document,'mousedown',function(ev){if(!Terminal.focus)return;var el = ev.target || ev.srcElement;if(!el)return;do{if(el === Terminal.focus.element)return;}while(el = el.parentNode);Terminal.focus.blur();});};Terminal.bindCopy = function(document){var window = document.defaultView; on(window,'copy',function(ev){var term = Terminal.focus;if(!term)return;if(!term._selected)return;var textarea = term.getCopyTextarea();var text = term.grabText(term._selected.x1,term._selected.x2,term._selected.y1,term._selected.y2);term.emit('copy',text);textarea.focus();textarea.textContent = text;textarea.value = text;textarea.setSelectionRange(0,text.length);setTimeout(function(){term.element.focus();term.focus();},1);});};Terminal.prototype.fixMobile = function(document){var self = this;var textarea = document.createElement('textarea');textarea.style.position = 'absolute';textarea.style.left = '-32000px';textarea.style.top = '-32000px';textarea.style.width = '0px';textarea.style.height = '0px';textarea.style.opacity = '0';textarea.style.backgroundColor = 'transparent';textarea.style.borderStyle = 'none';textarea.style.outlineStyle = 'none';textarea.autocapitalize = 'none';textarea.autocorrect = 'off';document.getElementsByTagName('body')[0].appendChild(textarea);Terminal._textarea = textarea;setTimeout(function(){textarea.focus();},1000);if(this.isAndroid){on(textarea,'change',function(){var value = textarea.textContent || textarea.value;textarea.value = '';textarea.textContent = '';self.send(value + '\r');});}};Terminal.insertStyle = function(document,bg,fg){var style = document.getElementById('term-style');if(style)return;var head = document.getElementsByTagName('head')[0];if(!head)return;var style = document.createElement('style');style.id = 'term-style'; style.innerHTML = '' + '.terminal {\n' + ' float: left;\n' + ' border: ' + bg + ' solid 5px;\n' + ' font-family: "DejaVu Sans Mono", "Liberation Mono", monospace;\n' + ' font-size: 11px;\n' + ' color: ' + fg + ';\n' + ' background: ' + bg + ';\n' + '}\n' + '\n' + '.terminal-cursor {\n' + ' color: ' + bg + ';\n' + ' background: ' + fg + ';\n' + '}\n'; head.insertBefore(style,head.firstChild);};Terminal.prototype.open = function(parent){var self = this,i = 0,div;this.parent = parent || this.parent;if(!this.parent){throw new Error('Terminal requires a parent element.');} this.context = this.parent.ownerDocument.defaultView;this.document = this.parent.ownerDocument;this.body = this.document.getElementsByTagName('body')[0]; if(this.context.navigator && this.context.navigator.userAgent){this.isMac = !!~this.context.navigator.userAgent.indexOf('Mac');this.isIpad = !!~this.context.navigator.userAgent.indexOf('iPad');this.isIphone = !!~this.context.navigator.userAgent.indexOf('iPhone');this.isAndroid = !!~this.context.navigator.userAgent.indexOf('Android');this.isMobile = this.isIpad || this.isIphone || this.isAndroid;this.isMSIE = !!~this.context.navigator.userAgent.indexOf('MSIE');this.isFirefox = !!~this.context.navigator.userAgent.indexOf('Firefox');} this.element = this.document.createElement('div');this.element.className = 'terminal';this.element.style.outline = 'none';this.element.setAttribute('tabindex',0);this.element.setAttribute('spellcheck','false');this.element.style.backgroundColor = this.colors[256];this.element.style.color = this.colors[257]; this.children =[];for(;i < this.rows;i++){div = this.document.createElement('div');this.element.appendChild(div);this.children.push(div);}this.parent.appendChild(this.element); this.refresh(0,this.rows - 1);if(!('useEvents' in this.options)|| this.options.useEvents){ this.initGlobal();}if(!('useFocus' in this.options)|| this.options.useFocus){ this.focus(); this.startBlink(); on(this.element,'focus',function(){self.focus();if(self.isMobile){Terminal._textarea.focus();}}); on(this.element,'mousedown',function(){self.focus();}); on(this.element,'mousedown',function(ev){var button = ev.button != null ? +ev.button : ev.which != null ? ev.which - 1 : null; if(self.isMSIE){button = button === 1 ? 0 : button === 4 ? 1 : button;} if(button !== 1 && button !== 2){ self.element.contentEditable = 'inherit';return;} self.element.contentEditable = 'true';if(self.isFirefox){ window.getSelection().collapse(self.element,0);} if(!self.isFirefox){setTimeout(function(){self.element.contentEditable = 'inherit';},1);}},true);}if(!('useMouse' in this.options)|| this.options.useMouse){ this.bindMouse();} if(!('useFocus' in this.options)|| this.options.useFocus){ setTimeout(function(){self.element.focus();},100);} if(Terminal.brokenBold == null){Terminal.brokenBold = isBoldBroken(this.document);}this.emit('open');};Terminal.prototype.setRawMode = function(value){this.isRaw = !!value;}; Terminal.prototype.bindMouse = function(){var el = this.element,self = this,pressed = 32;var wheelEvent = 'onmousewheel' in this.context ? 'mousewheel' : 'DOMMouseScroll'; function sendButton(ev){var button,pos; button = getButton(ev); pos = getCoords(ev);if(!pos)return;sendEvent(button,pos);switch(ev.type){case 'mousedown': pressed = button;break;case 'mouseup': pressed = 32;break;case wheelEvent: break;}} function sendMove(ev){var button = pressed,pos;pos = getCoords(ev);if(!pos)return; button += 32;sendEvent(button,pos);} function encode(data,ch){if(!self.utfMouse){if(ch === 255)return data.push(0);if(ch > 127)ch = 127;data.push(ch);}else{if(ch === 2047)return data.push(0);if(ch < 127){data.push(ch);}else{if(ch > 2047)ch = 2047;data.push(0xC0 |(ch >> 6));data.push(0x80 |(ch & 0x3F));}}} function sendEvent(button,pos){ if(self.vt300Mouse){ button &= 3;pos.x -= 32;pos.y -= 32;var data = '\x1b[24';if(button === 0)data += '1';else if(button === 1)data += '3';else if(button === 2)data += '5';else if(button === 3)return;else data += '0';data += '~[' + pos.x + ',' + pos.y + ']\r';self.send(data);return;}if(self.decLocator){ button &= 3;pos.x -= 32;pos.y -= 32;if(button === 0)button = 2;else if(button === 1)button = 4;else if(button === 2)button = 6;else if(button === 3)button = 3;self.send('\x1b[' + button + ';' +(button === 3 ? 4 : 0)+ ';' + pos.y + ';' + pos.x + ';' +(pos.page || 0)+ '&w');return;}if(self.urxvtMouse){pos.x -= 32;pos.y -= 32;pos.x++;pos.y++;self.send('\x1b[' + button + ';' + pos.x + ';' + pos.y + 'M');return;}if(self.sgrMouse){pos.x -= 32;pos.y -= 32;self.send('\x1b[<' +((button & 3)=== 3 ? button & ~3 : button)+ ';' + pos.x + ';' + pos.y +((button & 3)=== 3 ? 'm' : 'M'));return;}var data =[];encode(data,button);encode(data,pos.x);encode(data,pos.y);self.send('\x1b[M' + String.fromCharCode.apply(String,data));}function getButton(ev){var button,shift,meta,ctrl,mod; switch(ev.type){case 'mousedown': button = ev.button != null ? +ev.button : ev.which != null ? ev.which - 1 : null;if(self.isMSIE){button = button === 1 ? 0 : button === 4 ? 1 : button;}break;case 'mouseup': button = 3;break;case 'DOMMouseScroll': button = ev.detail < 0 ? 64 : 65;break;case 'mousewheel': button = ev.wheelDeltaY > 0 ? 64 : 65;break;} shift = ev.shiftKey ? 4 : 0;meta = ev.metaKey ? 8 : 0;ctrl = ev.ctrlKey ? 16 : 0;mod = shift | meta | ctrl; if(self.vt200Mouse){ mod &= ctrl;}else if(!self.normalMouse){mod = 0;} button =(32 +(mod << 2))+ button;return button;} function getCoords(ev){var x,y,w,h,el; if(ev.pageX == null)return;x = ev.pageX;y = ev.pageY;el = self.element; while(el && el !== self.document.documentElement){x -= el.offsetLeft;y -= el.offsetTop;el = 'offsetParent' in el ? el.offsetParent : el.parentNode;} w = self.element.clientWidth;h = self.element.clientHeight;x = Math.round((x / w)* self.cols);y = Math.round((y / h)* self.rows); if(x < 0)x = 0;if(x > self.cols)x = self.cols;if(y < 0)y = 0;if(y > self.rows)y = self.rows; x += 32;y += 32;return{x: x,y: y,type: ev.type === wheelEvent ? 'mousewheel' : ev.type};}on(el,'mousedown',function(ev){if(!self.mouseEvents)return; sendButton(ev); self.focus(); if(self.normalMouse)on(self.document,'mousemove',sendMove); if(!self.x10Mouse){on(self.document,'mouseup',function up(ev){sendButton(ev);if(self.normalMouse)off(self.document,'mousemove',sendMove);off(self.document,'mouseup',up);return cancel(ev);});}return cancel(ev);}); on(el,wheelEvent,function(ev){if(!self.mouseEvents)return;if(self.x10Mouse || self.vt300Mouse || self.decLocator)return;sendButton(ev);return cancel(ev);}); on(el,wheelEvent,function(ev){if(self.mouseEvents)return;if(self.applicationKeypad)return;if(ev.type === 'DOMMouseScroll'){self.scrollDisp(ev.detail < 0 ? -5 : 5);}else{self.scrollDisp(ev.wheelDeltaY > 0 ? -5 : 5);}return cancel(ev);});};Terminal.prototype.close = Terminal.prototype.destroySoon = Terminal.prototype.destroy = function(){if(this.destroyed){return;}if(this._blink){clearInterval(this._blink);delete this._blink;}this.readable = false;this.writable = false;this.destroyed = true;this._events ={};this.handler = function(){};this.write = function(){};this.end = function(){};if(this.element.parentNode){this.element.parentNode.removeChild(this.element);}this.emit('end');this.emit('close');this.emit('finish');this.emit('destroy');}; Terminal.prototype.refresh = function(start,end){var x,y,i,line,out,ch,width,data,attr,bg,fg,flags,row,parent;if(end - start >= this.rows / 2){parent = this.element.parentNode;if(parent)parent.removeChild(this.element);}width = this.cols;y = start;if(end >= this.lines.length){this.log('`end` is too large. Most likely a bad CSR.');end = this.lines.length - 1;}for(;y <= end;y++){row = y + this.ydisp;line = this.lines[row];out = '';if(y === this.y && this.cursorState &&(this.ydisp === this.ybase || this.selectMode)&& !this.cursorHidden){x = this.x;}else{x = -1;}attr = this.defAttr;i = 0;for(;i < width;i++){data = line[i][0];ch = line[i][1];if(i === x)data = -1;if(data !== attr){if(attr !== this.defAttr){out += '</span>';}if(data !== this.defAttr){if(data === -1){out += '<span class="reverse-video terminal-cursor">';}else{out += '<span style="';bg = data & 0x1ff;fg =(data >> 9)& 0x1ff;flags = data >> 18; if(flags & 1){if(!Terminal.brokenBold){out += 'font-weight:bold;';} if(fg < 8)fg += 8;} if(flags & 2){out += 'text-decoration:underline;';} if(flags & 4){if(flags & 2){out = out.slice(0,-1);out += ' blink;';}else{out += 'text-decoration:blink;';}} if(flags & 8){bg =(data >> 9)& 0x1ff;fg = data & 0x1ff; if((flags & 1)&& fg < 8)fg += 8;} if(flags & 16){out += 'visibility:hidden;';} if(bg !== 256){out += 'background-color:' + this.colors[bg]+ ';';}if(fg !== 257){out += 'color:' + this.colors[fg]+ ';';}out += '">';}}}switch(ch){case '&': out += '&amp;';break;case '<': out += '&lt;';break;case '>': out += '&gt;';break;default: if(ch <= ' '){out += '&nbsp;';}else{if(isWide(ch))i++;out += ch;}break;}attr = data;}if(attr !== this.defAttr){out += '</span>';}this.children[y].innerHTML = out;}if(parent)parent.appendChild(this.element);};Terminal.prototype._cursorBlink = function(){if(Terminal.focus !== this)return;this.cursorState ^= 1;this.refresh(this.y,this.y);};Terminal.prototype.showCursor = function(){if(!this.cursorState){this.cursorState = 1;this.refresh(this.y,this.y);}else{}};Terminal.prototype.startBlink = function(){if(!this.cursorBlink)return;var self = this;this._blinker = function(){self._cursorBlink();};this._blink = setInterval(this._blinker,500);};Terminal.prototype.refreshBlink = function(){if(!this.cursorBlink || !this._blink)return;clearInterval(this._blink);this._blink = setInterval(this._blinker,500);};Terminal.prototype.scroll = function(){var row;if(++this.ybase === this.scrollback){this.ybase = this.ybase / 2 | 0;this.lines = this.lines.slice(-(this.ybase + this.rows)+ 1);}this.ydisp = this.ybase; row = this.ybase + this.rows - 1; row -= this.rows - 1 - this.scrollBottom;if(row === this.lines.length){ this.lines.push(this.blankLine());}else{ this.lines.splice(row,0,this.blankLine());}if(this.scrollTop !== 0){if(this.ybase !== 0){this.ybase--;this.ydisp = this.ybase;}this.lines.splice(this.ybase + this.scrollTop,1);} this.updateRange(this.scrollTop);this.updateRange(this.scrollBottom);};Terminal.prototype.scrollDisp = function(disp){this.ydisp += disp;if(this.ydisp > this.ybase){this.ydisp = this.ybase;}else if(this.ydisp < 0){this.ydisp = 0;}this.refresh(0,this.rows - 1);};Terminal.prototype.write = function(data){var l = data.length,i = 0,j,cs,ch;this.refreshStart = this.y;this.refreshEnd = this.y;if(this.ybase !== this.ydisp){this.ydisp = this.ybase;this.maxRange();} for(;i < l;i++,this.lch = ch){ch = data[i];switch(this.state){case normal: switch(ch){ case '\x07': this.bell();break; case '\n': case '\x0b': case '\x0c': if(this.convertEol){this.x = 0;} this.y++;if(this.y > this.scrollBottom){this.y--;this.scroll();}break; case '\r': this.x = 0;break; case '\x08': if(this.x > 0){this.x--;}break; case '\t': this.x = this.nextStop();break; case '\x0e': this.setgLevel(1);break; case '\x0f': this.setgLevel(0);break; case '\x1b': this.state = escaped;break;default: if(ch >= ' '){if(this.charset && this.charset[ch]){ch = this.charset[ch];}if(this.x >= this.cols){this.x = 0;this.y++;if(this.y > this.scrollBottom){this.y--;this.scroll();}}this.lines[this.y + this.ybase][this.x]=[this.curAttr,ch];this.x++;this.updateRange(this.y);if(isWide(ch)){j = this.y + this.ybase;if(this.cols < 2 || this.x >= this.cols){this.lines[j][this.x - 1]=[this.curAttr,' '];break;}this.lines[j][this.x]=[this.curAttr,' '];this.x++;}}break;}break;case escaped: switch(ch){ case '[': this.params =[];this.currentParam = 0;this.state = csi;break; case ']': this.params =[];this.currentParam = 0;this.state = osc;break; case 'P': this.params =[];this.prefix = '';this.currentParam = '';this.state = dcs;break; case '_': this.state = ignore;break; case '^': this.state = ignore;break; case 'c': this.reset();break; case 'E': this.x = 0;;case 'D': this.index();break; case 'M': this.reverseIndex();break; case '%': this.setgLevel(0);this.setgCharset(0,Terminal.charsets.US);this.state = normal;i++;break; case '(': case ')': case '*': case '+': case '-': case '.': switch(ch){case '(': this.gcharset = 0;break;case ')': this.gcharset = 1;break;case '*': this.gcharset = 2;break;case '+': this.gcharset = 3;break;case '-': this.gcharset = 1;break;case '.': this.gcharset = 2;break;}this.state = charset;break; case '/': this.gcharset = 3;this.state = charset;i--;break; case 'N': break; case 'O': break; case 'n': this.setgLevel(2);break; case 'o': this.setgLevel(3);break; case '|': this.setgLevel(3);break; case '}': this.setgLevel(2);break; case '~': this.setgLevel(1);break; case '7': this.saveCursor();this.state = normal;break; case '8': this.restoreCursor();this.state = normal;break; case '#': this.state = normal;i++;break; case 'H': this.tabSet();break; case '=': this.log('Serial port requested application keypad.');this.applicationKeypad = true;this.state = normal;break; case '>': this.log('Switching back to normal keypad.');this.applicationKeypad = false;this.state = normal;break;default: this.state = normal;this.error('Unknown ESC control: %s.',ch);break;}break;case charset: switch(ch){case '0': cs = Terminal.charsets.SCLD;break;case 'A': cs = Terminal.charsets.UK;break;case 'B': cs = Terminal.charsets.US;break;case '4': cs = Terminal.charsets.Dutch;break;case 'C': case '5': cs = Terminal.charsets.Finnish;break;case 'R': cs = Terminal.charsets.French;break;case 'Q': cs = Terminal.charsets.FrenchCanadian;break;case 'K': cs = Terminal.charsets.German;break;case 'Y': cs = Terminal.charsets.Italian;break;case 'E': case '6': cs = Terminal.charsets.NorwegianDanish;break;case 'Z': cs = Terminal.charsets.Spanish;break;case 'H': case '7': cs = Terminal.charsets.Swedish;break;case '=': cs = Terminal.charsets.Swiss;break;case '/': cs = Terminal.charsets.ISOLatin;i++;break;default: cs = Terminal.charsets.US;break;}this.setgCharset(this.gcharset,cs);this.gcharset = null;this.state = normal;break;case osc: if((this.lch === '\x1b' && ch === '\\')|| ch === '\x07'){if(this.lch === '\x1b'){if(typeof this.currentParam === 'string'){this.currentParam = this.currentParam.slice(0,-1);}else if(typeof this.currentParam == 'number'){this.currentParam =(this.currentParam -('\x1b'.charCodeAt(0)- 48))/ 10;}}this.params.push(this.currentParam);switch(this.params[0]){case 0: case 1: case 2: if(this.params[1]){this.title = this.params[1];this.handleTitle(this.title);}break;case 3: break;case 4: case 5: break;case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: break;case 46: break;case 50: break;case 51: break;case 52: break;case 104: case 105: case 110: case 111: case 112: case 113: case 114: case 115: case 116: case 117: case 118: break;}this.params =[];this.currentParam = 0;this.state = normal;}else{if(!this.params.length){if(ch >= '0' && ch <= '9'){this.currentParam = this.currentParam * 10 + ch.charCodeAt(0)- 48;}else if(ch === ';'){this.params.push(this.currentParam);this.currentParam = '';}}else{this.currentParam += ch;}}break;case csi: if(ch === '?' || ch === '>' || ch === '!'){this.prefix = ch;break;} if(ch >= '0' && ch <= '9'){this.currentParam = this.currentParam * 10 + ch.charCodeAt(0)- 48;break;} if(ch === '$' || ch === '"' || ch === ' ' || ch === '\''){this.postfix = ch;break;}this.params.push(this.currentParam);this.currentParam = 0; if(ch === ';')break;this.state = normal;switch(ch){ case 'A': this.cursorUp(this.params);break; case 'B': this.cursorDown(this.params);break; case 'C': this.cursorForward(this.params);break; case 'D': this.cursorBackward(this.params);break; case 'H': this.cursorPos(this.params);break; case 'J': this.eraseInDisplay(this.params);break; case 'K': this.eraseInLine(this.params);break; case 'm': if(!this.prefix){this.charAttributes(this.params);}break; case 'n': if(!this.prefix){this.deviceStatus(this.params);}break; case '@': this.insertChars(this.params);break; case 'E': this.cursorNextLine(this.params);break; case 'F': this.cursorPrecedingLine(this.params);break; case 'G': this.cursorCharAbsolute(this.params);break; case 'L': this.insertLines(this.params);break; case 'M': this.deleteLines(this.params);break; case 'P': this.deleteChars(this.params);break; case 'X': this.eraseChars(this.params);break; case '`': this.charPosAbsolute(this.params);break; case 'a': this.HPositionRelative(this.params);break; case 'c': this.sendDeviceAttributes(this.params);break; case 'd': this.linePosAbsolute(this.params);break; case 'e': this.VPositionRelative(this.params);break; case 'f': this.HVPosition(this.params);break; case 'h': this.setMode(this.params);break; case 'l': this.resetMode(this.params);break; case 'r': this.setScrollRegion(this.params);break; case 's': this.saveCursor(this.params);break; case 'u': this.restoreCursor(this.params);break; case 'I': this.cursorForwardTab(this.params);break; case 'S': this.scrollUp(this.params);break; case 'T': if(this.params.length < 2 && !this.prefix){this.scrollDown(this.params);}break; case 'Z': this.cursorBackwardTab(this.params);break; case 'b': this.repeatPrecedingCharacter(this.params);break; case 'g': this.tabClear(this.params);break; case 'p': switch(this.prefix){ case '!': this.softReset(this.params);break;}break; default: this.error('Unknown CSI code: %s.',ch);break;}this.prefix = '';this.postfix = '';break;case dcs: if((this.lch === '\x1b' && ch === '\\')|| ch === '\x07'){ if(this.prefix === 'tmux;\x1b'){ if(ch === '\x07'){this.currentParam += ch;continue;}}if(this.lch === '\x1b'){if(typeof this.currentParam === 'string'){this.currentParam = this.currentParam.slice(0,-1);}else if(typeof this.currentParam == 'number'){this.currentParam =(this.currentParam -('\x1b'.charCodeAt(0)- 48))/ 10;}}this.params.push(this.currentParam);var pt = this.params[this.params.length - 1];switch(this.prefix){ case UDK: this.emit('udk',{clearAll: this.params[0]=== 0,eraseBelow: this.params[0]=== 1,lockKeys: this.params[1]=== 0,dontLockKeys: this.params[1]=== 1,keyList:(this.params[2]+ '').split(';').map(function(part){part = part.split('/');return{keyCode: part[0],hexKeyValue: part[1]};})});break; case '$q': var valid = 0;switch(pt){ case '"q': pt = '0"q';valid = 1;break; case '"p': pt = '61;0"p';valid = 1;break; case 'r': pt = '' +(this.scrollTop + 1)+ ';' +(this.scrollBottom + 1)+ 'r';valid = 1;break; case 'm': valid = 0; break;default: this.error('Unknown DCS Pt: %s.',pt);valid = 0; break;}this.send('\x1bP' + valid + '$r' + pt + '\x1b\\');break; case '+p': this.emit('set terminfo',{name: this.params[0]});break; case '+q': var valid = false;this.send('\x1bP' + +valid + '+r' + pt + '\x1b\\');break; case 'tmux;\x1b': this.emit('passthrough',pt);break;default: this.error('Unknown DCS prefix: %s.',pt);break;}this.currentParam = 0;this.prefix = '';this.state = normal;}else{this.currentParam += ch;if(!this.prefix){if(/^\d*;\d*\|/.test(this.currentParam)){this.prefix = UDK;this.params = this.currentParam.split(/[;|]/).map(function(n){if(!n.length)return 0;return +n;}).slice(0,-1);this.currentParam = '';}else if(/^[$+][a-zA-Z]/.test(this.currentParam)|| /^\w+;\x1b/.test(this.currentParam)){this.prefix = this.currentParam;this.currentParam = '';}}}break;case ignore: if((this.lch === '\x1b' && ch === '\\')|| ch === '\x07'){this.state = normal;}break;}}this.updateRange(this.y);this.refresh(this.refreshStart,this.refreshEnd);return true;};Terminal.prototype.writeln = function(data){return this.write(data + '\r\n');};Terminal.prototype.end = function(data){var ret = true;if(data){ret = this.write(data);}this.destroySoon();return ret;};Terminal.prototype.resume = function(){;};Terminal.prototype.pause = function(){;}; Terminal.prototype.keyDown = function(ev){var self = this,key;switch(ev.keyCode){ case 8: if(ev.altKey){key = '\x17';break;}if(ev.shiftKey){key = '\x08'; break;}key = '\x7f'; break; case 9: if(ev.shiftKey){key = '\x1b[Z';break;}key = '\t';break; case 13: key = '\r';break; case 27: key = '\x1b';break; case 32: key = '\x20';break; case 37: if(this.applicationCursor){key = '\x1bOD'; break;}if(ev.ctrlKey){key = '\x1b[5D';break;}key = '\x1b[D';break; case 39: if(this.applicationCursor){key = '\x1bOC';break;}if(ev.ctrlKey){key = '\x1b[5C';break;}key = '\x1b[C';break; case 38: if(this.applicationCursor){key = '\x1bOA';break;}if(ev.ctrlKey){this.scrollDisp(-1);return cancel(ev);}else{key = '\x1b[A';}break; case 40: if(this.applicationCursor){key = '\x1bOB';break;}if(ev.ctrlKey){this.scrollDisp(1);return cancel(ev);}else{key = '\x1b[B';}break; case 46: key = '\x1b[3~';break; case 45: key = '\x1b[2~';break; case 36: if(this.applicationKeypad){key = '\x1bOH';break;}key = '\x1bOH';break; case 35: if(this.applicationKeypad){key = '\x1bOF';break;}key = '\x1bOF';break; case 33: if(ev.shiftKey){this.scrollDisp(-(this.rows - 1));return cancel(ev);}else{key = '\x1b[5~';}break; case 34: if(ev.shiftKey){this.scrollDisp(this.rows - 1);return cancel(ev);}else{key = '\x1b[6~';}break; case 112: key = '\x1bOP';break; case 113: key = '\x1bOQ';break; case 114: key = '\x1bOR';break; case 115: key = '\x1bOS';break; case 116: key = '\x1b[15~';break; case 117: key = '\x1b[17~';break; case 118: key = '\x1b[18~';break; case 119: key = '\x1b[19~';break; case 120: key = '\x1b[20~';break; case 121: key = '\x1b[21~';break; case 122: key = '\x1b[23~';break; case 123: key = '\x1b[24~';break;default: if(ev.ctrlKey){if(ev.keyCode >= 65 && ev.keyCode <= 90){ if(this.screenKeys){if(!this.prefixMode && !this.selectMode && ev.keyCode === 65){this.enterPrefix();return cancel(ev);}} if(this.prefixMode && ev.keyCode === 86){this.leavePrefix(); var term = Terminal.focus;term.element.contentEditable = 'true';return;} if((this.prefixMode || this.selectMode)&& ev.keyCode === 67){if(this.visualMode){setTimeout(function(){self.leaveVisual();},1);}return;}key = String.fromCharCode(ev.keyCode - 64);}else if(ev.keyCode === 32){ key = String.fromCharCode(0);}else if(ev.keyCode >= 51 && ev.keyCode <= 55){ key = String.fromCharCode(ev.keyCode - 51 + 27);}else if(ev.keyCode === 56){ key = String.fromCharCode(127);}else if(ev.keyCode === 219){ key = String.fromCharCode(27);}else if(ev.keyCode === 221){ key = String.fromCharCode(29);}}else if(ev.altKey){if(ev.keyCode >= 65 && ev.keyCode <= 90){key = '\x1b' + String.fromCharCode(ev.keyCode + 32);}else if(ev.keyCode === 192){key = '\x1b`';}else if(ev.keyCode >= 48 && ev.keyCode <= 57){key = '\x1b' +(ev.keyCode - 48);}}break;}if(!key)return true;if(this.prefixMode){this.leavePrefix();return cancel(ev);}if(this.selectMode){this.keySelect(ev,key);return cancel(ev);}this.emit('keydown',ev);this.emit('key',key,ev);this.showCursor();this.handler(key);return cancel(ev);};Terminal.prototype.setgLevel = function(g){this.glevel = g;this.charset = this.charsets[g];};Terminal.prototype.setgCharset = function(g,charset){this.charsets[g]= charset;if(this.glevel === g){this.charset = charset;}};Terminal.prototype.keyPress = function(ev){var key;cancel(ev);if(ev.charCode){key = ev.charCode;}else if(ev.which == null){key = ev.keyCode;}else if(ev.which !== 0 && ev.charCode !== 0){key = ev.which;}else{return false;}if(!key || ev.ctrlKey || ev.altKey || ev.metaKey)return false;key = String.fromCharCode(key);if(this.prefixMode){this.leavePrefix();this.keyPrefix(ev,key);return false;}if(this.selectMode){this.keySelect(ev,key);return false;}this.emit('keypress',key,ev);this.emit('key',key,ev);this.showCursor();this.handler(key);return false;};Terminal.prototype.send = function(data){var self = this;if(!this.queue){setTimeout(function(){self.handler(self.queue);self.queue = '';},1);}this.queue += data;};Terminal.prototype.bell = function(){this.emit('bell');if(!this.visualBell)return;var self = this;this.element.style.borderColor = 'white';setTimeout(function(){self.element.style.borderColor = '';},10);if(this.popOnBell)this.focus();};Terminal.prototype.log = function(){if(!this.debug)return;if(!this.context.console || !this.context.console.log)return;var args = Array.prototype.slice.call(arguments);this.context.console.log.apply(this.context.console,args);};Terminal.prototype.error = function(){if(!this.debug)return;if(!this.context.console || !this.context.console.error)return;var args = Array.prototype.slice.call(arguments);this.context.console.error.apply(this.context.console,args);};Terminal.prototype.resize = function(x,y){var line,el,i,j,ch;if(x < 1)x = 1;if(y < 1)y = 1; j = this.cols;if(j < x){ch =[this.defAttr,' ']; i = this.lines.length;while(i--){while(this.lines[i].length < x){this.lines[i].push(ch);}}}else if(j > x){i = this.lines.length;while(i--){while(this.lines[i].length > x){this.lines[i].pop();}}}this.setupStops(j);this.cols = x;this.columns = x; j = this.rows;if(j < y){el = this.element;while(j++ < y){if(this.lines.length < y + this.ybase){this.lines.push(this.blankLine());}if(this.children.length < y){line = this.document.createElement('div');el.appendChild(line);this.children.push(line);}}}else if(j > y){while(j-- > y){if(this.lines.length > y + this.ybase){this.lines.pop();}if(this.children.length > y){el = this.children.pop();if(!el)continue;el.parentNode.removeChild(el);}}}this.rows = y; if(this.y >= y)this.y = y - 1;if(this.x >= x)this.x = x - 1;this.scrollTop = 0;this.scrollBottom = y - 1;this.refresh(0,this.rows - 1); this.normal = null; this.emit('resize');};Terminal.prototype.updateRange = function(y){if(y < this.refreshStart)this.refreshStart = y;if(y > this.refreshEnd)this.refreshEnd = y;};Terminal.prototype.maxRange = function(){this.refreshStart = 0;this.refreshEnd = this.rows - 1;};Terminal.prototype.setupStops = function(i){if(i != null){if(!this.tabs[i]){i = this.prevStop(i);}}else{this.tabs ={};i = 0;}for(;i < this.cols;i += 8){this.tabs[i]= true;}};Terminal.prototype.prevStop = function(x){if(x == null)x = this.x;while(!this.tabs[--x]&& x > 0);return x >= this.cols ? this.cols - 1 : x < 0 ? 0 : x;};Terminal.prototype.nextStop = function(x){if(x == null)x = this.x;while(!this.tabs[++x]&& x < this.cols);return x >= this.cols ? this.cols - 1 : x < 0 ? 0 : x;}; Terminal.prototype.eraseAttr = function(){ return(this.defAttr & ~0x1ff)|(this.curAttr & 0x1ff);};Terminal.prototype.eraseRight = function(x,y){var line = this.lines[this.ybase + y],ch =[this.eraseAttr(),' ']; for(;x < this.cols;x++){line[x]= ch;}this.updateRange(y);};Terminal.prototype.eraseLeft = function(x,y){var line = this.lines[this.ybase + y],ch =[this.eraseAttr(),' ']; x++;while(x--)line[x]= ch;this.updateRange(y);};Terminal.prototype.eraseLine = function(y){this.eraseRight(0,y);};Terminal.prototype.blankLine = function(cur){var attr = cur ? this.eraseAttr(): this.defAttr;var ch =[attr,' '],line =[],i = 0;for(;i < this.cols;i++){line[i]= ch;}return line;};Terminal.prototype.ch = function(cur){return cur ?[this.eraseAttr(),' ']:[this.defAttr,' '];};Terminal.prototype.is = function(term){var name = this.termName;return(name + '').indexOf(term)=== 0;};Terminal.prototype.handler = function(data){this.emit('data',data);};Terminal.prototype.handleTitle = function(title){this.emit('title',title);}; Terminal.prototype.index = function(){this.y++;if(this.y > this.scrollBottom){this.y--;this.scroll();}this.state = normal;}; Terminal.prototype.reverseIndex = function(){var j;this.y--;if(this.y < this.scrollTop){this.y++; this.lines.splice(this.y + this.ybase,0,this.blankLine(true));j = this.rows - 1 - this.scrollBottom;this.lines.splice(this.rows - 1 + this.ybase - j + 1,1); this.updateRange(this.scrollTop);this.updateRange(this.scrollBottom);}this.state = normal;}; Terminal.prototype.reset = function(){this.options.rows = this.rows;this.options.cols = this.cols;Terminal.call(this,this.options);this.refresh(0,this.rows - 1);}; Terminal.prototype.tabSet = function(){this.tabs[this.x]= true;this.state = normal;}; Terminal.prototype.cursorUp = function(params){var param = params[0];if(param < 1)param = 1;this.y -= param;if(this.y < 0)this.y = 0;}; Terminal.prototype.cursorDown = function(params){var param = params[0];if(param < 1)param = 1;this.y += param;if(this.y >= this.rows){this.y = this.rows - 1;}}; Terminal.prototype.cursorForward = function(params){var param = params[0];if(param < 1)param = 1;this.x += param;if(this.x >= this.cols){this.x = this.cols - 1;}}; Terminal.prototype.cursorBackward = function(params){var param = params[0];if(param < 1)param = 1;this.x -= param;if(this.x < 0)this.x = 0;}; Terminal.prototype.cursorPos = function(params){var row,col;row = params[0]- 1;if(params.length >= 2){col = params[1]- 1;}else{col = 0;}if(row < 0){row = 0;}else if(row >= this.rows){row = this.rows - 1;}if(col < 0){col = 0;}else if(col >= this.cols){col = this.cols - 1;}this.x = col;this.y = row;}; Terminal.prototype.eraseInDisplay = function(params){var j;switch(params[0]){case 0: this.eraseRight(this.x,this.y);j = this.y + 1;for(;j < this.rows;j++){this.eraseLine(j);}break;case 1: this.eraseLeft(this.x,this.y);j = this.y;while(j--){this.eraseLine(j);}break;case 2: j = this.rows;while(j--)this.eraseLine(j);break;case 3:; break;}}; Terminal.prototype.eraseInLine = function(params){switch(params[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);break;}}; Terminal.prototype.charAttributes = function(params){ if(params.length === 1 && params[0]=== 0){this.curAttr = this.defAttr;return;}var l = params.length,i = 0,flags = this.curAttr >> 18,fg =(this.curAttr >> 9)& 0x1ff,bg = this.curAttr & 0x1ff,p;for(;i < l;i++){p = params[i];if(p >= 30 && p <= 37){ fg = p - 30;}else if(p >= 40 && p <= 47){ bg = p - 40;}else if(p >= 90 && p <= 97){ p += 8;fg = p - 90;}else if(p >= 100 && p <= 107){ p += 8;bg = p - 100;}else if(p === 0){ flags = this.defAttr >> 18;fg =(this.defAttr >> 9)& 0x1ff;bg = this.defAttr & 0x1ff;}else if(p === 1){ flags |= 1;}else if(p === 4){ flags |= 2;}else if(p === 5){ flags |= 4;}else if(p === 7){ flags |= 8;}else if(p === 8){ flags |= 16;}else if(p === 22){ flags &= ~1;}else if(p === 24){ flags &= ~2;}else if(p === 25){ flags &= ~4;}else if(p === 27){ flags &= ~8;}else if(p === 28){ flags &= ~16;}else if(p === 39){ fg =(this.defAttr >> 9)& 0x1ff;}else if(p === 49){ bg = this.defAttr & 0x1ff;}else if(p === 38){ if(params[i + 1]=== 2){i += 2;fg = matchColor(params[i]& 0xff,params[i + 1]& 0xff,params[i + 2]& 0xff);if(fg === -1)fg = 0x1ff;i += 2;}else if(params[i + 1]=== 5){i += 2;p = params[i]& 0xff;fg = p;}}else if(p === 48){ if(params[i + 1]=== 2){i += 2;bg = matchColor(params[i]& 0xff,params[i + 1]& 0xff,params[i + 2]& 0xff);if(bg === -1)bg = 0x1ff;i += 2;}else if(params[i + 1]=== 5){i += 2;p = params[i]& 0xff;bg = p;}}else if(p === 100){ fg =(this.defAttr >> 9)& 0x1ff;bg = this.defAttr & 0x1ff;}else{this.error('Unknown SGR attribute: %d.',p);}}this.curAttr =(flags << 18)|(fg << 9)| bg;}; Terminal.prototype.deviceStatus = function(params){if(!this.prefix){switch(params[0]){case 5: this.send('\x1b[0n');break;case 6: this.send('\x1b[' +(this.y + 1)+ ';' +(this.x + 1)+ 'R');break;}}else if(this.prefix === '?'){ switch(params[0]){case 6: this.send('\x1b[?' +(this.y + 1)+ ';' +(this.x + 1)+ 'R');break;case 15: break;case 25: break;case 26: break;case 53: break;}}}; Terminal.prototype.insertChars = function(params){var param,row,j,ch;param = params[0];if(param < 1)param = 1;row = this.y + this.ybase;j = this.x;ch =[this.eraseAttr(),' ']; while(param-- && j < this.cols){this.lines[row].splice(j++,0,ch);this.lines[row].pop();}}; Terminal.prototype.cursorNextLine = function(params){var param = params[0];if(param < 1)param = 1;this.y += param;if(this.y >= this.rows){this.y = this.rows - 1;}this.x = 0;}; Terminal.prototype.cursorPrecedingLine = function(params){var param = params[0];if(param < 1)param = 1;this.y -= param;if(this.y < 0)this.y = 0;this.x = 0;}; Terminal.prototype.cursorCharAbsolute = function(params){var param = params[0];if(param < 1)param = 1;this.x = param - 1;}; Terminal.prototype.insertLines = function(params){var param,row,j;param = params[0];if(param < 1)param = 1;row = this.y + this.ybase;j = this.rows - 1 - this.scrollBottom;j = this.rows - 1 + this.ybase - j + 1;while(param--){ this.lines.splice(row,0,this.blankLine(true));this.lines.splice(j,1);} this.updateRange(this.y);this.updateRange(this.scrollBottom);}; Terminal.prototype.deleteLines = function(params){var param,row,j;param = params[0];if(param < 1)param = 1;row = this.y + this.ybase;j = this.rows - 1 - this.scrollBottom;j = this.rows - 1 + this.ybase - j;while(param--){ this.lines.splice(j + 1,0,this.blankLine(true));this.lines.splice(row,1);} this.updateRange(this.y);this.updateRange(this.scrollBottom);}; Terminal.prototype.deleteChars = function(params){var param,row,ch;param = params[0];if(param < 1)param = 1;row = this.y + this.ybase;ch =[this.eraseAttr(),' ']; while(param--){this.lines[row].splice(this.x,1);this.lines[row].push(ch);}}; Terminal.prototype.eraseChars = function(params){var param,row,j,ch;param = params[0];if(param < 1)param = 1;row = this.y + this.ybase;j = this.x;ch =[this.eraseAttr(),' ']; while(param-- && j < this.cols){this.lines[row][j++]= ch;}}; Terminal.prototype.charPosAbsolute = function(params){var param = params[0];if(param < 1)param = 1;this.x = param - 1;if(this.x >= this.cols){this.x = this.cols - 1;}}; Terminal.prototype.HPositionRelative = function(params){var param = params[0];if(param < 1)param = 1;this.x += param;if(this.x >= this.cols){this.x = this.cols - 1;}}; Terminal.prototype.sendDeviceAttributes = function(params){if(params[0]> 0)return;if(!this.prefix){if(this.is('xterm')|| this.is('rxvt-unicode')|| this.is('screen')){this.send('\x1b[?1;2c');}else if(this.is('linux')){this.send('\x1b[?6c');}}else if(this.prefix === '>'){ if(this.is('xterm')){this.send('\x1b[>0;276;0c');}else if(this.is('rxvt-unicode')){this.send('\x1b[>85;95;0c');}else if(this.is('linux')){ this.send(params[0]+ 'c');}else if(this.is('screen')){this.send('\x1b[>83;40003;0c');}}}; Terminal.prototype.linePosAbsolute = function(params){var param = params[0];if(param < 1)param = 1;this.y = param - 1;if(this.y >= this.rows){this.y = this.rows - 1;}}; Terminal.prototype.VPositionRelative = function(params){var param = params[0];if(param < 1)param = 1;this.y += param;if(this.y >= this.rows){this.y = this.rows - 1;}}; Terminal.prototype.HVPosition = function(params){if(params[0]< 1)params[0]= 1;if(params[1]< 1)params[1]= 1;this.y = params[0]- 1;if(this.y >= this.rows){this.y = this.rows - 1;}this.x = params[1]- 1;if(this.x >= this.cols){this.x = this.cols - 1;}}; Terminal.prototype.setMode = function(params){if(typeof params === 'object'){var l = params.length,i = 0;for(;i < l;i++){this.setMode(params[i]);}return;}if(!this.prefix){switch(params){case 4: this.insertMode = true;break;case 20: break;}}else if(this.prefix === '?'){switch(params){case 1: this.applicationCursor = true;break;case 2: this.setgCharset(0,Terminal.charsets.US);this.setgCharset(1,Terminal.charsets.US);this.setgCharset(2,Terminal.charsets.US);this.setgCharset(3,Terminal.charsets.US); break;case 3: this.savedCols = this.cols;this.resize(132,this.rows);break;case 6: this.originMode = true;break;case 7: this.wraparoundMode = true;break;case 12: break;case 66: this.log('Serial port requested application keypad.');this.applicationKeypad = true;break;case 9: case 1000: case 1002: case 1003: this.x10Mouse = params === 9;this.vt200Mouse = params === 1000;this.normalMouse = params > 1000;this.mouseEvents = true;this.element.style.cursor = 'default';this.log('Binding to mouse events.');break;case 1004: this.sendFocus = true;break;case 1005: this.utfMouse = true; break;case 1006: this.sgrMouse = true; break;case 1015: this.urxvtMouse = true; break;case 25: this.cursorHidden = false;break;case 1049:; case 47: case 1047: if(!this.normal){var normal ={lines: this.lines,ybase: this.ybase,ydisp: this.ydisp,x: this.x,y: this.y,scrollTop: this.scrollTop,scrollBottom: this.scrollBottom,tabs: this.tabs};this.reset();this.normal = normal;this.showCursor();}break;}}}; Terminal.prototype.resetMode = function(params){if(typeof params === 'object'){var l = params.length,i = 0;for(;i < l;i++){this.resetMode(params[i]);}return;}if(!this.prefix){switch(params){case 4: this.insertMode = false;break;case 20: break;}}else if(this.prefix === '?'){switch(params){case 1: this.applicationCursor = false;break;case 3: if(this.cols === 132 && this.savedCols){this.resize(this.savedCols,this.rows);}delete this.savedCols;break;case 6: this.originMode = false;break;case 7: this.wraparoundMode = false;break;case 12: break;case 66: this.log('Switching back to normal keypad.');this.applicationKeypad = false;break;case 9: case 1000: case 1002: case 1003: this.x10Mouse = false;this.vt200Mouse = false;this.normalMouse = false;this.mouseEvents = false;this.element.style.cursor = '';break;case 1004: this.sendFocus = false;break;case 1005: this.utfMouse = false;break;case 1006: this.sgrMouse = false;break;case 1015: this.urxvtMouse = false;break;case 25: this.cursorHidden = true;break;case 1049:; case 47: case 1047: if(this.normal){this.lines = this.normal.lines;this.ybase = this.normal.ybase;this.ydisp = this.normal.ydisp;this.x = this.normal.x;this.y = this.normal.y;this.scrollTop = this.normal.scrollTop;this.scrollBottom = this.normal.scrollBottom;this.tabs = this.normal.tabs;this.normal = null; this.refresh(0,this.rows - 1);this.showCursor();}break;}}}; Terminal.prototype.setScrollRegion = function(params){if(this.prefix)return;this.scrollTop =(params[0]|| 1)- 1;this.scrollBottom =(params[1]|| this.rows)- 1;this.x = 0;this.y = 0;}; Terminal.prototype.saveCursor = function(params){this.savedX = this.x;this.savedY = this.y;}; Terminal.prototype.restoreCursor = function(params){this.x = this.savedX || 0;this.y = this.savedY || 0;}; Terminal.prototype.cursorForwardTab = function(params){var param = params[0]|| 1;while(param--){this.x = this.nextStop();}}; Terminal.prototype.scrollUp = function(params){var param = params[0]|| 1;while(param--){this.lines.splice(this.ybase + this.scrollTop,1);this.lines.splice(this.ybase + this.scrollBottom,0,this.blankLine());} this.updateRange(this.scrollTop);this.updateRange(this.scrollBottom);}; Terminal.prototype.scrollDown = function(params){var param = params[0]|| 1;while(param--){this.lines.splice(this.ybase + this.scrollBottom,1);this.lines.splice(this.ybase + this.scrollTop,0,this.blankLine());} this.updateRange(this.scrollTop);this.updateRange(this.scrollBottom);}; Terminal.prototype.initMouseTracking = function(params){}; Terminal.prototype.resetTitleModes = function(params){;}; Terminal.prototype.cursorBackwardTab = function(params){var param = params[0]|| 1;while(param--){this.x = this.prevStop();}}; Terminal.prototype.repeatPrecedingCharacter = function(params){var param = params[0]|| 1,line = this.lines[this.ybase + this.y],ch = line[this.x - 1]||[this.defAttr,' '];while(param--)line[this.x++]= ch;}; Terminal.prototype.tabClear = function(params){var param = params[0];if(param <= 0){delete this.tabs[this.x];}else if(param === 3){this.tabs ={};}}; Terminal.prototype.mediaCopy = function(params){;}; Terminal.prototype.setResources = function(params){;}; Terminal.prototype.disableModifiers = function(params){;}; Terminal.prototype.setPointerMode = function(params){;}; Terminal.prototype.softReset = function(params){this.cursorHidden = false;this.insertMode = false;this.originMode = false;this.wraparoundMode = false; this.applicationKeypad = false; this.applicationCursor = false;this.scrollTop = 0;this.scrollBottom = this.rows - 1;this.curAttr = this.defAttr;this.x = this.y = 0; this.charset = null;this.glevel = 0; this.charsets =[null];}; Terminal.prototype.requestAnsiMode = function(params){;}; Terminal.prototype.requestPrivateMode = function(params){;}; Terminal.prototype.setConformanceLevel = function(params){;}; Terminal.prototype.loadLEDs = function(params){;}; Terminal.prototype.setCursorStyle = function(params){;}; Terminal.prototype.setCharProtectionAttr = function(params){;}; Terminal.prototype.restorePrivateValues = function(params){;}; Terminal.prototype.setAttrInRectangle = function(params){var t = params[0],l = params[1],b = params[2],r = params[3],attr = params[4];var line,i;for(;t < b + 1;t++){line = this.lines[this.ybase + t];for(i = l;i < r;i++){line[i]=[attr,line[i][1]];}} this.updateRange(params[0]);this.updateRange(params[2]);}; Terminal.prototype.savePrivateValues = function(params){;}; Terminal.prototype.manipulateWindow = function(params){;}; Terminal.prototype.reverseAttrInRectangle = function(params){;}; Terminal.prototype.setTitleModeFeature = function(params){;}; Terminal.prototype.setWarningBellVolume = function(params){;}; Terminal.prototype.setMarginBellVolume = function(params){;}; Terminal.prototype.copyRectangle = function(params){;}; Terminal.prototype.enableFilterRectangle = function(params){;}; Terminal.prototype.requestParameters = function(params){;}; Terminal.prototype.selectChangeExtent = function(params){;}; Terminal.prototype.fillRectangle = function(params){var ch = params[0],t = params[1],l = params[2],b = params[3],r = params[4];var line,i;for(;t < b + 1;t++){line = this.lines[this.ybase + t];for(i = l;i < r;i++){line[i]=[line[i][0],String.fromCharCode(ch)];}} this.updateRange(params[1]);this.updateRange(params[3]);}; Terminal.prototype.enableLocatorReporting = function(params){var val = params[0]> 0;}; Terminal.prototype.eraseRectangle = function(params){var t = params[0],l = params[1],b = params[2],r = params[3];var line,i,ch;ch =[this.eraseAttr(),' ']; for(;t < b + 1;t++){line = this.lines[this.ybase + t];for(i = l;i < r;i++){line[i]= ch;}} this.updateRange(params[0]);this.updateRange(params[2]);}; Terminal.prototype.setLocatorEvents = function(params){;}; Terminal.prototype.selectiveEraseRectangle = function(params){;}; Terminal.prototype.requestLocatorPosition = function(params){;}; Terminal.prototype.insertColumns = function(){var param = params[0],l = this.ybase + this.rows,ch =[this.eraseAttr(),' '],i;while(param--){for(i = this.ybase;i < l;i++){this.lines[i].splice(this.x + 1,0,ch);this.lines[i].pop();}}this.maxRange();}; Terminal.prototype.deleteColumns = function(){var param = params[0],l = this.ybase + this.rows,ch =[this.eraseAttr(),' '],i;while(param--){for(i = this.ybase;i < l;i++){this.lines[i].splice(this.x,1);this.lines[i].push(ch);}}this.maxRange();};Terminal.prototype.enterPrefix = function(){this.prefixMode = true;};Terminal.prototype.leavePrefix = function(){this.prefixMode = false;};Terminal.prototype.enterSelect = function(){this._real ={x: this.x,y: this.y,ydisp: this.ydisp,ybase: this.ybase,cursorHidden: this.cursorHidden,lines: this.copyBuffer(this.lines),write: this.write};this.write = function(){};this.selectMode = true;this.visualMode = false;this.cursorHidden = false;this.refresh(this.y,this.y);};Terminal.prototype.leaveSelect = function(){this.x = this._real.x;this.y = this._real.y;this.ydisp = this._real.ydisp;this.ybase = this._real.ybase;this.cursorHidden = this._real.cursorHidden;this.lines = this._real.lines;this.write = this._real.write;delete this._real;this.selectMode = false;this.visualMode = false;this.refresh(0,this.rows - 1);};Terminal.prototype.enterVisual = function(){this._real.preVisual = this.copyBuffer(this.lines);this.selectText(this.x,this.x,this.ydisp + this.y,this.ydisp + this.y);this.visualMode = true;};Terminal.prototype.leaveVisual = function(){this.lines = this._real.preVisual;delete this._real.preVisual;delete this._selected;this.visualMode = false;this.refresh(0,this.rows - 1);};Terminal.prototype.enterSearch = function(down){this.entry = '';this.searchMode = true;this.searchDown = down;this._real.preSearch = this.copyBuffer(this.lines);this._real.preSearchX = this.x;this._real.preSearchY = this.y;var bottom = this.ydisp + this.rows - 1;for(var i = 0;i < this.entryPrefix.length;i++){ this.lines[bottom][i]=[(this.defAttr & ~0x1ff)| 4,this.entryPrefix[i]];}this.y = this.rows - 1;this.x = this.entryPrefix.length;this.refresh(this.rows - 1,this.rows - 1);};Terminal.prototype.leaveSearch = function(){this.searchMode = false;if(this._real.preSearch){this.lines = this._real.preSearch;this.x = this._real.preSearchX;this.y = this._real.preSearchY;delete this._real.preSearch;delete this._real.preSearchX;delete this._real.preSearchY;}this.refresh(this.rows - 1,this.rows - 1);};Terminal.prototype.copyBuffer = function(lines){var lines = lines || this.lines,out =[];for(var y = 0;y < lines.length;y++){out[y]=[];for(var x = 0;x < lines[y].length;x++){out[y][x]=[lines[y][x][0],lines[y][x][1]];}}return out;};Terminal.prototype.getCopyTextarea = function(text){var textarea = this._copyTextarea,document = this.document;if(!textarea){textarea = document.createElement('textarea');textarea.style.position = 'absolute';textarea.style.left = '-32000px';textarea.style.top = '-32000px';textarea.style.width = '0px';textarea.style.height = '0px';textarea.style.opacity = '0';textarea.style.backgroundColor = 'transparent';textarea.style.borderStyle = 'none';textarea.style.outlineStyle = 'none';document.getElementsByTagName('body')[0].appendChild(textarea);this._copyTextarea = textarea;}return textarea;}; Terminal.prototype.copyText = function(text){var self = this,textarea = this.getCopyTextarea();this.emit('copy',text);textarea.focus();textarea.textContent = text;textarea.value = text;textarea.setSelectionRange(0,text.length);setTimeout(function(){self.element.focus();self.focus();},1);};Terminal.prototype.selectText = function(x1,x2,y1,y2){var ox1,ox2,oy1,oy2,tmp,x,y,xl,attr;if(this._selected){ox1 = this._selected.x1;ox2 = this._selected.x2;oy1 = this._selected.y1;oy2 = this._selected.y2;if(oy2 < oy1){tmp = ox2;ox2 = ox1;ox1 = tmp;tmp = oy2;oy2 = oy1;oy1 = tmp;}if(ox2 < ox1 && oy1 === oy2){tmp = ox2;ox2 = ox1;ox1 = tmp;}for(y = oy1;y <= oy2;y++){x = 0;xl = this.cols - 1;if(y === oy1){x = ox1;}if(y === oy2){xl = ox2;}for(;x <= xl;x++){if(this.lines[y][x].old != null){ attr = this.lines[y][x].old;delete this.lines[y][x].old;this.lines[y][x]=[attr,this.lines[y][x][1]];}}}y1 = this._selected.y1;x1 = this._selected.x1;}y1 = Math.max(y1,0);y1 = Math.min(y1,this.ydisp + this.rows - 1);y2 = Math.max(y2,0);y2 = Math.min(y2,this.ydisp + this.rows - 1);this._selected ={x1: x1,x2: x2,y1: y1,y2: y2};if(y2 < y1){tmp = x2;x2 = x1;x1 = tmp;tmp = y2;y2 = y1;y1 = tmp;}if(x2 < x1 && y1 === y2){tmp = x2;x2 = x1;x1 = tmp;}for(y = y1;y <= y2;y++){x = 0;xl = this.cols - 1;if(y === y1){x = x1;}if(y === y2){xl = x2;}for(;x <= xl;x++){ attr = this.lines[y][x][0];this.lines[y][x]=[(attr & ~0x1ff)|((0x1ff << 9)| 4),this.lines[y][x][1]];this.lines[y][x].old = attr;}}y1 = y1 - this.ydisp;y2 = y2 - this.ydisp;y1 = Math.max(y1,0);y1 = Math.min(y1,this.rows - 1);y2 = Math.max(y2,0);y2 = Math.min(y2,this.rows - 1); this.refresh(0,this.rows - 1);};Terminal.prototype.grabText = function(x1,x2,y1,y2){var out = '',buf = '',ch,x,y,xl,tmp;if(y2 < y1){tmp = x2;x2 = x1;x1 = tmp;tmp = y2;y2 = y1;y1 = tmp;}if(x2 < x1 && y1 === y2){tmp = x2;x2 = x1;x1 = tmp;}for(y = y1;y <= y2;y++){x = 0;xl = this.cols - 1;if(y === y1){x = x1;}if(y === y2){xl = x2;}for(;x <= xl;x++){ch = this.lines[y][x][1];if(ch === ' '){buf += ch;continue;}if(buf){out += buf;buf = '';}out += ch;if(isWide(ch))x++;}buf = '';out += '\n';} for(x = x2,y = y2;x < this.cols;x++){if(this.lines[y][x][1]!== ' '){out = out.slice(0,-1);break;}}return out;};Terminal.prototype.keyPrefix = function(ev,key){if(key === 'k' || key === '&'){this.destroy();}else if(key === 'p' || key === ']'){this.emit('request paste');}else if(key === 'c'){this.emit('request create');}else if(key >= '0' && key <= '9'){key = +key - 1;if(!~key)key = 9;this.emit('request term',key);}else if(key === 'n'){this.emit('request term next');}else if(key === 'P'){this.emit('request term previous');}else if(key === ':'){this.emit('request command mode');}else if(key === '['){this.enterSelect();}};Terminal.prototype.keySelect = function(ev,key){this.showCursor();if(this.searchMode || key === 'n' || key === 'N'){return this.keySearch(ev,key);}if(key === '\x04'){ var y = this.ydisp + this.y;if(this.ydisp === this.ybase){ this.y = Math.min(this.y +(this.rows - 1)/ 2 | 0,this.rows - 1);this.refresh(0,this.rows - 1);}else{this.scrollDisp((this.rows - 1)/ 2 | 0);}if(this.visualMode){this.selectText(this.x,this.x,y,this.ydisp + this.y);}return;}if(key === '\x15'){ var y = this.ydisp + this.y;if(this.ydisp === 0){ this.y = Math.max(this.y -(this.rows - 1)/ 2 | 0,0);this.refresh(0,this.rows - 1);}else{this.scrollDisp(-(this.rows - 1)/ 2 | 0);}if(this.visualMode){this.selectText(this.x,this.x,y,this.ydisp + this.y);}return;}if(key === '\x06'){ var y = this.ydisp + this.y;this.scrollDisp(this.rows - 1);if(this.visualMode){this.selectText(this.x,this.x,y,this.ydisp + this.y);}return;}if(key === '\x02'){ var y = this.ydisp + this.y;this.scrollDisp(-(this.rows - 1));if(this.visualMode){this.selectText(this.x,this.x,y,this.ydisp + this.y);}return;}if(key === 'k' || key === '\x1b[A'){var y = this.ydisp + this.y;this.y--;if(this.y < 0){this.y = 0;this.scrollDisp(-1);}if(this.visualMode){this.selectText(this.x,this.x,y,this.ydisp + this.y);}else{this.refresh(this.y,this.y + 1);}return;}if(key === 'j' || key === '\x1b[B'){var y = this.ydisp + this.y;this.y++;if(this.y >= this.rows){this.y = this.rows - 1;this.scrollDisp(1);}if(this.visualMode){this.selectText(this.x,this.x,y,this.ydisp + this.y);}else{this.refresh(this.y - 1,this.y);}return;}if(key === 'h' || key === '\x1b[D'){var x = this.x;this.x--;if(this.x < 0){this.x = 0;}if(this.visualMode){this.selectText(x,this.x,this.ydisp + this.y,this.ydisp + this.y);}else{this.refresh(this.y,this.y);}return;}if(key === 'l' || key === '\x1b[C'){var x = this.x;this.x++;if(this.x >= this.cols){this.x = this.cols - 1;}if(this.visualMode){this.selectText(x,this.x,this.ydisp + this.y,this.ydisp + this.y);}else{this.refresh(this.y,this.y);}return;}if(key === 'v' || key === ' '){if(!this.visualMode){this.enterVisual();}else{this.leaveVisual();}return;}if(key === 'y'){if(this.visualMode){var text = this.grabText(this._selected.x1,this._selected.x2,this._selected.y1,this._selected.y2);this.copyText(text);this.leaveVisual();}return;}if(key === 'q' || key === '\x1b'){if(this.visualMode){this.leaveVisual();}else{this.leaveSelect();}return;}if(key === 'w' || key === 'W'){var ox = this.x;var oy = this.y;var oyd = this.ydisp;var x = this.x;var y = this.y;var yb = this.ydisp;var saw_space = false;for(;;){var line = this.lines[yb + y];while(x < this.cols){if(line[x][1]<= ' '){saw_space = true;}else if(saw_space){break;}x++;}if(x >= this.cols)x = this.cols - 1;if(x === this.cols - 1 && line[x][1]<= ' '){x = 0;if(++y >= this.rows){y--;if(++yb > this.ybase){yb = this.ybase;x = this.x;break;}}continue;}break;}this.x = x,this.y = y;this.scrollDisp(-this.ydisp + yb);if(this.visualMode){this.selectText(ox,this.x,oy + oyd,this.ydisp + this.y);}return;}if(key === 'b' || key === 'B'){var ox = this.x;var oy = this.y;var oyd = this.ydisp;var x = this.x;var y = this.y;var yb = this.ydisp;for(;;){var line = this.lines[yb + y];var saw_space = x > 0 && line[x][1]> ' ' && line[x - 1][1]> ' ';while(x >= 0){if(line[x][1]<= ' '){if(saw_space &&(x + 1 < this.cols && line[x + 1][1]> ' ')){x++;break;}else{saw_space = true;}}x--;}if(x < 0)x = 0;if(x === 0 &&(line[x][1]<= ' ' || !saw_space)){x = this.cols - 1;if(--y < 0){y++;if(--yb < 0){yb++;x = 0;break;}}continue;}break;}this.x = x,this.y = y;this.scrollDisp(-this.ydisp + yb);if(this.visualMode){this.selectText(ox,this.x,oy + oyd,this.ydisp + this.y);}return;}if(key === 'e' || key === 'E'){var x = this.x + 1;var y = this.y;var yb = this.ydisp;if(x >= this.cols)x--;for(;;){var line = this.lines[yb + y];while(x < this.cols){if(line[x][1]<= ' '){x++;}else{break;}}while(x < this.cols){if(line[x][1]<= ' '){if(x - 1 >= 0 && line[x - 1][1]> ' '){x--;break;}}x++;}if(x >= this.cols)x = this.cols - 1;if(x === this.cols - 1 && line[x][1]<= ' '){x = 0;if(++y >= this.rows){y--;if(++yb > this.ybase){yb = this.ybase;break;}}continue;}break;}this.x = x,this.y = y;this.scrollDisp(-this.ydisp + yb);if(this.visualMode){this.selectText(ox,this.x,oy + oyd,this.ydisp + this.y);}return;}if(key === '^' || key === '0'){var ox = this.x;if(key === '0'){this.x = 0;}else if(key === '^'){var line = this.lines[this.ydisp + this.y];var x = 0;while(x < this.cols){if(line[x][1]> ' '){break;}x++;}if(x >= this.cols)x = this.cols - 1;this.x = x;}if(this.visualMode){this.selectText(ox,this.x,this.ydisp + this.y,this.ydisp + this.y);}else{this.refresh(this.y,this.y);}return;}if(key === '$'){var ox = this.x;var line = this.lines[this.ydisp + this.y];var x = this.cols - 1;while(x >= 0){if(line[x][1]> ' '){if(this.visualMode && x < this.cols - 1)x++;break;}x--;}if(x < 0)x = 0;this.x = x;if(this.visualMode){this.selectText(ox,this.x,this.ydisp + this.y,this.ydisp + this.y);}else{this.refresh(this.y,this.y);}return;}if(key === 'g' || key === 'G'){var ox = this.x;var oy = this.y;var oyd = this.ydisp;if(key === 'g'){this.x = 0,this.y = 0;this.scrollDisp(-this.ydisp);}else if(key === 'G'){this.x = 0,this.y = this.rows - 1;this.scrollDisp(this.ybase);}if(this.visualMode){this.selectText(ox,this.x,oy + oyd,this.ydisp + this.y);}return;}if(key === 'H' || key === 'M' || key === 'L'){var ox = this.x;var oy = this.y;if(key === 'H'){this.x = 0,this.y = 0;}else if(key === 'M'){this.x = 0,this.y = this.rows / 2 | 0;}else if(key === 'L'){this.x = 0,this.y = this.rows - 1;}if(this.visualMode){this.selectText(ox,this.x,this.ydisp + oy,this.ydisp + this.y);}else{this.refresh(oy,oy);this.refresh(this.y,this.y);}return;}if(key === '{' || key === '}'){var ox = this.x;var oy = this.y;var oyd = this.ydisp;var line;var saw_full = false;var found = false;var first_is_space = -1;var y = this.y +(key === '{' ? -1 : 1);var yb = this.ydisp;var i;if(key === '{'){if(y < 0){y++;if(yb > 0)yb--;}}else if(key === '}'){if(y >= this.rows){y--;if(yb < this.ybase)yb++;}}for(;;){line = this.lines[yb + y];for(i = 0;i < this.cols;i++){if(line[i][1]> ' '){if(first_is_space === -1){first_is_space = 0;}saw_full = true;break;}else if(i === this.cols - 1){if(first_is_space === -1){first_is_space = 1;}else if(first_is_space === 0){found = true;}else if(first_is_space === 1){if(saw_full)found = true;}break;}}if(found)break;if(key === '{'){y--;if(y < 0){y++;if(yb > 0)yb--;else break;}}else if(key === '}'){y++;if(y >= this.rows){y--;if(yb < this.ybase)yb++;else break;}}}if(!found){if(key === '{'){y = 0;yb = 0;}else if(key === '}'){y = this.rows - 1;yb = this.ybase;}}this.x = 0,this.y = y;this.scrollDisp(-this.ydisp + yb);if(this.visualMode){this.selectText(ox,this.x,oy + oyd,this.ydisp + this.y);}return;}if(key === '/' || key === '?'){if(!this.visualMode){this.enterSearch(key === '/');}return;}return false;};Terminal.prototype.keySearch = function(ev,key){if(key === '\x1b'){this.leaveSearch();return;}if(key === '\r' ||(!this.searchMode &&(key === 'n' || key === 'N'))){this.leaveSearch();var entry = this.entry;if(!entry){this.refresh(0,this.rows - 1);return;}var ox = this.x;var oy = this.y;var oyd = this.ydisp;var line;var found = false;var wrapped = false;var x = this.x + 1;var y = this.ydisp + this.y;var yb,i;var up = key === 'N' ? this.searchDown : !this.searchDown;for(;;){line = this.lines[y];while(x < this.cols){for(i = 0;i < entry.length;i++){if(x + i >= this.cols)break;if(line[x + i][1]!== entry[i]){break;}else if(line[x + i][1]=== entry[i]&& i === entry.length - 1){found = true;break;}}if(found)break;x += i + 1;}if(found)break;x = 0;if(!up){y++;if(y > this.ybase + this.rows - 1){if(wrapped)break; wrapped = true;y = 0;}}else{y--;if(y < 0){if(wrapped)break; wrapped = true;y = this.ybase + this.rows - 1;}}}if(found){if(y - this.ybase < 0){yb = y;y = 0;if(yb > this.ybase){y = yb - this.ybase;yb = this.ybase;}}else{yb = this.ybase;y -= this.ybase;}this.x = x,this.y = y;this.scrollDisp(-this.ydisp + yb);if(this.visualMode){this.selectText(ox,this.x,oy + oyd,this.ydisp + this.y);}return;} this.refresh(0,this.rows - 1);return;}if(key === '\b' || key === '\x7f'){if(this.entry.length === 0)return;var bottom = this.ydisp + this.rows - 1;this.entry = this.entry.slice(0,-1);var i = this.entryPrefix.length + this.entry.length; this.lines[bottom][i]=[this.lines[bottom][i][0],' '];this.x--;this.refresh(this.rows - 1,this.rows - 1);this.refresh(this.y,this.y);return;}if(key.length === 1 && key >= ' ' && key <= '~'){var bottom = this.ydisp + this.rows - 1;this.entry += key;var i = this.entryPrefix.length + this.entry.length - 1; this.lines[bottom][i]=[(this.defAttr & ~0x1ff)| 4,key];this.x++;this.refresh(this.rows - 1,this.rows - 1);this.refresh(this.y,this.y);return;}return false;};Terminal.charsets ={}; Terminal.charsets.SCLD ={ '`': '\u25c6', 'a': '\u2592', 'b': '\u0009', 'c': '\u000c', 'd': '\u000d', 'e': '\u000a', 'f': '\u00b0', 'g': '\u00b1', 'h': '\u2424', 'i': '\u000b', 'j': '\u2518', 'k': '\u2510', 'l': '\u250c', 'm': '\u2514', 'n': '\u253c', 'o': '\u23ba', 'p': '\u23bb', 'q': '\u2500', 'r': '\u23bc', 's': '\u23bd', 't': '\u251c', 'u': '\u2524', 'v': '\u2534', 'w': '\u252c', 'x': '\u2502', 'y': '\u2264', 'z': '\u2265', '{': '\u03c0', '|': '\u2260', '}': '\u00a3', '~': '\u00b7'};Terminal.charsets.UK = null; Terminal.charsets.US = null; Terminal.charsets.Dutch = null; Terminal.charsets.Finnish = null; Terminal.charsets.French = null; Terminal.charsets.FrenchCanadian = null; Terminal.charsets.German = null; Terminal.charsets.Italian = null; Terminal.charsets.NorwegianDanish = null; Terminal.charsets.Spanish = null; Terminal.charsets.Swedish = null; Terminal.charsets.Swiss = null; Terminal.charsets.ISOLatin = null; function on(el,type,handler,capture){el.addEventListener(type,handler,capture || false);}function off(el,type,handler,capture){el.removeEventListener(type,handler,capture || false);}function cancel(ev){if(ev.preventDefault)ev.preventDefault();ev.returnValue = false;if(ev.stopPropagation)ev.stopPropagation();ev.cancelBubble = true;return false;}function inherits(child,parent){function f(){this.constructor = child;}f.prototype = parent.prototype;child.prototype = new f;} function isBoldBroken(document){var body = document.getElementsByTagName('body')[0];var terminal = document.createElement('div');terminal.className = 'terminal';var line = document.createElement('div');var el = document.createElement('span');el.innerHTML = 'hello world';line.appendChild(el);terminal.appendChild(line);body.appendChild(terminal);var w1 = el.scrollWidth;el.style.fontWeight = 'bold';var w2 = el.scrollWidth;body.removeChild(terminal);return w1 !== w2;}var String = this.String;var setTimeout = this.setTimeout;var setInterval = this.setInterval;function indexOf(obj,el){var i = obj.length;while(i--){if(obj[i]=== el)return i;}return -1;}function isWide(ch){if(ch <= '\uff00')return false;return(ch >= '\uff01' && ch <= '\uffbe')||(ch >= '\uffc2' && ch <= '\uffc7')||(ch >= '\uffca' && ch <= '\uffcf')||(ch >= '\uffd2' && ch <= '\uffd7')||(ch >= '\uffda' && ch <= '\uffdc')||(ch >= '\uffe0' && ch <= '\uffe6')||(ch >= '\uffe8' && ch <= '\uffee');}function matchColor(r1,g1,b1){var hash =(r1 << 16)|(g1 << 8)| b1;if(matchColor._cache[hash]!= null){return matchColor._cache[hash];}var ldiff = Infinity,li = -1,i = 0,c,r2,g2,b2,diff;for(;i < Terminal.vcolors.length;i++){c = Terminal.vcolors[i];r2 = c[0];g2 = c[1];b2 = c[2];diff = matchColor.distance(r1,g1,b1,r2,g2,b2);if(diff === 0){li = i;break;}if(diff < ldiff){ldiff = diff;li = i;}}return matchColor._cache[hash]= li;}matchColor._cache ={}; matchColor.distance = function(r1,g1,b1,r2,g2,b2){return Math.pow(30 *(r1 - r2),2)+ Math.pow(59 *(g1 - g2),2)+ Math.pow(11 *(b1 - b2),2);};function each(obj,iter,con){if(obj.forEach)return obj.forEach(iter,con);for(var i = 0;i < obj.length;i++){iter.call(con,obj[i],i,obj);}}function keys(obj){if(Object.keys)return Object.keys(obj);var key,keys =[];for(key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){keys.push(key);}}return keys;}Terminal.EventEmitter = EventEmitter;Terminal.Stream = Stream;Terminal.inherits = inherits;Terminal.on = on;Terminal.off = off;Terminal.cancel = cancel;if(typeof module !== 'undefined'){module.exports = Terminal;}else{this.Terminal = Terminal;}}).call(function(){return this ||(typeof window !== 'undefined' ? window : global);}());</script>
<script>var saveAs = saveAs ||(function(view){"use strict"; if(typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE[1-9]\./.test(navigator.userAgent)){return;}var doc = view.document,get_URL = function(){return view.URL || view.webkitURL || view;},save_link = doc.createElementNS("http://www.w3.org/1999/xhtml","a"),can_use_save_link = "download" in save_link,click = function(node){var event = new MouseEvent("click");node.dispatchEvent(event);},is_safari = /constructor/i.test(view.HTMLElement),is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent),throw_outside = function(ex){(view.setImmediate || view.setTimeout)(function(){throw ex;},0);},force_saveable_type = "application/octet-stream",arbitrary_revoke_timeout = 1000 * 40,revoke = function(file){var revoker = function(){if(typeof file === "string"){ get_URL().revokeObjectURL(file);}else{ file.remove();}};setTimeout(revoker,arbitrary_revoke_timeout);},dispatch = function(filesaver,event_types,event){event_types =[].concat(event_types);var i = event_types.length;while(i--){var listener = filesaver["on" + event_types[i]];if(typeof listener === "function"){try{listener.call(filesaver,event || filesaver);}catch(ex){throw_outside(ex);}}}},auto_bom = function(blob){ if(/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)){return new Blob([String.fromCharCode(0xFEFF),blob],{type: blob.type});}return blob;},FileSaver = function(blob,name,no_auto_bom){if(!no_auto_bom){blob = auto_bom(blob);} var filesaver = this,type = blob.type,force = type === force_saveable_type,object_url,dispatch_all = function(){dispatch(filesaver,"writestart progress write writeend".split(" "));},fs_error = function(){if((is_chrome_ios ||(force && is_safari))&& view.FileReader){ var reader = new FileReader();reader.onloadend = function(){var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/,'data:attachment/file;');var popup = view.open(url,'_blank');if(!popup)view.location.href = url;url=undefined; filesaver.readyState = filesaver.DONE;dispatch_all();};reader.readAsDataURL(blob);filesaver.readyState = filesaver.INIT;return;} if(!object_url){object_url = get_URL().createObjectURL(blob);}if(force){view.location.href = object_url;}else{var opened = view.open(object_url,"_blank");if(!opened){ view.location.href = object_url;}}filesaver.readyState = filesaver.DONE;dispatch_all();revoke(object_url);};filesaver.readyState = filesaver.INIT;if(can_use_save_link){object_url = get_URL().createObjectURL(blob);setTimeout(function(){save_link.href = object_url;save_link.download = name;click(save_link);dispatch_all();revoke(object_url);filesaver.readyState = filesaver.DONE;});return;}fs_error();},FS_proto = FileSaver.prototype,saveAs = function(blob,name,no_auto_bom){return new FileSaver(blob,name || blob.name || "download",no_auto_bom);}; if(typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob){return function(blob,name,no_auto_bom){name = name || blob.name || "download";if(!no_auto_bom){blob = auto_bom(blob);}return navigator.msSaveOrOpenBlob(blob,name);};}FS_proto.abort = function(){};FS_proto.readyState = FS_proto.INIT = 0;FS_proto.WRITING = 1;FS_proto.DONE = 2;FS_proto.error = FS_proto.onwritestart = FS_proto.onprogress = FS_proto.onwrite = FS_proto.onabort = FS_proto.onerror = FS_proto.onwriteend = null;return saveAs;}(typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content)); if(typeof module !== "undefined" && module.exports){module.exports.saveAs = saveAs;}else if((typeof define !== "undefined" && define !== null)&&(define.amd !== null)){define([],function(){return saveAs;});}</script>
</head>
<body>
<h1>MicroPythonOS WebREPL</h1>
<div style="display:inline-block; vertical-align:top;">
<form>
<input type="text" name="webrepl_url" id="url" value="" />
<input type="submit" id="button" value="Connect" onclick="button_click(); return false;" />
</form>
<div id="term-wrapper">
<div id="term"></div>
</div>
</div>
<div id="file-boxes" style="display:inline-block; vertical-align:top; width:230px;">
<div class="file-box">
<strong>Send a file</strong>
<input type="file" id="put-file-select" />
<div id="put-file-list"></div>
<input type="button" value="Send to device" id="put-file-button" onclick="put_file(); return false;" />
</div>
<div class="file-box">
<strong>Get a file</strong>
<input type="text" name="get_filename" id="get_filename" value="" size="13" />
<input type="button" value="Get from device" onclick="get_file(); return false;" />
</div>
<div class="file-box" id="file-status"><span style="color:#707070">(file operation status)</span></div>
</div>
<br clear="both" />
<i>Terminal widget should be focused (text cursor visible) to accept input. Click on it if not.</i><br/>
<i>To paste, press Ctrl+A, then Ctrl+V</i>
</body>
<script>var term;var ws;var connected = false;var binary_state = 0;var put_file_name = null;var put_file_data = null;var get_file_name = null;var get_file_data = null;function calculate_size(win){var cols = Math.max(80,Math.min(150,(win.innerWidth - 280)/ 7))| 0;var rows = Math.max(24,Math.min(80,(win.innerHeight - 180)/ 12))| 0;return[cols,rows];}(function(){window.onload = function(){var url = window.location.hash.substring(1);if(!url){ url = document.location.host;}document.getElementById('url').value = 'ws://' + url;var size = calculate_size(self);term = new Terminal({cols: size[0],rows: size[1],useStyle: true,screenKeys: true,cursorBlink: false});term.open(document.getElementById("term"));show_https_warning();};window.addEventListener('resize',function(){var size = calculate_size(self);term.resize(size[0],size[1]);});}).call(this);function show_https_warning(){if(window.location.protocol == 'https:'){var warningDiv = document.createElement('div');warningDiv.style.cssText = 'background:#f99;padding:5px;margin-bottom:10px;line-height:1.5em;text-align:center';warningDiv.innerHTML =['The WebREPL client cannot be accessed over HTTPS connections.','Load the WebREPL client from the device instead.'].join('<br>');document.body.insertBefore(warningDiv,document.body.childNodes[0]);term.resize(term.cols,term.rows - 7);}}function button_click(){if(connected){ws.close();}else{document.getElementById('url').disabled = true;document.getElementById('button').value = "Disconnect";connected = true;connect(document.getElementById('url').value);}}function prepare_for_connect(){document.getElementById('url').disabled = false;document.getElementById('button').value = "Connect";}function update_file_status(s){document.getElementById('file-status').innerHTML = s;}function connect(url){var hostport = url.substring(5);if(hostport === document.location.host){hostport = '';}window.location.hash = hostport;ws = new WebSocket(url);ws.binaryType = 'arraybuffer';ws.onopen = function(){term.removeAllListeners('data');term.on('data',function(data){ data = data.replace(/\n/g,"\r");ws.send(data);});term.on('title',function(title){document.title = title;});term.focus();term.element.focus();term.write('\x1b[31mWelcome to MicroPython!\x1b[m\r\n');ws.onmessage = function(event){if(event.data instanceof ArrayBuffer){var data = new Uint8Array(event.data);switch(binary_state){case 11: if(decode_resp(data)== 0){ for(var offset = 0;offset < put_file_data.length;offset += 1024){ws.send(put_file_data.slice(offset,offset + 1024));}binary_state = 12;}break;case 12: if(decode_resp(data)== 0){update_file_status('Sent ' + put_file_name + ', ' + put_file_data.length + ' bytes');}else{update_file_status('Failed sending ' + put_file_name);}binary_state = 0;break;case 21: if(decode_resp(data)== 0){binary_state = 22;var rec = new Uint8Array(1);rec[0]= 0;ws.send(rec);}break;case 22:{ var sz = data[0]|(data[1]<< 8);if(data.length == 2 + sz){ if(sz == 0){ binary_state = 23;}else{ var new_buf = new Uint8Array(get_file_data.length + sz);new_buf.set(get_file_data);new_buf.set(data.slice(2),get_file_data.length);get_file_data = new_buf;update_file_status('Getting ' + get_file_name + ', ' + get_file_data.length + ' bytes');var rec = new Uint8Array(1);rec[0]= 0;ws.send(rec);}}else{binary_state = 0;}break;}case 23: if(decode_resp(data)== 0){update_file_status('Got ' + get_file_name + ', ' + get_file_data.length + ' bytes');saveAs(new Blob([get_file_data],{type: "application/octet-stream"}),get_file_name);}else{update_file_status('Failed getting ' + get_file_name);}binary_state = 0;break;case 31: console.log('GET_VER',data);binary_state = 0;break;}}term.write(event.data);};};ws.onclose = function(){connected = false;if(term){term.write('\x1b[31mDisconnected\x1b[m\r\n');}term.off('data');prepare_for_connect();}}function decode_resp(data){if(data[0]== 'W'.charCodeAt(0)&& data[1]== 'B'.charCodeAt(0)){var code = data[2]|(data[3]<< 8);return code;}else{return -1;}}function put_file(){var dest_fname = put_file_name;var dest_fsize = put_file_data.length; var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64);rec[0]= 'W'.charCodeAt(0);rec[1]= 'A'.charCodeAt(0);rec[2]= 1; rec[3]= 0;rec[4]= 0;rec[5]= 0;rec[6]= 0;rec[7]= 0;rec[8]= 0;rec[9]= 0;rec[10]= 0;rec[11]= 0;rec[12]= dest_fsize & 0xff;rec[13]=(dest_fsize >> 8)& 0xff;rec[14]=(dest_fsize >> 16)& 0xff;rec[15]=(dest_fsize >> 24)& 0xff;rec[16]= dest_fname.length & 0xff;rec[17]=(dest_fname.length >> 8)& 0xff;for(var i = 0;i < 64;++i){if(i < dest_fname.length){rec[18 + i]= dest_fname.charCodeAt(i);}else{rec[18 + i]= 0;}} binary_state = 11;update_file_status('Sending ' + put_file_name + '...');ws.send(rec);}function get_file(){var src_fname = document.getElementById('get_filename').value; var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64);rec[0]= 'W'.charCodeAt(0);rec[1]= 'A'.charCodeAt(0);rec[2]= 2; rec[3]= 0;rec[4]= 0;rec[5]= 0;rec[6]= 0;rec[7]= 0;rec[8]= 0;rec[9]= 0;rec[10]= 0;rec[11]= 0;rec[12]= 0;rec[13]= 0;rec[14]= 0;rec[15]= 0;rec[16]= src_fname.length & 0xff;rec[17]=(src_fname.length >> 8)& 0xff;for(var i = 0;i < 64;++i){if(i < src_fname.length){rec[18 + i]= src_fname.charCodeAt(i);}else{rec[18 + i]= 0;}} binary_state = 21;get_file_name = src_fname;get_file_data = new Uint8Array(0);update_file_status('Getting ' + get_file_name + '...');ws.send(rec);}function get_ver(){ var rec = new Uint8Array(2 + 1 + 1 + 8 + 4 + 2 + 64);rec[0]= 'W'.charCodeAt(0);rec[1]= 'A'.charCodeAt(0);rec[2]= 3; binary_state = 31;ws.send(rec);}function handle_put_file_select(evt){ var files = evt.target.files; var f = files[0];put_file_name = f.name;var reader = new FileReader();reader.onload = function(e){put_file_data = new Uint8Array(e.target.result);document.getElementById('put-file-list').innerHTML = '' + escape(put_file_name)+ ' - ' + put_file_data.length + ' bytes';document.getElementById('put-file-button').disabled = false;};reader.readAsArrayBuffer(f);}document.getElementById('put-file-select').addEventListener('click',function(){this.value = null;},false);document.getElementById('put-file-select').addEventListener('change',handle_put_file_select,false);document.getElementById('put-file-button').disabled = true;</script>
<script>(function(){var maxReadyAttempts = 90;var readyAttempts = 0;var resizeScheduled = false;var resizeDebounce = null;function get_term_container(){return document.getElementById('term');} function get_term_wrapper(){return document.getElementById('term-wrapper')|| get_term_container();} function calculate_dynamic_rows(){var wrapper = get_term_wrapper();var termContainer = get_term_container();if(!wrapper || !termContainer){return null;}var rect = wrapper.getBoundingClientRect();var height = rect.height || wrapper.clientHeight || window.innerHeight;var style = window.getComputedStyle(termContainer);var paddingTop = parseFloat(style.paddingTop)|| 0;var paddingBottom = parseFloat(style.paddingBottom)|| 0;var contentHeight = height - paddingTop - paddingBottom;return Math.max(24,Math.min(80,(contentHeight - 20)/ 12))| 0;}function resize_term_rows(){if(!window.term || !window.term.resize){return false;}var rows = calculate_dynamic_rows();if(rows === null){return false;}var cols = window.term.cols || 80;if(window.term.rows !== rows){window.term.resize(cols,rows);}return true;}function schedule_resize(){if(resizeScheduled){return;}resizeScheduled = true;window.requestAnimationFrame(function(){resizeScheduled = false;resize_term_rows();});}function schedule_resize_debounced(){if(resizeDebounce){window.clearTimeout(resizeDebounce);}resizeDebounce = window.setTimeout(function(){resizeDebounce = null;schedule_resize();},80);}function wait_for_term_ready(){if(window.term && window.term.resize){schedule_resize_debounced();if(resize_term_rows()){return;}}if(readyAttempts >= maxReadyAttempts){return;}readyAttempts += 1;window.requestAnimationFrame(wait_for_term_ready);}window.addEventListener('load',function(){wait_for_term_ready();schedule_resize_debounced();});window.addEventListener('resize',function(){schedule_resize_debounced();});}).call(this);</script>
</html>