pgsh.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. function pgsh(ev) {
  2. var self = this
  3. /**
  4. * Terminal UI/Shell Entry Point
  5. */
  6. ev.on('cmd_entered', function(input) {
  7. var args = input.trim().split(' ')
  8. // Use the first argument as a command, or the value of `active`.
  9. var command = self.active ? self.active : args.shift()
  10. if (command in self.commands) {
  11. self[command](args)
  12. } else if (command) {
  13. show(command + ' not found')
  14. }
  15. })
  16. /**
  17. * Key functions
  18. */
  19. ev.on('mount', function() {
  20. document.getElementById(ev.id).onkeydown = function(e) {
  21. var code = e.keyCode || e.which
  22. // Tab
  23. if (code == 9) {
  24. var input = ev.tags['command-line'].command.value.trim().split(' ')
  25. var command = input.shift()
  26. if (['cat', 'cd', 'ls'].indexOf(command) != -1) {
  27. var args = input.join(' ').trim()
  28. var isFile = (command == 'cat') ? true : false;
  29. var path = completePath(args, isFile)
  30. if (path) { ev.trigger('cmd_set', cmd + ' ' + path) }
  31. }
  32. e.preventDefault()
  33. }
  34. }
  35. })
  36. /**
  37. * Variables and Setup
  38. */
  39. var getHome = function() { return self.su_active ? '/root' : '/home/pgs' }
  40. this.cwd = getHome()
  41. var getPrompt = function() {
  42. if (self.su_active) {
  43. return '' +
  44. '<span style="color:#dc322f">root </span>' +
  45. '<span style="color:#b58900">' + self.cwd + ' </span>' +
  46. '<span style="color:#268bd2">% </span>'
  47. }
  48. return '' +
  49. '<span style="color:#d33682">pgs </span>' +
  50. '<span style="color:#b58900">' + self.cwd + ' </span>' +
  51. '<span style="color:#2aa198">$ </span>'
  52. }
  53. this.prompt = getPrompt()
  54. this.welcome = '' +
  55. 'Linux parsleygardens.net 3.4.5-6-7-i286 #8 PGS Vimputer' +
  56. '3.4.56-7 i286\n\n' +
  57. '~ Welcome to Parsley Gardens! ~\n' +
  58. '"He maketh me to lie down in green pa..."\n\n' +
  59. 'Type `<strong>help</strong>` for list of commands\n\n'
  60. this.tree = {
  61. 'bin': {},
  62. 'etc': {},
  63. 'home': {
  64. 'pgs': {
  65. 'git.txt':
  66. 'Code Repository at <a target="_blank"' +
  67. 'href="https://code.parsleygardens.net/explore/repos">' +
  68. 'code.parsleygardens.net</a>',
  69. 'magic.txt':
  70. 'Animation & Illustration at <a target="_blank"' +
  71. 'href="http://slightlymagic.com.au">' +
  72. 'slightlymagic.com.au</a>',
  73. 'about.md':
  74. '# Parsley Gardens Website\n\n' +
  75. '1. Made by Weiyi Lou ' + new Date().getFullYear() + '\n' +
  76. '2. Done for fun.\n' +
  77. '3. Not a real terminal.',
  78. }
  79. },
  80. 'mnt': {},
  81. 'root': {
  82. 'bin': {
  83. 'destroy.sh':
  84. "#!/usr/bin/env bash\n" +
  85. "set -euo pipefail\n" +
  86. "IFS=$'\\n\\t'\n\n" +
  87. "rm -rf /"
  88. },
  89. 'everyday_lost_item_locations_global.sqlite': '',
  90. 'government-secrets.txt': ''
  91. },
  92. 'usr': {
  93. 'local': {
  94. 'bin': {
  95. 'nothing.txt': 'Really nothing'
  96. }
  97. }
  98. },
  99. 'var': {
  100. 'cache': {},
  101. 'log': {},
  102. 'run': {}
  103. }
  104. }
  105. this.commands = {
  106. 'about': { order: 1, help: 'Author Information' },
  107. 'cat': { order: 2, help: 'Con(cat)enate the contents of a file' },
  108. 'cd': { order: 2, help: 'Change folder/directory' },
  109. 'clear': { order: 2, help: 'Clears the screen' },
  110. 'exit': { order: 2, help: 'Leave the current context' },
  111. 'hello': { order: 2, help: 'Say hello to the computer\nUsage: hello [name]' },
  112. 'help': { order: 1, help: 'List commands or view information for one\nUsage: help [command]' },
  113. 'ls': { order: 2, help: 'List folder contents' },
  114. 'magic': { order: 1, help: 'The Website of an Artist' },
  115. 'pwd': { order: 2, help: 'Show the current working directory' },
  116. 'question': { order: 1, help: 'Displays a question' },
  117. 'search': { order: 2, help: 'Search the Web (with a Duck)\nUsage: search [query]' },
  118. 'su': { order: 2, help: 'Gain Phenomenal Cosmic Power' },
  119. 'version': { order: 1, help: 'Display shell information' }
  120. }
  121. /**
  122. * Commands
  123. */
  124. this.about = function(args) { self.cat(['/home/pgs/about.md']) }
  125. this.magic = function(args) { self.cat(['/home/pgs/magic.txt']) }
  126. this.cat = function(args) {
  127. var arg = args.join(' ').trim()
  128. var path = resolveAbsPath(arg)
  129. if (pathExists(path, true)) {
  130. var content = getContents(path)
  131. if (path.search('.md|.sh') != -1) {
  132. content = hljs.highlightAuto(content).value
  133. }
  134. show('\n' + content + '\n\n')
  135. } else {
  136. show("cd: " + arg + ": no such file")
  137. }
  138. }
  139. this.cd = function(args) {
  140. var arg = args.join(' ').trim()
  141. arg = arg ? arg : getHome()
  142. var path = resolveAbsPath(arg)
  143. if (pathExists(path)) {
  144. self.cwd = path
  145. ev.trigger('prompt_set', getPrompt())
  146. } else {
  147. show("cd: " + arg + ": no such directory")
  148. }
  149. }
  150. this.clear = function(args) { ev.trigger('disp_clear') }
  151. this.exit = function(args) {
  152. if (self.su_active) {
  153. self.su_active = false
  154. ev.trigger('prompt_set', getPrompt())
  155. return
  156. }
  157. show('Close browser window to exit')
  158. }
  159. this.hello = function(args) {
  160. address = args.join(' ').trim()
  161. if (address.length == 0) {
  162. show('Hello to you too')
  163. } else if (address == 'pgsh') {
  164. show('Hello human')
  165. } else {
  166. show('My name is not "' + address + '"')
  167. }
  168. }
  169. this.help = function(args) {
  170. command = args.join(' ').trim()
  171. if (command in self.commands) {
  172. show(self.commands[command].help)
  173. } else if (command) {
  174. show('No information for: ' + command)
  175. } else {
  176. var main = []
  177. var other = []
  178. for (var name in self.commands) {
  179. if (self.commands[name].order == 1) {
  180. main.push(name)
  181. } else {
  182. other.push(name)
  183. }
  184. }
  185. show('\nAvailable commands:\n\n' +
  186. ' ' + main.sort().join(' ') + '\n\n' +
  187. ' ' + other.sort().join(' ') + '\n\n' +
  188. '`help [command]` for more information.')
  189. }
  190. }
  191. this.ls = function(args) {
  192. var arg = args.join(' ').trim()
  193. var folder = arg || self.cwd
  194. var path = resolveAbsPath(folder)
  195. if (pathExists(path)) {
  196. var contents = getContents(path)
  197. var items = []
  198. for (var item in contents) {
  199. items.push(item)
  200. }
  201. items.sort()
  202. var dstat = 'drwxr-xr-x <span style="color:#268bd2">'
  203. var fstat = '-rw-r--r-- '
  204. items.forEach(function(item, key, items) {
  205. var prefix = (typeof contents[item] === 'string') ? fstat : dstat
  206. var suffix = (typeof contents[item] === 'string') ? '' : '</span>'
  207. items[key] = prefix + item + suffix
  208. })
  209. items.unshift(dstat + '..</span>')
  210. items.unshift('total ' + items.length)
  211. show(items.join('\n'))
  212. } else {
  213. show("ls: " + args + ": no such directory")
  214. }
  215. }
  216. this.pwd = function(args) { show(self.cwd) }
  217. this.question = function(args) {
  218. var questions = [
  219. 'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
  220. ' target="_blank">this song</a> the best?',
  221. '1 + 2 = ?',
  222. 'Am I a sandwich?',
  223. 'Where were you at 3:15am on April 14th?',
  224. "Don't you mean prism?",
  225. 'Butts twelve by pies?',
  226. 'I say there, Monstrosity. Do you know the times?',
  227. 'What is the name of the spaces between the teeth of a comb?',
  228. 'Why are you dressed up like Ship\'s Captain?'
  229. ]
  230. var rand = Math.floor(Math.random() * questions.length);
  231. show(questions[rand])
  232. }
  233. this.search = function(args) {
  234. if (!args.join(' ').trim()) { return show(self.commands.search.help) }
  235. ev.trigger('prompt_hide')
  236. show('Searching for "' + args.join(' ') + '" in new window...')
  237. setTimeout(function() {
  238. window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
  239. ev.trigger('prompt_show')
  240. }, 1000)
  241. }
  242. this.su = function(args) {
  243. if (self.su_active !== true) {
  244. self.su_active = true
  245. if (!self.su_warned) {
  246. self.su_warned = true
  247. show('\n\n' +
  248. 'We trust you have received the usual lecture from the ' +
  249. 'local System Administrator.\n' +
  250. 'It usually boils down to these three things:\n\n' +
  251. ' #1) Respect the privacy of others.\n' +
  252. ' #2) Think before you type.\n' +
  253. ' #3) With great power comes great\n\n')
  254. }
  255. ev.trigger('prompt_set', getPrompt())
  256. }
  257. }
  258. this.version = function(args) {
  259. show('Parsley Gardens Shell (pgsh) 1.1.0 Built with ' +
  260. '<a target="_blank" href="http://riotjs.com">Riot</a> ' +
  261. '<a target="_blank" href="https://skeleton-framework.github.io">Skeleton</a> ' +
  262. '<a target="_blank" href="https://highlightjs.org">highlight.js</a>')
  263. }
  264. /**
  265. * Helper Functions
  266. */
  267. var show = function(text) { ev.trigger('disp_add', text) }
  268. var set = function(text) { ev.trigger('disp_set', text) }
  269. // Calculate a destination's path with respect to the current folder.
  270. var resolveAbsPath = function(dest) {
  271. var parts = dest.trim().split('/')
  272. var absolute = dest.startsWith('/') || self.cwd == '/'
  273. var path = absolute ? [''] : self.cwd.split('/')
  274. parts.forEach(function(part) {
  275. if (part == '..' && path.length > 1) { path.pop() }
  276. if (part != '..' && part) { path.push(part) }
  277. })
  278. return path.length > 1 ? path.join('/') : '/'
  279. }
  280. // Check if a path exists in the tree
  281. var pathExists = function(path, isFile) {
  282. var isFile = (typeof isFile !== 'undefined') ? isFile : false; // Old way, for Safari.
  283. var parts = path.trim().split('/')
  284. var fname = isFile ? parts.pop() : ''
  285. var tree = self.tree
  286. for (var i = 0; i < parts.length; i++) {
  287. var part = parts[i]
  288. if (!part) {
  289. continue
  290. }
  291. if (!(tree.hasOwnProperty(part) && typeof tree[part] === 'object')) {
  292. return false
  293. }
  294. tree = tree[part]
  295. }
  296. if (isFile && !(tree.hasOwnProperty(fname) && typeof tree[fname] === 'string')) {
  297. return false
  298. }
  299. return true
  300. }
  301. // Return the contents of a given path.
  302. var getContents = function(path) {
  303. var parts = path.trim().split('/')
  304. var tree = self.tree
  305. parts.forEach(function(part) {
  306. if (part) { tree = tree[part] }
  307. })
  308. return tree
  309. }
  310. var completePath = function(path, isFile) {
  311. // check if we are doing partial name completion
  312. // ending slash means completing first content
  313. // not ending slash means word completion - cut off bit for completion
  314. // first get right directory with absolute path
  315. console.log('completing')
  316. return ''
  317. }
  318. }