-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
149 lines (143 loc) · 5.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Terminal Page</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/2.19.0/css/jquery.terminal.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.terminal/2.19.0/js/jquery.terminal.min.js"></script>
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
overflow: hidden;
}
#terminal {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="terminal"></div>
<script>
$(document).ready(function() {
var fileSystem = {
'/': {
type: 'dir',
contents: {
'file1.txt': {
type: 'file',
content: 'This is file 1 content.'
},
'file2.txt': {
type: 'file',
content: 'This is file 2 content.'
},
'file3.txt': {
type: 'file',
content: 'This is file 3 content.'
}
}
}
};
$('#terminal').terminal(function(command) {
if (command.startsWith("echo ")) {
var textToEcho = command.substring(5);
this.echo(textToEcho);
} else if (command.startsWith("ls")) {
var path = command.split(' ')[1] || '/';
var directory = resolvePath(path);
if (directory && directory.type === 'dir') {
var files = Object.keys(directory.contents);
this.echo(files.join(' '));
} else {
this.error('Directory not found');
}
} else if (command.startsWith("cat ")) {
var path = command.substring(4);
var file = resolvePath(path);
if (file && file.type === 'file') {
// Display the contents of the file
this.echo(file.content);
} else {
// File not found error
this.error('File not found');
}
} else if (command.startsWith("touch ")) {
var path = command.substring(6);
var parts = path.split('/');
var fileName = parts.pop();
var directoryPath = parts.join('/');
var directory = resolvePath(directoryPath);
if (directory && directory.type === 'dir') {
if (!directory.contents[fileName]) {
directory.contents[fileName] = {
type: 'file',
content: ''
};
this.echo('File ' + fileName + ' created successfully.');
} else {
this.error('File already exists.');
}
} else {
this.error('Directory not found.');
}
} else if (command.startsWith("edit ")) {
var path = command.substring(5);
var file = resolvePath(path);
if (file && file.type === 'file') {
var terminal = this;
this.push(function(command, term) {
if (command === ':wq') {
var content = term.export_view();
if (content) {
file.content = content.export_view_data.text; // Corrected access to exported text
}
term.pop();
} else if (command === ':q') {
term.pop();
}
}, {
prompt: 'nmsedit $ ',
exit: false
});
this.echo(file.content, {format: 'html', raw: true}).export_view();
} else {
this.error('File not found');
}
} else if (command.startsWith("help")) {
this.echo("Available commands:");
this.echo(" - echo [text]: Echo the provided text.");
this.echo(" - ls [path]: List files and directories in the specified path (default is root).");
this.echo(" - cat [file]: Display the contents of the specified file.");
this.echo(" - touch [file]: Create a new file.");
this.echo(" - edit [file]: Edit the contents of the specified file using a vim-like interface.");
this.echo(" - help: Display this help message.");
} else if (command !== '') {
this.echo('The command "' + command + '" may not exist, type help to view a full list of commands.');
}
}, {
greetings: 'Welcome to nmsos!',
name: 'terminal',
prompt: 'user@nmsos $ '
});
function resolvePath(path) {
var parts = path.split('/');
var currentDir = fileSystem['/'];
for (var i = 0; i < parts.length; i++) {
var part = parts[i];
if (!part) continue;
if (currentDir.type !== 'dir') return null;
if (!currentDir.contents[part]) return null;
currentDir = currentDir.contents[part];
}
return currentDir;
}
});
</script>
</body>
</html>