-
Notifications
You must be signed in to change notification settings - Fork 3
/
ptty.jquery.min.js
9 lines (9 loc) · 11.2 KB
/
ptty.jquery.min.js
1
2
3
4
5
6
7
8
9
/**
* @file : ptty.jquery.min.js
* @ver : 0.0.5 (beta)
* @author : Pachanka <social@pachanka.org>
* @url : http://goto.pachanka.org/ptty/docs
* @desc : Ptty (Pseudo teletype). A terminal emulator plugin for jQuery.
* @license: WTFPL Version 2. (http://www.wtfpl.net/)
**/
!function(e){"use strict";e.fn.Ptty=function(t){var n={};return this.each(function(){var i=e(this),o={},a={},r={},l={},s=[],d={ps:null,in:null,out:null,last:null,next:null,data:null},c=e.extend(!0,{ajax_options:{url:window.location.pathname,type:"POST"},param:"cmd",ps:"$",caret:"▮",caret_blink:800,native_css:!0,theme:"boring",native_cmds:!0,autocomplete:!0,history_max:800,autofocus:!0,before_cmd:!1,after_cmd:!1,i18n:{welcome:"Ptty (0.0.5 beta).<br> Type <b>help</b> to list the available commands.",error_not_found:"Command not found.",error_bad_method:"Invalid command method.",error_ajax:"Server error."}},t);n.get_terminal=function(e){return e?i.find(e):i},n.native_style=function(t,n){var i=t.attr("id");if(i=i?"#"+i:"."+t.attr("class").split(" ")[1],"boring"===n){var o=['.boring, .boring .prompt, .boring .content{ font-family: "Courier New", Courier, monospace; background-color: #111; color: #ddd; }',".boring .content{ padding: 15px 15px 0 15px; }",".boring .prompt{ padding: 0 15px 15px 15px; }",'.boring .loading span::after{content: "⚙"; color: #ddd; font-size: 10em; border-radius: 10em; opacity: 0.4;}',".boring .content ul{ margin: 0; }",".boring .prompt .input.show-caret{ color: #ddd; opacity: .85; }",".boring .prompt .input, .boring .prompt .input::before, .boring .prompt .input::after{ color: transparent; text-shadow:0 0 0 #ddd; }",".boring .content div .cmd_in .cmd_ps, .boring .prompt .input::before{ padding-right: 10px; }",".boring .content ul li{ list-style-type: none; }",".boring div.prompt div.input::after{ font-size: 2em; }",".boring div.prompt div.input, .boring div.content div div.cmd_in, .boring div.prompt div.input::before{ line-height: 2em; }"];o=o.join("\n"),e('<style id="ptty-boring-theme">'+o+"</style>").appendTo("head")}var a=[i+"{ position: relative; display: block; overflow-X: hidden; height: 100%; }","div.content div p{ margin: 0; }","div.content div{ clear: both; white-space:pre-wrap; word-wrap:break-word; }","div.content div ul{ padding: 0; white-space: normal }","div.content div ul li{ list-style: none; }","div.content div ul.sq-li li{ display: inline-block; text-align: center; padding: 10px; min-width: 5%; }","div.prompt div.input{ width: 100%; white-space:pre-wrap; word-wrap:break-word; cursor: default; outline: none;}","div.prompt div.input::before{ vertical-align: middle; content: attr(data-ps); }","div.prompt div.input::after{ visibility : visible; vertical-align: middle; content: attr(data-caret); margin-left:-0.15em;}","div.prompt div.input.blink::after{ visibility : hidden; }","div.prompt .hide{ position:absolute; top: -9999em; }","div.loading{ display: none; }","div.loading.working{ display: block; display:flex; justify-content: center; align-items: center;position: fixed; width: inherit; height: inherit; }","div.loading span{ -webkit-animation: spin 4s linear infinite; -moz-animation: spin 4s linear infinite; -ms-animation: spin 4s linear infinite; -o-animation: spin 4s linear infinite; animation: spin 4s linear infinite; }"];a=a.join("\n "+i+" "),e('<style id="ptty-styles">'+(a+="@-moz-keyframes spin { 100% { -moz-transform: rotate(360deg); } }@-webkit-keyframes spin { 100% { -webkit-transform: rotate(360deg); } }@-ms-keyframes spin { 100% { -ms-transform: rotate(360deg); } }@-o-keyframes spin { 100% { -o-transform: rotate(360deg); } }@keyframes spin { 100% { transform: rotate(360deg); } }")+"</style>").appendTo("head")},n.native_commands=function(){n.register("command",{name:"clear",method:function(e){return e.last="",e.out="",e},options:[],help:"Cleans the screen leaving a new command prompt ready."}),n.register("callback",{name:"clear",method:function(e){return i.find(".content").html(""),e}}),n.register("command",{name:"history",method:function(e){if(e.hasOwnProperty("clear"))s=[],e.out="History cleared.";else if(s.length>0){e.out="<ul>";for(var t=0;t<s.length;t+=1)e.out+="<li>"+s[t]+"</li>";e.out+="</ul>"}return e},options:["clear"],help:"Shows list of typed in commands. Type <i>history clear</i> to clear your history."}),n.register("command",{name:"help",method:function(e){var t=Object.keys(o);if("string"==typeof e[1]&&e[1].length>0)if(e.hasOwnProperty("-a")||e.hasOwnProperty("--all")){e.out="<b>Available commands:</b></br></br><ul>";for(var n=0;n<t.length;n+=1)e.out+="<li><p><b>"+n+"</b> - ",e.out+=o[t[n]].help+"</p></br></li>";e.out+="</ul>\n"}else void 0!==o[e[1]]?(e.out="<b>"+e[1]+"</b> - ",""!==o[e[1]].help?e.out+=o[e[1]].help+"\n":e.out+="No help entry available.\n"):e.out='help: The "'+e[1]+'" option does not exist.\n';else{e.out='Use "help [comand name]" to display specific info about a command.</br>\n',e.out+='Available commands are:</br><ul class="sq-li">';for(n=0;n<t.length;n+=1)e.out+="<li>"+t[n]+"</li>";e.out+="</ul>\n"}return e},options:[1,"-a","--all"],help:"Displays a list of useful information. Usage: <i>help command-name</i> to show <i>command-name</i>'s help.<i>help -a</i> or <i>help --all</i> to display all help."})},n.native_responses=function(e){for(var t in e)e.hasOwnProperty(t)&&n.register("response",{name:t,method:function(n){return e[t]=n[t],n}})},n.run_command=function(e,t){w=t,x(e)},n.echo=function(e,t){e&&i.find(".content").append('<div><div class="cmd_out">'+e+"</div></div>"),t||C()},n.change_settings=function(t){e.extend(!0,c,t)},n.unregister=function(e,t){var n=!1;return"object"==typeof t&&t.hasOwnProperty("name")&&(t=t.name),"callbefore"==e&&l.hasOwnProperty(t)?(n=!0,delete l[t]):"command"==e&&o.hasOwnProperty(t)?(n=!0,delete o[t]):"response"==e&&a.hasOwnProperty(t)?(n=!0,delete a[t]):"callback"==e&&r.hasOwnProperty(t)&&(n=!0,delete r[t]),n},n.register=function(e,t){var n=!1;if(t){var i=!!t.hasOwnProperty("name")&&t.name,s=!!t.hasOwnProperty("method")&&t.method,d=t.hasOwnProperty("options")?t.options:[],c=t.hasOwnProperty("help")?t.help:"";"callbefore"==e&&"function"==typeof s?(l[i]=s,n=!0):"command"!=e||"string"!=typeof s&&"function"!=typeof s?"response"==e&&"function"==typeof s?(a[i]=s,n=!0):"callback"==e&&"function"==typeof s&&(r[i]=s,n=!0):(o[i]={help:c,options:d,exe:s},n=!0)}return n},n.set_command_option=function(t){return e.extend(!0,d,t)},n.get_command_option=function(e){var t;if("string"==typeof e)t=!!d.hasOwnProperty(e)&&d[e];else if("object"==typeof e){t={};for(var n=e.length-1;n>=0;n--)void 0!==d[e[n]]&&(t[e[n]]=d[e[n]])}else t=d;return t},n.tokenize=function(t,n){var i={},o=e.trim(t).split(/\s+/);if(void 0===o[0]||""===o[0])i=!1;else if(void 0!==n){var a=!1,r=!1,l=!1,s=!1,d=!1,c=!1,p=!1,u=n.filter(function(e){if("number"==typeof e&&e>0&&void 0!==o[e])return i[e]=o[e],e});n=e(n).not(u).get();for(var m=0;m<o.length;m++)d=o[m].charAt(0),c=o[m].slice(-1),p=o[m].charAt(o[m].length-2),e.inArray(o[m],n)>=0?(a=o[m],r=!1):'"'==d&&!1===s&&'"'!==c?(l='"',s=!0,r=o[m]):"'"==d&&!1===s&&"'"!==c?(l="'",s=!0,r=o[m]):c==l&&!0===s&&p+c!=="\\"+l?(s=!1,r+=" "+o[m],r=e.trim(r.substring(1).slice(0,-1).replace(/\\(.)/gm,"$1"))):!0===s?r+=" "+o[m]:r="'"==d&&"'"==c||'"'==d&&'"'==c?e.trim(o[m].substring(1).slice(0,-1)):o[m],a&&!1===s&&(i[a]=r)}else i[o[0]]=o;return i},i.html("");var p=null,u=null,m={};i.append('<div class="loading"><span></span></div><div class="content"><div>'+c.i18n.welcome+'</div></div><div class="prompt"><div class="input" contenteditable spellcheck="false" data-caret="'+c.caret+'" data-ps="'+c.ps+'"></div></div>');var f=i.find(".prompt .input"),v=i.find(".content"),h=i.find(".loading");i.attr("data-theme",c.theme).addClass(c.theme),c.native_css&&n.native_style(i,c.theme);var g=c.autofocus,b=c.autocomplete,y=c.history_max;c.autofocus&&f.focus(),i.bind("focus click",function(){var e="";void 0!==window.getSelection?e=window.getSelection().toString():void 0!==document.selection&&"Text"==document.selection.type&&(e=document.selection.createRange().text),""==e&&j()}),f.click(function(){j()}),f.bind("blur",function(){g=!1}),c.caret_blink>0&&setInterval(function(){c.caret_blink>0&&!0===g&&f.toggleClass("blink")},c.caret_blink),c.native_cmds&&n.native_commands(),n.native_responses(d);var w=null,x=function(t){var i;if(h.addClass("working"),i=void 0!==t?t:f.text(),b=c.autocomplete,y=c.history_max,d.last=i,"string"==typeof d.next&&(i=d.next.replace(/%cmd%/i,i),d.next=null,y=0),!i||""==i)return k();if(u=i.split(/\s+/)[0],void 0===o[u])return w||(d.out=u+" : "+c.i18n.error_not_found),k();if(m=n.tokenize(i,o[u].options),"function"==typeof c.before_cmd&&!(m=_(c.before_cmd(m))))return k();if("function"==typeof l[u]&&!(m=_(l[u](m))))return k();if(w||P(d.last),"function"==typeof o[u].exe)return _(o[u].exe(m)),k();if("string"!=typeof o[u].exe)return d.out=c.i18n.error_bad_method,k();var a={};if(!c.ajax_options.data){var r={};r[c.param]=null!==d.in?d.in:u,r[c.param+"_data"]=null!==d.data?d.data:m,a.data=r}var s=e.extend(!0,a,c.ajax_options);o[u].exe&&(s.url=o[u].exe);var p=e.ajax(s);p.done(function(e){m=_(e)}),p.fail(function(e,t,n){d.out=c.i18n.error_ajax}),p.always(function(e,t,n){return k()})},_=function(t){if("object"==typeof t)for(var n in t)a.hasOwnProperty(n)&&e.extend(!0,d,a[n](t));return t},k=function(){b=c.autocomplete,y=c.history_max;var e=d.ps?d.ps:c.ps,t=d.out?d.out:"",n=d.in?d.in:"",o=d.last?d.last:"",a=d.next?d.next:null;w?v.append('<div><div class="cmd_out">'+t+"</div></div>"):v.append('<div><div class="cmd_in"><span class="cmd_ps">'+f.attr("data-ps")+"</span>"+o+'</div><div class="cmd_out">'+t+"</div></div>"),m&&(r.hasOwnProperty(u)&&_(r[u](m)),"function"==typeof c.after_cmd&&_(c.after_cmd(m))),f.attr("data-caret",c.caret).attr("data-ps",e).text(n),0===c.caret_blink&&f.removeClass("blink"),f.hasClass("show-caret")&&f.removeClass("show-caret"),i.hasClass(c.theme)||i.removeClass(i.attr("data-theme")).addClass(c.theme).attr("data-theme",c.theme),a&&(b=!1,y=0),w=null,m=d={ps:null,in:null,out:null,last:null,next:a,data:null},O()},O=function(){C(),j(),h.removeClass("working")},P=function(t){void 0!==o[u]&&""!==t&&y>0&&(s.length>c.history_entries&&s.shift(),s.push(e.trim(t))),p=0},C=function(){i.scrollTop(i.height()+1e17)},j=function(){if(f.focus(),g=!0,void 0!==window.getSelection&&void 0!==document.createRange){var e=document.createRange();e.selectNodeContents(f.get(0)),e.collapse(!1);var t=window.getSelection();t.removeAllRanges(),t.addRange(e)}else if(void 0!==document.body.createTextRange){var n=document.body.createTextRange();n.moveToElementText(f.get(0)),n.collapse(!1),n.select()}};f.keydown(function(t){switch(t.keyCode){case 9:t.preventDefault(),b&&(!function(e){var t=[];if(e.match(/^[^\s]{0,}$/)){for(var n in o)""==e?t.push(n):0==n.indexOf(e)&&t.push(n);t.length>1?(d.out="<ul><li>"+t.join("</li><li>")+"</li></ul>",d.in=e,k()):1==t.length&&f.text(t.pop()+" ")}}(e.trim(f.text())),O());break;case 37:case 39:c.caret_blink>0&&(g=!1,f.addClass("blink show-caret"));break;case 38:t.preventDefault(),y>0&&(p=null===p||0==p?s.length-1:p-1,f.text(s[p]),O());break;case 40:if(t.preventDefault(),y>0){if(null===p||p==s.length-1){f.html("");break}p++,f.text(s[p]),O()}break;case 46:case 8:1!==f.text().length&&window.getSelection().toString()!=f.text()||f.html("");break;case 13:t.preventDefault(),document.execCommand("insertHTML",!1,""),x();break;case 27:d={ps:null,in:null,out:null,last:null,next:null,data:null},f.text(""),x()}})}),n}}(jQuery);