1
+ const input = document . getElementById ( 'input' ) ;
2
+ const history = document . getElementById ( 'history' ) ;
3
+ const files = { "readme.txt" :"test readme file..." , "about.txt" :"My name is Travis Pooley, I'm a software engineer from Adelaide, Australia." } ;
4
+
5
+ // add codepen
6
+ const directories = { "FlindersFinder" :"https://travispooley.com/VEED2201/" , "LinkedIn" :"https://www.linkedin.com/in/travispooley/" , "Twitter" :"https://twitter.com/TravisPooley" } ;
7
+
8
+ let commandHistory = [ ] ;
9
+ let historyNumber = - 1 ;
10
+
11
+ // auto focus on input
12
+ function focus ( ) {
13
+ document . getElementById ( "input" ) . focus ( )
14
+ }
15
+
16
+ function handleCommand ( command ) {
17
+ const line = document . createElement ( 'p' ) ;
18
+ console . log ( command )
19
+ // sanitise input
20
+ const commandText = document . createElement ( 'span' ) ;
21
+ commandText . innerText = command ;
22
+ line . innerHTML = '<span style="color: #7fff7f;">[email protected] </span>:<span style="color: #3391ff;">~</span>$' ;
23
+ line . appendChild ( commandText ) ;
24
+ history . appendChild ( line ) ;
25
+
26
+ // if command is not blank add to the history of commands
27
+ if ( command != "" ) {
28
+ commandHistory . push ( command ) ;
29
+ }
30
+
31
+ if ( command == "clear" ) {
32
+ history . innerHTML = "" ;
33
+ }
34
+ else if ( command == "ls" ) {
35
+ let ls = document . createElement ( 'p' ) ;
36
+ for ( directory in directories ) {
37
+ let directoryName = document . createElement ( 'span' ) ;
38
+ directoryName . setAttribute ( 'class' , "lsDisplay" )
39
+ directoryName . innerText = directory ;
40
+ ls . appendChild ( directoryName ) ;
41
+ }
42
+ for ( file in files ) {
43
+ let fileName = document . createElement ( 'span' ) ;
44
+ fileName . setAttribute ( 'class' , "lsDisplay" )
45
+ fileName . style . color = "white" ;
46
+ fileName . innerText = file ;
47
+ ls . appendChild ( fileName ) ;
48
+ }
49
+ history . appendChild ( ls ) ;
50
+ }
51
+ // man command
52
+
53
+ else if ( command == "reboot" ) {
54
+ location . reload ( ) ;
55
+ }
56
+ // reboot
57
+ else if ( command . split ( " " ) [ 0 ] == "cd" ) {
58
+ if ( Object . keys ( directories ) . includes ( command . split ( " " ) [ 1 ] ) ) {
59
+ window . open ( directories [ command . split ( " " ) [ 1 ] ] , '_blank' ) . focus ( ) ;
60
+ let redirect = document . createElement ( 'p' ) ;
61
+ redirect . innerHTML = "attempting to open: <a href='" + ( directories [ command . split ( " " ) [ 1 ] ] ) + "'>" + ( directories [ command . split ( " " ) [ 1 ] ] ) + "<a>" ;
62
+ history . appendChild ( redirect ) ;
63
+ }
64
+ }
65
+ else if ( command . split ( " " ) [ 0 ] == "cat" ) {
66
+ if ( Object . keys ( files ) . includes ( command . split ( " " ) [ 1 ] ) ) {
67
+ let cat = document . createElement ( 'p' ) ;
68
+ cat . innerHTML = files [ command . split ( " " ) [ 1 ] ] ;
69
+ history . appendChild ( cat ) ;
70
+ }
71
+ else {
72
+ let unknownFile = document . createElement ( 'p' ) ;
73
+ unknownFile . innerText = command . split ( " " ) [ 0 ] + ": " + command . split ( " " ) [ 1 ] + ": No such file or directory" ;
74
+ history . appendChild ( unknownFile ) ;
75
+ }
76
+ }
77
+ else if ( command != "" ) {
78
+ const unknownCommand = document . createElement ( 'p' ) ;
79
+ unknownCommand . innerText = command + ": command not found" ;
80
+ history . appendChild ( unknownCommand ) ;
81
+ }
82
+ console . log ( command . split ( " " ) [ 0 ] )
83
+ }
84
+
85
+
86
+ input . addEventListener ( "keyup" , function ( event ) {
87
+ if ( event . key === "Enter" ) {
88
+ handleCommand ( input . value ) ;
89
+ input . value = '' ;
90
+ historyNumber = - 1 ;
91
+ }
92
+ else if ( event . key === "ArrowUp" ) {
93
+ // console.log(event.key)
94
+ if ( historyNumber < commandHistory . length ) historyNumber ++ ;
95
+ input . value = commandHistory [ ( commandHistory . length - 1 ) - historyNumber ] !== undefined ? commandHistory [ ( commandHistory . length - 1 ) - historyNumber ] : "" ;
96
+ }
97
+ else if ( event . key === "ArrowDown" ) {
98
+ // console.log(event.key)
99
+ if ( historyNumber > - 1 ) historyNumber -- ;
100
+ input . value = commandHistory [ ( commandHistory . length - 1 ) - historyNumber ] !== undefined ? commandHistory [ ( commandHistory . length - 1 ) - historyNumber ] : "" ;
101
+ }
102
+ else if ( event . key ) {
103
+ // console.log(event.key)
104
+ }
105
+ } ) ;
0 commit comments