output.js (2255B)
1 /** 2 * The output service 3 * @return {Object} 4 */ 5 window.FakeTerminal.output = function (instance) { 6 7 /** 8 * Avoid scope issues by using `base` instead of `this` 9 * @type {Object} 10 */ 11 var base = this; 12 13 // -------------------------------------------------------------------------- 14 15 /** 16 * The screen element 17 * @type {Object} 18 */ 19 base.$screen = null; 20 21 // -------------------------------------------------------------------------- 22 23 /** 24 * Constructs window.FakeTerminal.output 25 * @returns {Object} 26 * @private 27 */ 28 base.__construct = function () { 29 base.$screen = $('<div>').addClass('faketerminal__screen'); 30 instance.$el.append(base.$screen); 31 return base; 32 }; 33 34 // -------------------------------------------------------------------------- 35 36 /** 37 * Writes a line to base.$screen 38 * @param {String} line The line to write 39 * @param {Boolean} prompt Whether to include the prompt element 40 * @returns {Object} A reference to the class, for chaining 41 */ 42 base.write = function (line, prompt) { 43 44 var $line = $('<div>').addClass('faketerminal__screen__line'); 45 46 if (prompt) { 47 $line.append( 48 $('<div>') 49 .addClass('faketerminal__prompt') 50 .html(instance.getPrompt()) 51 ); 52 } 53 54 // Colorize and encode spaces so white space is maintained 55 line = instance.colorize(line); 56 line = line.replace(/ /g, ' ', line); 57 line = line.replace(/<span class="/g, '<span class="', line); 58 line = line.replace(/<div class="/g, '<div class="', line); 59 60 $line.append(line); 61 base.$screen.append($line); 62 instance.scrollToBottom(); 63 return base; 64 }; 65 66 // -------------------------------------------------------------------------- 67 68 /** 69 * Clears all items from base.$screens 70 * @returns {Object} A reference to the class, for chaining 71 */ 72 base.clear = function () { 73 base.$screen.empty(); 74 return base; 75 }; 76 77 // -------------------------------------------------------------------------- 78 79 return base.__construct(); 80 };