-
Notifications
You must be signed in to change notification settings - Fork 8
/
C.html
239 lines (194 loc) · 6.23 KB
/
C.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!DOCTYPE html>
<html lang="en">
<head>
<title>Run C Online</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/style.css">
<link rel="stylesheet" href="xterm/dist/xterm.css" />
<style>
@media only screen and (max-width: 558px) {
.nav-tabs>li.active>a {
border-bottom: 1px solid #586473;
}
}
.nav-tabs {
padding-left: 15px;
margin-bottom: 0;
border: none;
}
.tab-content {
border: 1px solid #ddd;
border-radius: 4px;
padding: 15px;
}
.right {
text-align: right;
float: right;
}
</style>
<script src="https://cdn.jsdelivr.net/xterm/2.6.0/xterm.js"></script>
<script src="xterm/dist/addons/terminado/terminado.js"></script>
<script src="https://cdn.jsdelivr.net/ace/1.2.6/min/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/jquery/1.12.4/jquery.min.js" type="text/javascript"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript"></script></head>
<body>
<div class="container" id="container">
<h3 class="h3Title">
<span class="mainSpan">C</span>
<span class="codePlaySpan">code playground</span>
</h3>
<div class="panel panel-default">
<div id="menuButtons">
<button class="btn btn-default" id="btnRun">run<span class="glyphicon glyphicon-play" aria-hidden="true"></span> </button>
</div>
<div class="panel-body">
<div id="playGround">
<ul class="nav nav-tabs" role="tablist">
<li id="inputText" class="active">
<a href="#input" role="tab" data-toggle="tab" aria-expanded="true">
<h4>
input
</h4> </a>
</li>
<li id="outputText" class="">
<a href="#output" role="tab" data-toggle="tab" aria-expanded="false">
<h4>
output
</h4> </a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="input">
<div id="ace-editor"></div>
</div>
<div class="tab-pane" id="output">
<div id="terminal-container"> </div>
<textarea rows="5" cols="10" class="code" name="ace-editor" style="display: none;"></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
var code;
var editor = ace.edit("ace-editor");
var terminalOn = 'false';
var term = new Terminal();
var CodeEditor = {};
var firstLoad = 'true';
CodeEditor.init = function() {
editor.setTheme("ace/theme/chrome");
editor.$blockScrolling = Infinity;
}
CodeEditor.init();
var getCachedCode = function(localStorageVariable, exampleCode){
var tempCode = localStorageVariable in localStorage ? localStorage.getItem(localStorageVariable) : exampleCode;
editor.setValue(tempCode, -1);
return tempCode;
}
var textarea = $('textarea[name="ace-editor"]');
var isTermLoaded = function(language){
if ((term.title != "try@host: ~") && (term.title != "try@host: /home/try")) {
return false;
} else {
term.focus();
if (firstLoad != 'true') {
term.clear();
term.handler("clear");
term.keyPress({
which: 13
});
term.clear();
}
code = encodeURI(textarea.val());
firstLoad = 'false';
return true;
}
}
var initTerm = function(){
$('#terminal-container').text("Executing your code....");
var protocol = (location.protocol === 'https:') ? 'wss://' : 'ws://',
socketURL = protocol + location.hostname + ((location.port) ? (':' + location.port) : '') + "/websocket";
sock = new WebSocket(socketURL);
sock.addEventListener('open', function() {
term.terminadoAttach(sock);
});
term.on('open', function(){
heartbeatIntervalId = setInterval(heartbeat, 100);
});
term.open(document.getElementById('terminal-container'));
}
var changeToRunTab = function(){
$('#input').removeClass('active');
$('#output').addClass('active');
$('#inputText').removeClass('active');
$('#outputText').addClass('active');
}
var Example = `#include <stdio.h>
int main(){
printf("Hello, World!");
}`;
var cachedCode = getCachedCode('cprog', Example);
CodeEditor.initC = function() {
editor.getSession().setMode("ace/mode/c_cpp");
textarea.val(cachedCode);
editor.getSession().on("change", function () {
cacheCode = editor.getSession().getValue();
textarea.val(cacheCode);
localStorage.setItem('cprog', cacheCode);
});
}
CodeEditor.initC();
var heartbeat = function(){
if(isTermLoaded() == true) {
clearInterval(heartbeatIntervalId);
runC();
}
}
var heartbeatIntervalId;
var runC = function(){
var code = encodeURI(textarea.val().replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"'));
$.ajax({
type: 'POST',
url: 'https://host:81/C/',
dataType: 'json',
data: JSON.stringify({
"code": code
}),
contentType: "application/json",
cache: false
}).done(function(data, textStatus, xhr) {
term.focus();
if (firstLoad != 'true') {
term.clear();
term.handler("clear");
term.keyPress({
which: 13
});
term.clear();
}
term.handler("gcc -w " + data[0].file + ".c" + " -o " + data[0].file + " -lm && " + data[0].file + " || echo ");
term.keyPress({
which: 13
});
}).fail(function(data, textStatus, xhr) {
term.reset();
term.clear();
term.writeln("\n\nThere seems to be something wrong with this program.");
term.writeln("\nPlease report the error at ... and we will fix it as soon as possible.");
});
}
$('#btnRun').click(function() {
if (terminalOn == 'false') {
initTerm();
terminalOn = 'true';
} else {
runC();
}
changeToRunTab();
});
</script>
</body>
</html>