help.js (3104B)
1 /** 2 * The "help" command 3 * @param {window.FakeTerminal} instance The instance of FakeTerminal 4 * @return {Object} 5 */ 6 window.FakeTerminal.command.help = function (instance) { 7 8 // Extend the base command 9 window.FakeTerminal.command.apply(this, arguments); 10 11 /** 12 * Avoid scope issues by using `base` instead of `this` 13 * @type {Object} 14 */ 15 const base = this; 16 17 // -------------------------------------------------------------------------- 18 19 /** 20 * Describes the command 21 * @return {Object} 22 */ 23 base.info = function () { 24 return { 25 description: 'Displays information about the available commands' 26 }; 27 }; 28 29 // -------------------------------------------------------------------------- 30 31 /** 32 * Executes the command 33 * @return {Object} A promise which will be resolved when the command completes 34 */ 35 base.execute = function () { 36 37 let returnVal = []; 38 let commandInfo = {}; 39 40 if (arguments.length === 0) { 41 42 returnVal.push('The following commands are available, run <info>help [command]</info> to find out more.'); 43 returnVal.push(' '); 44 45 let commandString = ''; 46 $.each(window.FakeTerminal.command, function (command) { 47 48 const temp = instance.findCommand(command); 49 if (!temp) { 50 return; 51 } 52 53 // Check to see if the command is private 54 if (typeof temp.info === 'function') { 55 commandInfo = temp.info(); 56 if (typeof commandInfo.private === 'boolean' && commandInfo.private === true) { 57 return; 58 } 59 } 60 61 commandString += command + ' '; 62 }); 63 64 returnVal.push(commandString); 65 returnVal.push(' '); 66 67 } else { 68 69 const command = instance.findCommand(arguments[0]); 70 if (command) { 71 72 if (typeof command.info === 'function') { 73 74 commandInfo = command.info(); 75 76 if (typeof commandInfo.description === 'string') { 77 returnVal = [' ', arguments[0] + ' -- <comment>' + commandInfo.description + '</comment>', ' ']; 78 } else if (typeof commandInfo.description === 'object') { 79 returnVal = commandInfo.description; 80 } 81 } 82 83 if (returnVal.length === 0) { 84 returnVal = [' ', 'No description for "' + command + '"', ' ']; 85 } 86 87 } else { 88 returnVal = [' ', '"' + command + '" is not a valid command', ' ']; 89 } 90 } 91 92 // Write to the terminal 93 for (let i = 0; i < returnVal.length; i++) { 94 instance.output.write(returnVal[i]); 95 } 96 97 base.deferred.resolve(); 98 return base.deferred.promise(); 99 }; 100 101 // -------------------------------------------------------------------------- 102 103 return base; 104 };