Advertisement
Guest User

xkcd terminal

a guest
Apr 1st, 2010
909
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(a){a.browserTest=function(e,g) {
  2.     var f = "unknown";
  3.     var d = "X";
  4.     var b = function(k,j) {
  5.         for (var c = 0; c < j.length; c=c+1) {
  6.             k = k.replace(j[c][0], j[c][1])
  7.         }
  8.         return k};
  9.     var h = function(l, k, j, n) {
  10.         var m = {
  11.             name:b((k.exec(l) || [f,f])[1], j)
  12.         };
  13.         m[m.name] = true;
  14.         m.version = (n.exec(l)||[d,d,d,d])[3];
  15.         if (m.name.match(/safari/)&&m.version>400) {
  16.             m.version="2.0"
  17.         }
  18.         if(m.name==="presto") {
  19.             m.version=(a.browser.version>9.27) ? "futhark" : "linear_b";
  20.         }
  21.         m.versionNumber = parseFloat(m.version, 10) || 0;
  22.         m.versionX = (m.version !== d) ? (m.version + "").substr(0,1) : d;
  23.         m.className = m.name + m.versionX;
  24.         return m;
  25.     }
  26.     var e = (e.match(/Opera|Navigator|Minefield|KHTML|Chrome/) ? b(e,[[/(Firefox|MSIE|KHTML,\slike\sGecko|Konqueror)/,""],["Chrome
  27. Safari","Chrome"],["KHTML","Konqueror"],["Minefield","Firefox"],["Navigator","Netscape"]]):e).toLowerCase();a.browser=a.extend((!g)?a.browser:{},h(e,/(camino|chrome|firefox|netscape|konqueror|lynx|msie|opera|safari)/,[],/(camino|chrome|firefox|netscape|netscape6|opera|version|konqueror|lynx|msie|safari)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/));a.layout=h(e,/(gecko|konqueror|msie|opera|webkit)/,[["konqueror","khtml"],["msie","trident"],["opera","presto"]],/(applewebkit|rv|konqueror|msie)(\:|\/|\s)([a-z0-9\.]*?)(\;|\)|\s)/);a.os={name:(/(win|mac|linux|sunos|solaris|iphone)/.exec(navigator.platform.toLowerCase())||[f])[0].replace("sunos","solaris")};if(!g){a("html").addClass([a.os.name,a.browser.name,a.browser.className,a.layout.name,a.layout.className].join("
  28. "))}};a.browserTest(navigator.userAgent)})(jQuery);(function(b){b.hotkeys={version:"0.8",specialKeys:{8:"backspace",9:"tab",13:"return",16:"shift",17:"ctrl",18:"alt",19:"pause",20:"capslock",27:"esc",32:"space",33:"pageup",34:"pagedown",35:"end",36:"home",37:"left",38:"up",39:"right",40:"down",45:"insert",46:"del",96:"0",97:"1",98:"2",99:"3",100:"4",101:"5",102:"6",103:"7",104:"8",105:"9",106:"*",107:"+",109:"-",110:".",111:"/",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12",144:"numlock",145:"scroll",191:"/",224:"meta"},shiftNums:{"`":"~","1":"!","2":"@","3":"#","4":"$","5":"%","6":"^","7":"&","8":"*","9":"(","0":")","-":"_","=":"+",";":":
  29. ","'":'"',",":"<",".":">","/":"?","\\":"|"}};function a(d){if(typeof
  30. d.data!=="string"){return}var c=d.handler,e=d.data.toLowerCase().split("
  31. ");d.handler=function(n){if(this!==n.target&&(/textarea|select/i.test(n.target.nodeName)||n.target.type==="text")){return}var
  32. h=n.type!=="keypress"&&b.hotkeys.specialKeys[n.which],o=String.fromCharCode(n.which).toLowerCase(),k,m="",g={};if(n.altKey&&h!=="alt"){m+="alt+"}if(n.ctrlKey&&h!=="ctrl"){m+="ctrl+"}if(n.metaKey&&!n.ctrlKey&&h!=="meta"){m+="meta+"}if(n.shiftKey&&h!=="shift"){m+="shift+"}if(h){g[m+h]=true}else{g[m+o]=true;g[m+b.hotkeys.shiftNums[o]]=true;if(m==="shift+"){g[b.hotkeys.shiftNums[o]]=true}}for(var
  33. j=0,f=e.length;j<f;j++){if(g[e[j]]){return
  34. c.apply(this,arguments)}}}}b.each(["keydown","keyup","keypress"],function(){b.event.special[this]={add:a}})})(jQuery);function
  35. ltrim(b){if(b){var a=/\s*((\S+\s*)*)/;return b.replace(a,"$1")}return""}function
  36. rtrim(b){if(b){var a=/((\s*\S+)*)\s*/;return b.replace(a,"$1")}return""}function
  37. trim(a){if(a){return ltrim(rtrim(a))}return""}function
  38. entityEncode(a){a=a.replace(/&/g,"&amp;");a=a.replace(/</g,"&lt;");a=a.replace(/>/g,"&gt;");a=a.replace(/
  39. /g," &nbsp;");if(/msie/i.test(navigator.userAgent)){a=a.replace("\n","&nbsp;<br
  40. />")}else{a=a.replace(/\x0D/g,"&nbsp;<br />")}return a}var
  41. TerminalShell={commands:{help:function
  42. help(a){a.print($("<h3>help</h3>"));cmd_list=$("<ul>");$.each(this.commands,function(b,c){cmd_list.append($("<li>").text(b))});a.print(cmd_list)},clear:function(a){a.clear()}},fallback:null,process:function(a,b){var
  43. d=b.split(" ");var c=d.shift();d.unshift(a);if(c in
  44. this.commands){this.commands[c].apply(this,d)}else{if(!(this.fallback&&this.fallback(a,b))){a.print('Unrecognized
  45. command. Type "help" for assistance.')}}}};var
  46. Terminal={buffer:"",pos:0,history:[],historyPos:0,promptActive:true,cursorBlinkState:true,_cursorBlinkTimeout:null,spinnerIndex:0,_spinnerTimeout:null,output:TerminalShell,config:{scrollStep:20,scrollSpeed:100,bg_color:"#000",fg_color:"#FFF",cursor_blink_time:700,cursor_style:"block",prompt:"guest@xkcd:/$
  47. ",spinnerCharacters:["[ ]","[. ]","[..
  48. ]","[...]"],spinnerSpeed:250,typingSpeed:50},sticky:{keys:{ctrl:false,alt:false,scroll:false},set:function(a,b){this.keys[a]=b;$("#"+a+"-indicator").toggle(this.keys[a])},toggle:function(a){this.set(a,!this.keys[a])},reset:function(a){this.set(a,false)},resetAll:function(a){$.each(this.keys,$.proxy(function(b,c){this.reset(b)},this))}},init:function(){$(document).keypress($.proxy(function(c){if(!this.promptActive){return}if(c.which>=32&&c.which<=126){var
  49. b=String.fromCharCode(c.which);var
  50. a=b.toLowerCase()}else{return}if($.browser.opera&&!(/[\w\s]/.test(b))){return}if(this.sticky.keys.ctrl){if(a=="w"){this.deleteWord()}else{if(a=="h"){Terminal.deleteCharacter(false)}else{if(a=="l"){this.clear()}else{if(a=="a"){this.setPos(0)}else{if(a=="e"){this.setPos(this.buffer.length)}}}}}}else{if(b){this.addCharacter(b);c.preventDefault()}}},this)).bind("keydown","return",function(a){if(Terminal.promptActive){Terminal.processInputBuffer()}}).bind("keydown","backspace",function(a){a.preventDefault();Terminal.deleteCharacter(a.shiftKey)}).bind("keydown","del",function(a){Terminal.deleteCharacter(true)}).bind("keydown","left",function(a){Terminal.moveCursor(-1)}).bind("keydown","right",function(a){Terminal.moveCursor(1)}).bind("keydown","up",function(a){a.preventDefault();if(a.shiftKey||Terminal.sticky.keys.scroll){Terminal.scrollLine(-1)}else{if(a.ctrlKey||Terminal.sticky.keys.ctrl){Terminal.scrollPage(-1)}else{Terminal.moveHistory(-1)}}}).bind("keydown","down",function(a){a.preventDefault();if(a.shiftKey||Terminal.sticky.keys.scroll){Terminal.scrollLine(1)}else{if(a.ctrlKey||Terminal.sticky.keys.ctrl){Terminal.scrollPage(1)}else{Terminal.moveHistory(1)}}}).bind("keydown","pageup",function(a){Terminal.scrollPage(-1)}).bind("keydown","pagedown",function(a){Terminal.scrollPage(1)}).bind("keydown","home",function(a){a.preventDefault();if(a.ctrlKey||Terminal.sticky.keys.ctrl){Terminal.jumpToTop()}else{Terminal.setPos(0)}}).bind("keydown","end",function(a){a.preventDefault();if(a.ctrlKey||Terminal.sticky.keys.ctrl){Terminal.jumpToBottom()}else{Terminal.setPos(Terminal.buffer.length)}}).bind("keydown","tab",function(a){a.preventDefault()}).keyup(function(b){var
  51. a=$.hotkeys.specialKeys[b.which];if(a in
  52. {ctrl:true,alt:true,scroll:true}){Terminal.sticky.toggle(a)}else{if(!(a in
  53. {left:true,right:true,up:true,down:true})){Terminal.sticky.resetAll()}}});$(window).resize(function(a){$("#screen").scrollTop($("#screen").attr("scrollHeight"))});this.setCursorState(true);this.setWorking(false);$("#prompt").html(this.config.prompt);$("#screen").hide().fadeIn("fast",function(){$("#screen").triggerHandler("cli-load")})},setCursorState:function(b,a){this.cursorBlinkState=b;if(this.config.cursor_style=="block"){if(b){$("#cursor").css({color:this.config.bg_color,backgroundColor:this.config.fg_color})}else{$("#cursor").css({color:this.config.fg_color,backgroundColor:this.config.bg_color})}}else{if(b){$("#cursor").css("textDecoration","underline")}else{$("#cursor").css("textDecoration","none")}}if(!a&&this._cursorBlinkTimeout){window.clearTimeout(this._cursorBlinkTimeout);this._cursorBlinkTimeout=null}this._cursorBlinkTimeout=window.setTimeout($.proxy(function(){this.setCursorState(!this.cursorBlinkState,true)},this),this.config.cursor_blink_time)},updateInputDisplay:function(){var
  54. c="",b="
  55. ",a="";if(this.pos<0){this.pos=0}if(this.pos>this.buffer.length){this.pos=this.buffer.length}if(this.pos>0){c=this.buffer.substr(0,this.pos)}if(this.pos<this.buffer.length){b=this.buffer.substr(this.pos,1)}if(this.buffer.length-this.pos>1){a=this.buffer.substr(this.pos+1,this.buffer.length-this.pos-1)}$("#lcommand").text(c);$("#cursor").text(b);if(b=="
  56. "){$("#cursor").html("&nbsp;")}$("#rcommand").text(a);$("#prompt").text(this.config.prompt);return},clearInputBuffer:function(){this.buffer="";this.pos=0;this.updateInputDisplay()},clear:function(){$("#display").html("")},addCharacter:function(b){var
  57. c=this.buffer.substr(0,this.pos);var
  58. a=this.buffer.substr(this.pos,this.buffer.length-this.pos);this.buffer=c+b+a;this.pos++;this.updateInputDisplay();this.setCursorState(true)},deleteCharacter:function(a){var
  59. d=a?1:0;if(this.pos>=(1-d)){var c=this.buffer.substr(0,this.pos-1+d);var
  60. b=this.buffer.substr(this.pos+d,this.buffer.length-this.pos-d);this.buffer=c+b;this.pos-=1-d;this.updateInputDisplay()}this.setCursorState(true)},deleteWord:function(){if(this.pos>0){var
  61. a=this.pos;while(a>0&&this.buffer.charAt(a)!=="
  62. "){a--}left=this.buffer.substr(0,a-1);right=this.buffer.substr(a,this.buffer.length-this.pos);this.buffer=left+right;this.pos=a;this.updateInputDisplay()}this.setCursorState(true)},moveCursor:function(a){this.setPos(this.pos+a)},setPos:function(a){if((a>=0)&&(a<=this.buffer.length)){this.pos=a;Terminal.updateInputDisplay()}this.setCursorState(true)},moveHistory:function(b){var
  63. a=this.historyPos+b;if((a>=0)&&(a<=this.history.length)){if(a==this.history.length){this.clearInputBuffer()}else{this.buffer=this.history[a]}this.pos=this.buffer.length;this.historyPos=a;this.updateInputDisplay();this.jumpToBottom()}this.setCursorState(true)},addHistory:function(a){this.historyPos=this.history.push(a)},jumpToBottom:function(){$("#screen").animate({scrollTop:$("#screen").attr("scrollHeight")},this.config.scrollSpeed,"linear")},jumpToTop:function(){$("#screen").animate({scrollTop:0},this.config.scrollSpeed,"linear")},scrollPage:function(a){$("#screen").animate({scrollTop:$("#screen").scrollTop()+a*$("#screen").height()},this.config.scrollSpeed,"linear")},scrollLine:function(a){$("#screen").scrollTop($("#screen").scrollTop()+a*this.config.scrollStep)},print:function(a){if(!a){$("#display").append($("<div>"))}else{if(typeof
  64. a=="string"){$("#display").append($("<p>").text(a))}else{$("#display").append(a)}}this.jumpToBottom()},processInputBuffer:function(a){this.print($("<p>").addClass("command").text(this.config.prompt+this.buffer));var
  65. a=trim(this.buffer);this.clearInputBuffer();if(a.length==0){return
  66. false}this.addHistory(a);if(this.output){return
  67. this.output.process(this,a)}else{return
  68. false}},setPromptActive:function(a){this.promptActive=a;$("#inputline").toggle(this.promptActive)},setWorking:function(a){if(a&&!this._spinnerTimeout){$("#display
  69. .command:last-child").add("#bottomline").first().append($("#spinner"));this._spinnerTimeout=window.setInterval($.proxy(function(){if(!$("#spinner").is(":visible")){$("#spinner").fadeIn()}this.spinnerIndex=(this.spinnerIndex+1)%this.config.spinnerCharacters.length;$("#spinner").text(this.config.spinnerCharacters[this.spinnerIndex])},this),this.config.spinnerSpeed);this.setPromptActive(false);$("#screen").triggerHandler("cli-busy")}else{if(!a&&this._spinnerTimeout){clearInterval(this._spinnerTimeout);this._spinnerTimeout=null;$("#spinner").fadeOut();this.setPromptActive(true);$("#screen").triggerHandler("cli-ready")}}},runCommand:function(e){var
  70. b=0;var d=false;this.promptActive=false;var
  71. a=window.setInterval($.proxy(function
  72. c(){if(b<e.length){this.addCharacter(e.charAt(b));b+=1}else{clearInterval(a);this.promptActive=true;this.processInputBuffer()}},this),this.config.typingSpeed)}};$(document).ready(function(){$("#welcome").show();document.onkeydown=document.onkeypress=function(a){return
  73. $.hotkeys.specialKeys[a.keyCode]!="backspace"};Terminal.init()});function
  74. pathFilename(a){match=/\/([^\/]+)$/.exec(a);if(match){return match[1]}}function
  75. getRandomInt(b,a){return Math.floor(Math.random()*(a-b+1))+b}function
  76. randomChoice(a){return a[getRandomInt(0,a.length-1)]}var
  77. xkcd={latest:null,last:null,cache:{},proxy:"",get:function(b,c,a){if(b==null){path="/info.0.json"}else{if(Number(b)){path="/"+b+"/info.0.json"}else{a(false);return
  78. false}}if(b in this.cache){this.last=this.cache[b];c(this.cache[b])}else{return
  79. $.ajax({url:this.proxy+path,dataType:"json",success:$.proxy(function(d){this.last=this.cache[b]=d;c(d)},this),
  80.                 error:a
  81.             })
  82.         }
  83.     }
  84. };
  85.  
  86. var xkcdDisplay = TerminalShell.commands.display = function(b,c) {
  87.     function a() {
  88.         b.print($("<p>").addClass("error").text('display: unable to open image "'+c+'": No such file or directory.'));
  89.         b.setWorking(false)
  90.     }
  91.     b.setWorking(true);
  92.     if (c ) {
  93.         c=String(c);
  94.         num=Number(c.match(/^\d+/));
  95.         filename=pathFilename(c)
  96.     } else {
  97.         num=xkcd.last.num
  98.     }
  99.     xkcd.get(num,function(d) {
  100.         if (!filename ||( filename==pathFilename(d.img))) {
  101.             $("<img>").hide().load(function() {
  102.                 b.print($("<h3>").text(d.num+": "+d.title));
  103.                 $(this).fadeIn();
  104.                 var e=$(this);
  105.                 if(d.link) {
  106.                     e=$("<a>").attr("href",d.link).append($(this))
  107.                 }
  108.                 b.print(e);
  109.                 b.setWorking(false)
  110.             }).attr({
  111.                 src:d.img,
  112.                 alt:d.title,
  113.                 title:d.alt
  114.             }).addClass("comic")
  115.         } else {
  116.             a()
  117.         }
  118.     } ,a)
  119. };
  120.  
  121. TerminalShell.commands.next = function(a) {
  122.     xkcdDisplay(a,xkcd.last.num+1)
  123. };
  124.  
  125. TerminalShell.commands.previous = TerminalShell.commands.prev = function(a) {
  126.     xkcdDisplay(a,xkcd.last.num-1)
  127. };
  128.  
  129. TerminalShell.commands.first = function(a) {
  130.     xkcdDisplay(a,1)
  131. };
  132.  
  133. TerminalShell.commands.latest = TerminalShell.commands.last = function(a) {
  134.     xkcdDisplay(a,xkcd.latest.num)
  135. };
  136.  
  137. TerminalShell.commands.random = function(a) {
  138.     xkcdDisplay(a,getRandomInt(1,xkcd.latest.num))
  139. };
  140.  
  141. TerminalShell.commands["goto"] = function(a,b) {
  142.     $("#screen").one("cli-ready",function(c) {
  143.         a.print('Did you mean "display"?')
  144.     });
  145.     xkcdDisplay(a,292)
  146. };
  147.  
  148. function linkFile(a) {
  149.     return {
  150.         type:"dir",
  151.         enter:function()
  152.             window.location=a
  153.         }
  154.     }
  155. }
  156.  
  157. Filesystem = {
  158.     "welcome.txt":{
  159.         type:"file",
  160.         read: function(a) {
  161.             a.print($("<h4>").text("Welcome to the unixkcd console."));
  162.             a.print('To navigate, enter "next", "prev", "first", or "last".')
  163.         }
  164.     },
  165.     "license.txt":{
  166.         type:"file",
  167.         read:function(a) {
  168.             a.print($("<p>").html('Client-side logic for Wordpress CLI theme :: <a href="http://thrind.xamai.ca/">R. McFarland, 2006, 2007, 2008</a>'));
  169.             a.print($("<p>").html('jQuery rewrite and overhaul :: <a href="http://www.chromakode.com/">Chromakode, 2010</a>'));
  170.             a.print();
  171.             $.each([
  172.                 "This program is free software; you can redistribute it and/or",
  173.                 "modify it under the terms of the GNU General Public License",
  174.                 "as published by the Free Software Foundation; either version 2",
  175.                 "of the License, or (at your option) any later version.",
  176.                 "",
  177.                 "This program is distributed in the hope that it will be useful,",
  178.                 "but WITHOUT ANY WARRANTY; without even the implied warranty of",
  179.                 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
  180.                 "GNU General Public License for more details.",
  181.                 "",
  182.                 "You should have received a copy of the GNU General Public License",
  183.                 "along with this program; if not, write to the Free Software",
  184.                 "Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA."
  185.             ], function(c,b) {
  186.                 a.print(b)
  187.             })
  188.         }
  189.     }
  190. };
  191.  
  192. Filesystem.blog = Filesystem.blag = linkFile("http://blag.xkcd.com");
  193.  
  194. Filesystem.forums = Filesystem.fora = linkFile("http://forums.xkcd.com/");
  195.  
  196. Filesystem.store = linkFile("http://store.xkcd.com/");
  197.  
  198. Filesystem.about = linkFile("http://xkcd.com/about/");
  199.  
  200. TerminalShell.pwd=Filesystem;
  201.  
  202. TerminalShell.commands.cd=function(a,b) {
  203.     if(b in this.pwd) {
  204.         if(this.pwd[b].type == "dir") {
  205.             this.pwd[b].enter(a)
  206.         } else {
  207.             if (this.pwd[b].type=="file") {
  208.                 a.print("cd: " + b + ": Not a directory")
  209.             }
  210.         }
  211.     } else {
  212.         a.print("cd: " + b + ": No such file or directory")
  213.     }
  214. };
  215.  
  216. TerminalShell.commands.ls=function(a,b) {
  217.     name_list=$("<ul>");
  218.     $.each(this.pwd, function(c,d) {
  219.         if(d.type=="dir") {
  220.             c+="/"
  221.         }
  222.         name_list.append($("<li>").text(c))
  223.     });
  224.     a.print(name_list)
  225. };
  226.  
  227. TerminalShell.commands.cat=function(a,b) {
  228.     if (b in this.pwd) {
  229.         if (this.pwd[b].type == "file") {
  230.             this.pwd[b].read(a)
  231.         } else {
  232.             if (this.pwd[b].type == "dir") {
  233.                 a.print("cat: " + b + ": Is a directory");
  234.             }
  235.         }
  236.     } else {
  237.         if (pathFilename(b) == "alt.txt") {
  238.             a.setWorking(true);
  239.             num = Number(b.match(/^\d+/));
  240.             xkcd.get(num, function(c) {
  241.                 a.print(c.alt);
  242.                 a.setWorking(false)
  243.             }, function() {
  244.                 a.print($("<p>").addClass("error").text('cat: "' + b + '": No such file or directory.'));
  245.                 a.setWorking(false)
  246.             })
  247.         } else {
  248.             a.print("You're a kitty!")
  249.         }
  250.     }
  251. };
  252.  
  253. TerminalShell.commands.reddit = function(c,b) {
  254.     b=Number(b);
  255.     if (b ){
  256.         a="http://xkcd.com/" + b + "/"
  257.     } else {
  258.         var a=window.location
  259.     } c.print($('<iframe src="http://www.reddit.com/static/button/button1.html?width=140&url='+encodeURIComponent(a)+'&newwindow=1" height="22" width="140" scrolling="no" frameborder="0"></iframe>'))
  260. };
  261.  
  262. TerminalShell.commands["apt-get"] = function(c, d, a) {
  263.     if ((a !="sudo") && (d in {update:true, upgrade:true, "dist-upgrade":true})) {
  264.         c.print("E: Unable to lock the administration directory, are you root?");
  265.     } else {
  266.         if (d=="update") {
  267.             c.print("Reading package lists... Done")
  268.         } else {
  269.             if (d=="upgrade") {
  270.                 if (($.browser.name=="msie") || ($.browser.name=="firefox" || $.browser.versionX < 3)) {
  271.                     c.print($("<p>").append($("<a>").attr("href","http://abetterbrowser.org/").text("To complete installation, click here.")))
  272.                 } else {
  273.                     c.print("This looks pretty good to me.")
  274.                 }
  275.             } else {
  276.                 if (d == "dist-upgrade") {
  277.                     var e= {win:"Windows", mac:"OS X", linux:"Linux"};
  278.                     var b=$.os.name;
  279.                     if(b in e) {
  280.                         b=e[b];
  281.                     } else {
  282.                         b = "something fancy";
  283.                     }
  284.                     c.print("You are already running " + b + ".")
  285.                 } else {
  286.                     if(d=="moo") {
  287.                         c.print("(__)");
  288.                         c.print(" (oo)");
  289.                         c.print(" /------\\/ ");
  290.                         c.print(" / | || ");
  291.                         c.print("*/\\---/\\ ");
  292.                         c.print(" ~~ ~~ ");
  293.                         c.print('...."Have you mooed today?"...')
  294.                     } else {
  295.                         if (!d) {
  296.                             c.print("This APT has Super Cow Powers.")
  297.                         } else {
  298.                             c.print("E: Invalid operation "+d);
  299.                         }
  300.                     }
  301.                 }
  302.             }
  303.         }
  304.     }
  305. };
  306.  
  307. TerminalShell.commands.sudo = function(a) {
  308.     var c = Array.prototype.slice.call(arguments);
  309.     c.shift();
  310.     if(c.join(" ") == "make me a sandwich") {
  311.         a.print("Okay.")
  312.     } else {
  313.         var b = c.shift();
  314.         c.unshift(a);
  315.         c.push("sudo");
  316.         if (b in TerminalShell.commands) {
  317.             this.commands[b].apply(this, c)
  318.         } else {
  319.             a.print("sudo: " + b + ": command not found");
  320.         }
  321.     }
  322. };
  323.  
  324. function oneLiner(a,c,b) {
  325.     if(c in b) {
  326.         a.print(b[c]);
  327.         return true;
  328.     } else {
  329.         return false;
  330.     }
  331. }
  332.  
  333. TerminalShell.commands.man = function(a, b) {
  334.     pages={
  335.         last:"Man, last night was AWESOME.",
  336.         help:"Man, help me out here.",
  337.         next:"Request confirmed; you will be reincarnated as a man next.",
  338.         cat:"You are now riding a half-man half-cat."};
  339.         if (!oneLiner(a, b, pages)) {
  340.             a.print("Oh, I'm sure you can figure it out.")
  341.         }
  342.     };
  343.     Adventure= {
  344.         rooms:{
  345.         0:{description:"You are at a computer using unixkcd.",exits:{west:1,south:10}},
  346.         1:{description:"Life is peaceful there.",exits:{east:0,west:2}},
  347.         2:{description:"In the open air.",exits:{east:1,west:3}},
  348.         3:{description:"Where the skies are blue.",exits:{east:2,west:4}},
  349.         4:{description:"This is what we're gonna do.",exits:{east:3,west:5}},
  350.         5:{description:"Sun in wintertime.",exits:{east:4,west:6}},
  351.         6:{description:"We will do just fine.",exits:{east:5,west:7}},
  352.         7:{description:"Where the skies are blue.",exits:{east:6,west:8}},
  353.         8:{description:"This is what we're gonna do.",exits:{east:7}},
  354.         10:{description:"A dark hallway.",exits:{north:0,south:11},
  355.         enter: function(a) {
  356.             if (!Adventure.status.lamp) {
  357.                 a.print("You are eaten by a grue.");
  358.                 Adventure.status.alive = false;
  359.                 Adventure.goTo(a, 666);}
  360.                 }
  361.             },
  362.         11:{description:"Bed. This is where you sleep.",exits:{north:10}},
  363.         666:{description:"You're dead!"}
  364.     },
  365.     status:{
  366.         alive:true,
  367.         lamp:false
  368.     },
  369.     goTo:function(a,b) {
  370.         Adventure.location = Adventure.rooms[b];
  371.         Adventure.look(a);
  372.         if (Adventure.location.enter) {
  373.             Adventure.location.enter(a)
  374.         }
  375.     }
  376. };
  377.  
  378. Adventure.location = Adventure.rooms[0];
  379.  
  380. TerminalShell.commands.look = Adventure.look = function(b) {
  381.     b.print(Adventure.location.description);
  382.     if (Adventure.location.exits) {
  383.         b.print();
  384.         var a=[];
  385.         $.each(Adventure.location.exits, function(c,d) {
  386.             a.push(c)
  387.         });
  388.         b.print("Exits: " + a.join(", "))
  389.     }
  390. };
  391.  
  392. TerminalShell.commands.go = Adventure.go = function(a,b) {
  393.     if (Adventure.location.exits && b in Adventure.location.exits){
  394.         Adventure.goTo(a,Adventure.location.exits[b])
  395.     } else {
  396.         if (!b) {
  397.             a.print("Go where?");
  398.         } else {
  399.             a.print("You cannot go " + b + ".");
  400.         }
  401.     }
  402. };
  403.  
  404. TerminalShell.commands.light = function(a,b) {
  405.     if (b == "lamp") {
  406.         if (!Adventure.status.lamp) {
  407.             a.print("You set your lamp ablaze.");
  408.             Adventure.status.lamp = true;
  409.         } else {
  410.             a.print("Your lamp is already lit!")
  411.         }
  412.     } else {
  413.         a.print("Light what?")
  414.     }
  415. };
  416.  
  417. TerminalShell.commands.help = TerminalShell.commands.halp =  function(a) {
  418.     a.print("That would be cheating!")
  419. };
  420.  
  421. TerminalShell.fallback = function(a,b) {
  422.     oneliners = {
  423.         "make me a sandwich":"What? Make it yourself.",
  424.         "i read the source code":"<3",
  425.         pwd:"You are in a maze of twisty passages, all alike.",
  426.         lpr:"PC LOAD LETTER",
  427.         "hello joshua":"How about a nice game of Global Thermonuclear War?",
  428.         xyzzy:"Nothing happens.",
  429.         date:"March 32nd"
  430.     };
  431.     oneliners.emacs="You should really use vim.";
  432.     oneliners.vi=oneliners.vim="You should really use emacs.";
  433.     b=b.toLowerCase();
  434.     if(!oneLiner(a,b,oneliners)) {
  435.         if(b=="asl" || b=="a/s/l") {
  436.             a.print(randomChoice([
  437.                 "2/AMD64/Server Rack",
  438.                 "328/M/Transylvania",
  439.                 "6/M/Battle School",
  440.                 "48/M/The White House",
  441.                 "7/F/Rapture",
  442.                 "Exactly your age/A gender you're attracted to/Far far away.",
  443.                 "7,831/F/Lothlórien",
  444.                 "42/M/FBI Field Office"
  445.             ]))
  446.         } else {
  447.             if (b=="find kitten") {
  448.                 a.print($('<iframe width="800" height="600" src="http://www.robotfindskitten.net/rfk.swf"></iframe>'))
  449.             } else {
  450.                 if (b=="buy stuff"){
  451.                     Filesystem.store.enter()
  452.                 } else {
  453.                     $.get("/unixkcd/missing", {cmd:b});
  454.                     return false
  455.                 }
  456.             }
  457.         }
  458.     }
  459.     return true
  460. };
  461.  
  462. $(document).ready(function() {
  463.     Terminal.promptActive = false;
  464.     $("#screen").bind("cli-load", function(a) {
  465.         xkcd.get(null,function(b) {
  466.             xkcd.latest=b;
  467.             $("#screen").one("cli-ready", function(c) {
  468.                 Terminal.runCommand("cat welcome.txt")
  469.             });
  470.             Terminal.runCommand("display " + xkcd.latest.num + "/" + pathFilename(xkcd.latest.img))
  471.         }, function(b) {
  472.             Terminal.print($("<p>").addClass("error").text("Unable to load startup data. :-("));
  473.             Terminal.promptActive=true
  474.         })
  475.     })
  476. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement