pgsh.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 for file path completion.
  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', command + ' ' + 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.\n' +
  78. '4. But <a target="_blank" href="https://hyperterm.org/">this</a>' +
  79. ' totally is.'
  80. }
  81. },
  82. 'mnt': {},
  83. 'root': {
  84. 'bin': {
  85. 'destroy.sh':
  86. "#!/usr/bin/env bash\n" +
  87. "set -euo pipefail\n" +
  88. "IFS=$'\\n\\t'\n\n" +
  89. "rm -rf /"
  90. },
  91. 'everyday_lost_item_locations_global.sqlite': '',
  92. 'government-secrets.txt': ''
  93. },
  94. 'usr': {
  95. 'local': {
  96. 'bin': {
  97. 'nothing.txt': 'Really nothing'
  98. }
  99. }
  100. },
  101. 'var': {
  102. 'cache': {},
  103. 'log': {},
  104. 'run': {}
  105. }
  106. }
  107. this.commands = {
  108. 'about': { order: 1, help: 'Author Information' },
  109. 'cat': { order: 2, help: 'Con(cat)enate the contents of a file' },
  110. 'cd': { order: 2, help: 'Change folder/directory' },
  111. 'clear': { order: 2, help: 'Clears the screen' },
  112. 'exit': { order: 2, help: 'Leave the current context' },
  113. 'hello': { order: 2, help: 'Say hello to the computer\nUsage: hello [name]' },
  114. 'help': { order: 1, help: 'List commands or view information for one\nUsage: help [command]' },
  115. 'ls': { order: 2, help: 'List folder contents' },
  116. 'magic': { order: 1, help: 'The Website of an Artist' },
  117. 'pwd': { order: 2, help: 'Show the current working directory' },
  118. 'question': { order: 1, help: 'Displays a question' },
  119. 'search': { order: 2, help: 'Search the Web (with a Duck)\nUsage: search [query]' },
  120. 'su': { order: 2, help: 'Gain Phenomenal Cosmic Power' },
  121. 'version': { order: 1, help: 'Display shell information' }
  122. }
  123. /**
  124. * Commands
  125. */
  126. this.about = function(args) { self.cat(['/home/pgs/about.md']) }
  127. this.magic = function(args) { self.cat(['/home/pgs/magic.txt']) }
  128. this.cat = function(args) {
  129. var arg = args.join(' ').trim()
  130. var path = resolveAbsPath(arg)
  131. if (pathExists(path, true)) {
  132. var content = getContents(path)
  133. if (path.search('.md|.sh') != -1) {
  134. content = hljs.highlightAuto(content).value
  135. }
  136. show('\n' + content + '\n\n')
  137. } else {
  138. show("cat: " + arg + ": no such file")
  139. }
  140. }
  141. this.cd = function(args) {
  142. var arg = args.join(' ').trim()
  143. arg = arg ? arg : getHome()
  144. var path = resolveAbsPath(arg)
  145. if (pathExists(path)) {
  146. self.cwd = path
  147. ev.trigger('prompt_set', getPrompt())
  148. } else {
  149. show("cd: " + arg + ": no such directory")
  150. }
  151. }
  152. this.clear = function(args) { ev.trigger('disp_clear') }
  153. this.exit = function(args) {
  154. if (self.su_active) {
  155. self.su_active = false
  156. ev.trigger('prompt_set', getPrompt())
  157. return
  158. }
  159. show('Close browser window to exit')
  160. }
  161. this.hello = function(args) {
  162. address = args.join(' ').trim()
  163. if (address.length == 0) {
  164. show('Hello to you too')
  165. } else if (address == 'pgsh') {
  166. show('Hello human')
  167. } else {
  168. show('My name is not "' + address + '"')
  169. }
  170. }
  171. this.help = function(args) {
  172. command = args.join(' ').trim()
  173. if (command in self.commands) {
  174. show(self.commands[command].help)
  175. } else if (command) {
  176. show('No information for: ' + command)
  177. } else {
  178. var main = []
  179. var other = []
  180. for (var name in self.commands) {
  181. if (self.commands[name].order == 1) {
  182. main.push(name)
  183. } else {
  184. other.push(name)
  185. }
  186. }
  187. show('\nAvailable commands:\n\n' +
  188. ' ' + main.sort().join(' ') + '\n\n' +
  189. ' ' + other.sort().join(' ') + '\n\n' +
  190. '`help [command]` for more information.')
  191. }
  192. }
  193. this.ls = function(args) {
  194. var arg = args.join(' ').trim()
  195. var folder = arg || self.cwd
  196. var path = resolveAbsPath(folder)
  197. if (pathExists(path)) {
  198. var contents = getContents(path)
  199. var dstat = 'drwxr-xr-x <span style="color:#268bd2">'
  200. var fstat = '-rw-r--r-- '
  201. var items = []
  202. for (var item in contents) {
  203. var prefix = (typeof contents[item] === 'string') ? fstat : dstat
  204. var suffix = (typeof contents[item] === 'string') ? '' : '</span>'
  205. items.push(prefix + item + suffix)
  206. }
  207. items.unshift(dstat + '..</span>')
  208. items.unshift('total ' + items.length)
  209. show(items.join('\n'))
  210. } else {
  211. show("ls: " + args + ": no such directory")
  212. }
  213. }
  214. this.pwd = function(args) { show(self.cwd) }
  215. this.question = function(args) {
  216. var questions = [
  217. 'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
  218. ' target="_blank">this song</a> the best?',
  219. '1 + 2 = ?',
  220. 'Am I a sandwich?',
  221. 'Where were you at 3:15am on April 14th?',
  222. "Don't you mean prism?",
  223. 'Butts twelve by pies?',
  224. 'I say there, Monstrosity. Do you know the times?',
  225. 'What is the name of the spaces between the teeth of a comb?',
  226. 'Why are you dressed up like Ship\'s Captain?'
  227. ]
  228. var rand = Math.floor(Math.random() * questions.length);
  229. show(questions[rand])
  230. }
  231. this.search = function(args) {
  232. if (!args.join(' ').trim()) { return show(self.commands.search.help) }
  233. ev.trigger('prompt_hide')
  234. show('Searching for "' + args.join(' ') + '" in new window...')
  235. setTimeout(function() {
  236. window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
  237. ev.trigger('prompt_show')
  238. }, 1000)
  239. }
  240. this.su = function(args) {
  241. if (self.su_active !== true) {
  242. self.su_active = true
  243. if (!self.su_warned) {
  244. self.su_warned = true
  245. show('\n\n' +
  246. 'We trust you have received the usual lecture from the ' +
  247. 'local System Administrator.\n' +
  248. 'It usually boils down to these three things:\n\n' +
  249. ' #1) Respect the privacy of others.\n' +
  250. ' #2) Think before you type.\n' +
  251. ' #3) With great power comes great\n\n')
  252. }
  253. ev.trigger('prompt_set', getPrompt())
  254. }
  255. }
  256. this.version = function(args) {
  257. show('Parsley Gardens Shell (pgsh) 1.1.0\nBuilt with: ' +
  258. '<a target="_blank" href="http://riotjs.com">Riot</a> ' +
  259. '<a target="_blank" href="https://skeleton-framework.github.io">Skeleton</a> ' +
  260. '<a target="_blank" href="https://highlightjs.org">highlight.js</a>')
  261. }
  262. /**
  263. * Helper Functions
  264. */
  265. var show = function(text) { ev.trigger('disp_add', text) }
  266. var set = function(text) { ev.trigger('disp_set', text) }
  267. // Calculate a destination's path with respect to the current folder.
  268. var resolveAbsPath = function(dest) {
  269. var parts = dest.trim().split('/')
  270. var absolute = dest.substring(0, 1) == '/' || self.cwd == '/'
  271. var path = absolute ? [''] : self.cwd.split('/')
  272. parts.forEach(function(part) {
  273. if (part == '..' && path.length > 1) { path.pop() }
  274. if (part != '..' && part) { path.push(part) }
  275. })
  276. return (path.length > 1) ? path.join('/') : '/'
  277. }
  278. // Check if a path exists in the tree.
  279. var pathExists = function(path, isFile) {
  280. // Old way of defining default values, for Safari.
  281. var isFile = (typeof isFile !== 'undefined') ? isFile : false;
  282. var parts = path.trim().split('/')
  283. var fname = isFile ? parts.pop() : ''
  284. var tree = self.tree
  285. for (var i = 0; i < parts.length; i++) {
  286. var part = parts[i]
  287. if (!part) {
  288. continue
  289. }
  290. if (!(tree.hasOwnProperty(part) && typeof tree[part] === 'object')) {
  291. return false
  292. }
  293. tree = tree[part]
  294. }
  295. if (isFile && !(tree.hasOwnProperty(fname) && typeof tree[fname] === 'string')) {
  296. return false
  297. }
  298. return true
  299. }
  300. // Return the contents of a given path.
  301. var getContents = function(path) {
  302. var parts = path.trim().split('/')
  303. var tree = self.tree
  304. parts.forEach(function(part) {
  305. if (part) { tree = tree[part] }
  306. })
  307. // Sort JS object. Not a great way, but more compatible.
  308. if (typeof tree === 'object') {
  309. var keys = []
  310. for (key in tree) {
  311. keys.push(key)
  312. }
  313. keys.sort()
  314. var temp = {}
  315. keys.forEach(function(key) {
  316. temp[key] = tree[key]
  317. })
  318. tree = temp
  319. }
  320. return tree
  321. }
  322. // Complete a given file path string based on the current tree.
  323. var completePath = function(input, isFile) {
  324. // Extract file fragment and get actual path.
  325. var slash = input.lastIndexOf('/') + 1
  326. var frag = input.substring(slash)
  327. var base = input.substring(0, slash)
  328. var path = resolveAbsPath(base)
  329. // Complete fragments.
  330. if (pathExists(path)) {
  331. var contents = getContents(path)
  332. var found = ''
  333. for (var name in contents) {
  334. if (!isFile && typeof contents[name] === 'string') { continue }
  335. if (!frag) { found = name; break }
  336. if (name.indexOf(frag) == 0) { found = name; break }
  337. }
  338. var end = (typeof contents[found] === 'object') ? '/' : ''
  339. return base + found + end
  340. }
  341. return ''
  342. }
  343. }