pgsh.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.',
  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("cat: " + 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 dstat = 'drwxr-xr-x <span style="color:#268bd2">'
  198. var fstat = '-rw-r--r-- '
  199. var items = []
  200. for (var item in contents) {
  201. var prefix = (typeof contents[item] === 'string') ? fstat : dstat
  202. var suffix = (typeof contents[item] === 'string') ? '' : '</span>'
  203. items.push(prefix + item + suffix)
  204. }
  205. items.unshift(dstat + '..</span>')
  206. items.unshift('total ' + items.length)
  207. show(items.join('\n'))
  208. } else {
  209. show("ls: " + args + ": no such directory")
  210. }
  211. }
  212. this.pwd = function(args) { show(self.cwd) }
  213. this.question = function(args) {
  214. var questions = [
  215. 'Isn\'t <a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"' +
  216. ' target="_blank">this song</a> the best?',
  217. '1 + 2 = ?',
  218. 'Am I a sandwich?',
  219. 'Where were you at 3:15am on April 14th?',
  220. "Don't you mean prism?",
  221. 'Butts twelve by pies?',
  222. 'I say there, Monstrosity. Do you know the times?',
  223. 'What is the name of the spaces between the teeth of a comb?',
  224. 'Why are you dressed up like Ship\'s Captain?'
  225. ]
  226. var rand = Math.floor(Math.random() * questions.length);
  227. show(questions[rand])
  228. }
  229. this.search = function(args) {
  230. if (!args.join(' ').trim()) { return show(self.commands.search.help) }
  231. ev.trigger('prompt_hide')
  232. show('Searching for "' + args.join(' ') + '" in new window...')
  233. setTimeout(function() {
  234. window.open('https://duckduckgo.com/?q=' + args.join('+'), '_blank')
  235. ev.trigger('prompt_show')
  236. }, 1000)
  237. }
  238. this.su = function(args) {
  239. if (self.su_active !== true) {
  240. self.su_active = true
  241. if (!self.su_warned) {
  242. self.su_warned = true
  243. show('\n\n' +
  244. 'We trust you have received the usual lecture from the ' +
  245. 'local System Administrator.\n' +
  246. 'It usually boils down to these three things:\n\n' +
  247. ' #1) Respect the privacy of others.\n' +
  248. ' #2) Think before you type.\n' +
  249. ' #3) With great power comes great\n\n')
  250. }
  251. ev.trigger('prompt_set', getPrompt())
  252. }
  253. }
  254. this.version = function(args) {
  255. show('Parsley Gardens Shell (pgsh) 1.1.0\nBuilt with: ' +
  256. '<a target="_blank" href="http://riotjs.com">Riot</a> ' +
  257. '<a target="_blank" href="https://skeleton-framework.github.io">Skeleton</a> ' +
  258. '<a target="_blank" href="https://highlightjs.org">highlight.js</a>')
  259. }
  260. /**
  261. * Helper Functions
  262. */
  263. var show = function(text) { ev.trigger('disp_add', text) }
  264. var set = function(text) { ev.trigger('disp_set', text) }
  265. // Calculate a destination's path with respect to the current folder.
  266. var resolveAbsPath = function(dest) {
  267. var parts = dest.trim().split('/')
  268. var absolute = dest.startsWith('/') || self.cwd == '/'
  269. var path = absolute ? [''] : self.cwd.split('/')
  270. parts.forEach(function(part) {
  271. if (part == '..' && path.length > 1) { path.pop() }
  272. if (part != '..' && part) { path.push(part) }
  273. })
  274. return path.length > 1 ? path.join('/') : '/'
  275. }
  276. // Check if a path exists in the tree
  277. var pathExists = function(path, isFile) {
  278. var isFile = (typeof isFile !== 'undefined') ? isFile : false; // Old way, for Safari.
  279. var parts = path.trim().split('/')
  280. var fname = isFile ? parts.pop() : ''
  281. var tree = self.tree
  282. for (var i = 0; i < parts.length; i++) {
  283. var part = parts[i]
  284. if (!part) {
  285. continue
  286. }
  287. if (!(tree.hasOwnProperty(part) && typeof tree[part] === 'object')) {
  288. return false
  289. }
  290. tree = tree[part]
  291. }
  292. if (isFile && !(tree.hasOwnProperty(fname) && typeof tree[fname] === 'string')) {
  293. return false
  294. }
  295. return true
  296. }
  297. // Return the contents of a given path.
  298. var getContents = function(path) {
  299. var parts = path.trim().split('/')
  300. var tree = self.tree
  301. parts.forEach(function(part) {
  302. if (part) { tree = tree[part] }
  303. })
  304. // Sort JS object. TODO not a great way.
  305. if (typeof tree === 'object') {
  306. var keys = []
  307. for (key in tree) {
  308. keys.push(key)
  309. }
  310. keys.sort()
  311. var temp = {}
  312. keys.forEach(function(key) {
  313. temp[key] = tree[key]
  314. })
  315. tree = temp
  316. }
  317. return tree
  318. }
  319. var completePath = function(input, isFile) {
  320. // Extract file fragment and get actual path.
  321. var slash = input.lastIndexOf('/') + 1
  322. var frag = input.substring(slash)
  323. var base = input.substring(0, slash)
  324. var path = resolveAbsPath(base)
  325. // Complete fragments
  326. if (pathExists(path)) {
  327. var contents = getContents(path)
  328. var found = ''
  329. for (var name in contents) {
  330. if (!isFile && typeof contents[name] === 'string') { continue }
  331. if (!frag) { found = name; break }
  332. if (name.indexOf(frag) == 0) { found = name }
  333. }
  334. var end = typeof contents[found] === 'object' ? '/' : ''
  335. return base + found + end
  336. }
  337. return ''
  338. }
  339. }