-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.py
More file actions
189 lines (178 loc) · 6.58 KB
/
Copy pathterminal.py
File metadata and controls
189 lines (178 loc) · 6.58 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from flask import Flask, request, render_template_string
import subprocess
app = Flask(__name__)
# HTML Template for the Terminal
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebTerminal</title>
<style>
body {
font-family: 'Courier New', monospace;
background-color: #1a1a1a; /* Darker background for better contrast */
color: #00ff00;
margin: 0;
padding: 20px;
min-height: 100vh;
}
.container {
max-width: 900px;
margin: 0 auto;
padding-top: 20px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.5);
border-radius: 8px;
background-color: #1a1a1a; /* Matching container background */
border: 1px solid #00ff00; /* Green border to match theme */
}
h1 {
text-align: center;
font-size: 2.2em;
margin-bottom: 20px;
color: #00ff00;
}
#terminal {
background-color: #000000; /* Black terminal background */
border: 1px solid #00ff00; /* Green border to match theme */
border-radius: 5px;
padding: 15px;
min-height: 400px;
overflow-y: auto;
white-space: pre;
}
#output {
white-space: pre-wrap;
word-wrap: break-word;
}
#input-line {
display: flex;
margin-top: 15px;
}
#prompt {
color: #00ff00;
margin-right: 10px;
}
#command-input {
flex-grow: 1;
background-color: transparent;
border: none;
color: #00ff00;
font-family: 'Courier New', monospace;
font-size: 1em;
outline: none;
}
#command-input:focus {
border-bottom: 2px solid #ff0000; /* Red input focus border */
}
form {
margin: 0;
}
@media (max-width: 768px) {
#terminal {
font-size: 0.9em;
padding: 10px;
}
h1 {
font-size: 1.8em;
}
}
</style>
</head>
<body>
<div class="container" role="main">
<h1>WebTerminal</h1>
<div id="terminal" aria-live="polite">
<div id="output">{{ output }}</div>
<form method="POST" id="command-form">
<div id="input-line">
<span id="prompt" aria-hidden="true">user@webterminal:~$</span>
<input type="text" id="command-input" name="command" autocomplete="off" autofocus aria-label="Command Input">
</div>
</form>
</div>
</div>
<script>
let commandHistory = [];
let historyIndex = -1;
document.getElementById('command-form').addEventListener('submit', function(e) {
e.preventDefault();
const commandInput = document.getElementById('command-input');
const command = commandInput.value;
// Add command to history if not empty
if (command) {
commandHistory.push(command);
historyIndex = commandHistory.length; // Reset index to the end
}
fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: 'command=' + encodeURIComponent(command)
})
.then(response => response.text())
.then(html => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const newOutput = doc.getElementById('output').innerHTML; // Get updated output
document.getElementById('output').innerHTML = newOutput; // Update only the output div
commandInput.value = ''; // Clear the input field
});
});
// Handle arrow key navigation in command history
document.getElementById('command-input').addEventListener('keydown', function(e) {
const commandInput = this;
if (e.key === 'ArrowUp') {
e.preventDefault(); // Prevent default behavior
if (historyIndex > 0) {
historyIndex--;
commandInput.value = commandHistory[historyIndex];
setTimeout(() => {
commandInput.setSelectionRange(commandInput.value.length, commandInput.value.length); // Set cursor at end
}, 0);
}
} else if (e.key === 'ArrowDown') {
e.preventDefault(); // Prevent default behavior
if (historyIndex < commandHistory.length - 1) {
historyIndex++;
commandInput.value = commandHistory[historyIndex];
setTimeout(() => {
commandInput.setSelectionRange(commandInput.value.length, commandInput.value.length); // Set cursor at end
}, 0);
} else {
// If at the end of the history, clear the input
historyIndex++;
commandInput.value = '';
setTimeout(() => {
commandInput.setSelectionRange(0, 0); // Set cursor at start
}, 0);
}
}
});
</script>
</body>
</html>
"""
@app.route('/', methods=['GET', 'POST'])
def terminal():
output = ''
if request.method == 'POST':
command = request.form['command']
if command.lower() == 'help':
output = "Available commands:\n- echo: Display a line of text\n- help: Display this help message\n"
elif command.lower().startswith('echo'):
output = command[5:] + "\n"
else:
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=5)
output = result.stdout + result.stderr
except subprocess.TimeoutExpired:
output = "Command execution timed out.\n"
except Exception as e:
output = f"An error occurred: {str(e)}\n"
output = f"user@webterminal:~$ {command}\n{output}"
return render_template_string(html_template, output=output)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000) # default port: 5000