11const commandInput = document . querySelector ( ".commandField" ) ;
22const output = document . querySelector ( ".output" ) ;
33const outputContainer = document . querySelector ( ".command-output" ) ;
4- const commands = [ "help" , "clear" ] ;
4+ const commands = [ "help" , "clear" , "connect" , "contribute" ] ;
55
6- const executeCommand = ( cmd ) => {
6+ // help cmd output
7+ const showAllAvailableCmds = ( messageBox ) => {
8+
9+ // list down all commands from array
10+ messageBox . innerHTML += " <br>Available Commands to use are: " ;
11+ commands . forEach ( ( cmd ) => {
12+ messageBox . innerHTML += `<br> → ${ cmd } ` ;
13+ } ) ;
14+
15+ messageBox . innerHTML += "<br><br>» We are looking for contributors to improve this projects, are you interested? <br>» Type \'contribute\' and become a open-source buddy for Web-Projects." ;
16+ }
17+
18+ // clear cmd output
19+ const clearTerminal = ( ) => {
20+ // erase everything from output-container
21+ outputContainer . innerHTML = '' ;
22+ }
23+
24+ // connect cmd output
25+ const openGitHubProfile = ( ) => {
26+ // open my github profile
27+ window . open ( "https://github.com/Alkaison" ) ;
28+ }
29+
30+ // open github repo for contribution
31+ const contributionLink = ( ) => {
32+ // open github repo
33+ window . open ( "https://github.com/Alkaison/Web-Projects/" ) ;
34+ }
35+
36+ // executes functions as per command match
37+ const executeCommand = ( cmd , messageBox ) => {
738 switch ( cmd )
839 {
9- case 'help' :
10- console . log ( "Help cmd" ) ;
40+ case "help" :
41+ showAllAvailableCmds ( messageBox ) ;
42+ break ;
43+ case "clear" :
44+ clearTerminal ( messageBox ) ;
1145 break ;
12- case 'clear' :
13- console . log ( "Clear cmd" ) ;
46+ case "connect" :
47+ openGitHubProfile ( ) ;
48+ break ;
49+ case "contribute" :
50+ contributionLink ( ) ;
1451 break ;
1552 }
1653}
1754
55+ // validate the commands
1856commandInput . addEventListener ( "keydown" , ( e ) => {
1957
2058 // get command from input box
@@ -26,17 +64,19 @@ commandInput.addEventListener("keydown", (e) => {
2664 // create new element and append it on output
2765 const createElement = output . cloneNode ( true ) ;
2866 const outputTextMessage = createElement . querySelector ( ".outputText" ) ;
29-
3067 createElement . style . display = "block" ;
3168 outputTextMessage . textContent = inputCommand ;
32- outputContainer . append ( createElement ) ;
33-
34- // clear the input box
35- commandInput . value = '' ;
3669
70+ // append the message
3771 if ( commands . includes ( inputCommand ) )
38- executeCommand ( inputCommand ) ;
72+ executeCommand ( inputCommand , outputTextMessage ) ;
3973 else
40- outputTextMessage . setHTML ( `<br> ${ inputCommand } Command not found. Type \'help\' for list of available commands.` ) ;
74+ outputTextMessage . innerHTML += "<br>Command not found. Type \'help\' for list of available commands." ;
75+
76+ // clear the input box and append the element
77+ commandInput . value = '' ;
78+
79+ if ( inputCommand != "clear" )
80+ outputContainer . append ( createElement ) ;
4181 }
4282} ) ;
0 commit comments