Skip to content

Commit

Permalink
feat: Folder Support + mkdir Command Added (kind of)
Browse files Browse the repository at this point in the history
  • Loading branch information
meesam4687 committed May 16, 2024
1 parent ca55c61 commit a63e8bd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</div>
</div>
<div class="separator"></div>
<div class="terminalBody" id="terminalBody">root@browser:~$ <span class="caret"></span></div>
<div class="terminalBody" id="terminalBody">root@browser:/$ <span class="caret"></span></div>
</div>
</body>

Expand Down
7 changes: 5 additions & 2 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ let commands = {
"ls": {command: ls, description: "ls - List files in current directory"},
"cat": {command: cat, description: "cat - Show contents of a file"},
"touch": {command: touch, description: "touch - Create a new file"},
"rm": {command: rm, description: "rm - Remove a file"}
"rm": {command: rm, description: "rm - Remove a file"},
"arch": {command: arch, description: "arch - Show the architecture of the system"},
"cd": {command: cd, description: "cd - Change directory"},
"mkdir": {command: mkdir, description: "mkdir - Create a new directory"}
}

function exec(command, rw) {
Expand Down Expand Up @@ -40,7 +43,7 @@ document.addEventListener('keydown', function (event) {
terminalBody.innerHTML += '<br>';
rawInput = input;
exec(input, rawInput);
terminalBody.innerHTML += "root@browser:~$ ";
terminalBody.innerHTML += "root@browser:"+ currentDir +"$ ";
input = '';
terminalBody.scrollTop = terminalBody.scrollHeight - terminalBody.clientHeight;
}
Expand Down
37 changes: 37 additions & 0 deletions scripts/terminalFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,41 @@ function rm(raw){
}
storage.find(x => x.dirpath === currentDir).files.splice(fileIndex, 1)
localStorage.setItem('dirs', JSON.stringify(storage))
}

function arch(){
terminalWrite(navigator.userAgent)
}

function cd(raw){
let dir = raw.split(' ')[1]
if (!dir) {
terminalWrite('Usage: cd [directory]')
return;
}
if (dir === '..') {
currentDir = currentDir.split('/').slice(0, -1).join('/')
return;
}
let dirIndex = storage.findIndex(x => x.dirpath === currentDir + '/' + dir)
if (dirIndex === -1) {
terminalWrite('Directory not found')
return;
}
currentDir += '/' + dir
}

function mkdir(raw){
let dirname = raw.split(' ')[1]
if (!dirname) {
terminalWrite('Usage: mkdir [dirname]')
return;
}
let dir = {
dirname: dirname,
dirpath: currentDir + '/' + dirname,
files: []
}
storage.push(dir)
localStorage.setItem('dirs', JSON.stringify(storage))
}

0 comments on commit a63e8bd

Please sign in to comment.