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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
|
/*!
* JQuery Terminal Emulator Plugin
* Copyright (C) 2010 Jakub Jankiewicz <http://jcubic.pl>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
TODO:
fix cursor move in command longer then terminal width
fix resize issue in text that was echoed earlier
terminal must store list of lines that have bean echoed
and when terminal is resized every longer line must be splited
and putted in div
remove access to command_line from outside of terminal
add self.read method this will reduce the code for login
if (command == 'foo') {
var end = false;
while (!end) {
term.read("type foo:", function(str) {
// process str
end = true;
}, {mask:true}); // or passed as single value
}
Bash have "read x; echo $x"
or simply add mask method and users can handle
inputs by themself
if (command == 'passwd') {
term.set_prompt('password:');
term.echo('enter password');
term.mask(true);
process_passwd = true;
} else if (process_passwd) {
if (change_passwd(command)) {
term.echo('password changed');
}
term.mask(false);
}
add support for - $(...).each(function() { ... });
add destroy method to terminal
add suport for clipboard
add echo function without new line
$.fn.pluginname = function(options) {
var settings = $.extend({}, $.fn.pluginname.defaultOptions, options);
return this.each(function() {
var $this = $(this);
});
};
$.fn.pluginname.defaultOptions = {
};
*/
Array.prototype.has = function(val) {
for (var i=this.length; i--;) {
if (this[i] == val) {
return true;
}
}
return false;
};
(function($, undefined) {
// -----------------------------------------------------------------------
/*
function decodeHTML(str) {
if (typeof str == 'string') {
str = str.replace(/&/g, '&');
str = str.replace(/</g, '<').replace(/>/g, '>');
str = str.replace(/	/g, '\t');
str = str.replace(/<br\/?>/g, '\n').replace(/ /g, ' ');
return str;
} else {
return '';
}
}
*/
function str_parts(str, length) {
var result = [];
var len = str.length;
if (len < length) {
return [str];
}
for (var i=0; i<len; i+=length) {
result.push(str.substring(i, i+length));
}
return result;
}
// -----------------------------------------------------------------------
function encodeHTML(str) {
if (typeof str == 'string') {
str = str.replace(/&/g,'&');
str = str.replace(/</g,'<').replace(/>/g,'>');
str = str.replace(/\n/g, '<br/>');
str = str.replace(/ /g, ' ');
// only more than one space replace by to allow wrap of
// long lines
str = str.replace(/ (?= )/g, ' ');
str = str.replace(/\t/g, '	');
// restrict to visible ASCII characters, #32-#126.
str = str.replace(/[^ -~]/g, '');
return str;
} else {
return '';
}
}
// -----------------------------------------------------------------------
// CYCLE DATA STRUCTURE
// -----------------------------------------------------------------------
function Cycle(init) {
var data = init ? [init] : [];
var pos = 0;
$.extend(this, {
rotate: function() {
if (data.length == 1) {
return data[0];
} else {
if (pos == data.length-1) {
pos = 0;
} else {
++pos;
}
return data[pos];
}
},
length: function() {
return data.length;
},
set: function(item) {
for (var i=data.length; i--;) {
if (data[i] === item) {
pos = i;
return;
}
}
this.append(item);
},
front: function() {
return data[pos];
},
append: function(item) {
data.push(item);
}
});
}
// -----------------------------------------------------------------------
// :: BCYCLE DATA STRUCTURE // Two way cycle
// -----------------------------------------------------------------------
function BCycle(init) {
var data = init instanceof Array ? init : init ? [init] : [];
var pos = 0;
$.extend(this, {
left: function() {
if (pos === 0) {
pos = data.length-1;
} else {
--pos;
}
return data[pos];
},
right: function() {
if (pos == data.length-1) {
pos = 0;
} else {
++pos;
}
return data[pos];
},
current: function() {
return data[pos];
},
data: function() {
return data;
},
append: function(item) {
data.push(item);
pos = 0;
}});
}
// -----------------------------------------------------------------------
// :: STACK DATA STRUCTURE
// -----------------------------------------------------------------------
function Stack(init) {
var data = init ? [init] : [];
$.extend(this, {
size: function() {
return data.length;
},
pop: function() {
if (data.length === 0) {
return null;
} else {
var value = data[data.length-1];
data = data.slice(0, data.length-1);
return value;
}
},
push: function(value) {
data = data.concat([value]);
return value;
},
top: function() {
return data.length>0 ? data[data.length-1] : null;
}});
}
// serialize object myself (biwascheme or prototype library do something wiked with
// JSON serialization for Arrays)
function json_stringify(object, level) {
var result = '';
level = level === undefined ? 1 : level;
var type = typeof object;
switch (type) {
case 'function':
result += object;
break;
case 'boolean':
result += object ? 'true' : 'false';
break;
case 'object':
if (object === null) {
result += 'null';
} else if (object instanceof Array) {
result += '[';
var len = object.length;
for (var i=0; i<len-1; ++i) {
result += json_stringify(object[i], level+1);
}
result += json_stringify(object[len-1], level+1) + ']';
} else {
result += '{';
for (var property in object) {
if (object.hasOwnProperty(property)) {
result += '"' + property + '":' +
json_stringify(object[property], level+1);
}
}
result += '}';
}
break;
case 'string':
var str = object;
var repl = {
'\\\\': '\\\\',
'"': '\\"',
'/': '\\/',
'\\n': '\\n',
'\\r': '\\r',
'\\t': '\\t'};
for (var i in repl) {
if (repl.hasOwnProperty(i)) {
str = str.replace(new RegExp(i, "g"), repl[i]);
}
}
result += '"' + str + '"';
break;
case 'number':
result += String(object);
break;
}
result += (level > 1 ? ',' : '');
if (level == 1) {
// last colon fix
result = result.replace(/,([\]}])/g, '$1');
}
return result;
}
$.json_stringify = json_stringify;
// -----------------------------------------------------------------------
// :: HISTORY CLASS
// -----------------------------------------------------------------------
function History(name, cookie) {
var enabled = true;
if (typeof name === 'string' && name !== '') {
name += '_';
}
//default cookie are on
cookie = cookie === undefined || cookie;
var data = cookie ? $.cookie(name + 'commands') : null;
var bc = new BCycle(data ? eval("(" + data + ")") : ['']);
$.extend(this, {
append: function(item) {
if (enabled && bc.current() != item) {
bc.append(item);
if (cookie) {
$.cookie(name + 'commands', json_stringify(bc.data()));
}
}
},
data: function() {
return bc.data();
},
next: function() {
return bc.right();
},
previous: function() {
return bc.left();
},
clear: function() {
bc = new BCycle();
if (cookie) {
$.cookie(name + 'commands', null);
}
},
enable: function() {
enabled = true;
},
disable: function() {
enabled = false;
}});
}
// -----------------------------------------------------------------------
// :: COMMAND LINE PLUGIN
// -----------------------------------------------------------------------
$.fn.cmd = function(options) {
var self = this;
self.addClass('cmd');
self.append('<span class="prompt"></span><span></span>' +
'<span class="cursor"> </span><span></span>');
var clip = $('<textarea/>').addClass('clipboard').appendTo(self);
if (options.width) {
self.width(options.width);
}
var num_chars; // calculates by draw_prompt
var prompt_len;
var mask = options.mask || false;
var command = '';
var position = 0;
var prompt;
var enabled = options.enabled;
var name, history;
var blink = (function() {
var cursor = self.find('.cursor');
return function(i) {
cursor.toggleClass('inverted');
};
})();
function change_num_chars() {
self.append('<span class="__test"> </span>');
var test = self.find('.__test');
num_chars = Math.floor(self.width() / test.width());
test.remove();
}
function get_splited_command_line() {
var first = command.substring(0, num_chars-prompt_len-1);
var rest = command.substring(num_chars-prompt_len-1);
return [first].concat(str_parts(rest, num_chars));
}
var redraw = (function(self) {
var cursor = self.find('.cursor');
var before = cursor.prev();
var after = cursor.next();
return function() {
var string = mask ? command.replace(/./g, '*') : command;
self.find('div').remove();
before.html('');
if (string.length > num_chars-prompt_len-1) {
var array = get_splited_command_line();
var len = array.length;
for (var i=0; i<len-1; ++i) {
before.before('<div>' + encodeHTML(array[i]) + '</div>');
}
string = array[len-1];
}
if (string === '') {
before.html('');
cursor.html(' ');
after.html('');
} else if (position == command.length) {
before.html(encodeHTML(string));
cursor.html(' ');
after.html('');
} else {
if (position === 0) {
before.html('');
//fix for tilda in IE
cursor.html(string.slice(0, 1));
//cursor.html(encodeHTML(string[0]));
after.html(encodeHTML(string.slice(1)));
} else {
var before_str = encodeHTML(string.slice(0, position));
before.html(before_str);
//fix for tilda in IE
var c = string.slice(position, position+1);
//cursor.html(string[position]));
cursor.html(encodeHTML(c));
if (position == string.lenght-1) {
after.html('');
} else {
after.html(encodeHTML(string.slice(position+1)));
}
}
}
//browser don't display last single space before and after the cursor span
before.html(before.html().replace(/ $/, ' '));
after.html(after.html().replace(/^ /, ' '));
};
})(self);
var draw_prompt = (function() {
var prompt_node = self.find('.prompt');
return function() {
if (typeof prompt == 'string') {
prompt_len = prompt.length;
prompt_node.html(encodeHTML(prompt) + ' ');
} else {
prompt(function(string) {
prompt_len = string.length;
prompt_node.html(encodeHTML(string) + ' ');
});
}
change_num_chars();
};
})();
var keyboard_event = (function() {
//var prompt_node = self.find('.prompt');
return function(e) {
if (enabled) {
var pos, len;
if (e.keyCode == 13) {
//remove trailing spaces
command = command.replace(/\s*(.*)\s*$/mg, '$1');
if (history && command) {
history.append(command);
}
var tmp = command;
self.set('');
if (typeof prompt == 'function') {
draw_prompt();
}
if (options.commands) {
options.commands(tmp);
}
} else if (e.which == 32) { //space
append(' ');
} else if (e.which == 8) { //backspace
if (command !== '' && position > 0) {
command = command.slice(0, position-1) +
command.slice(position, command.length);
--position;
redraw();
}
} else if (e.which == 46) { //DELETE
if (command !== '' && position < command.length) {
command = command.slice(0, position) +
command.slice(position+1, command.length);
redraw();
}
} else if (history && e.which == 38 ||
(e.which == 80 && e.ctrlKey)) {
//UP ARROW or CTRL+P
self.set(history.previous());
} else if (history && e.which == 40 ||
(e.which == 78 && e.ctrlKey)) {
//DOWN ARROW or CTRL+N
self.set(history.next());
} else if (e.which == 27) { //escape
self.set('');
} else if (e.which == 37 ||
(e.which == 66 && e.ctrlKey)) {
//LEFT ARROW or CTRL+B
if (e.ctrlKey && e.which != 66) {
len = position-1;
pos = 0;
if (command[len] == ' ') {
--len;
}
for (var i=len; i>0; --i) {
if (command[i] == ' ' && command[i+1] != ' ') {
pos = i+1;
break;
}
}
self.position(pos);
} else {
if (position > 0) {
--position;
redraw();
}
}
} else if (e.which == 39 ||
(e.which == 70 && e.ctrlKey)) {
//RIGHT ARROW OR CTRL+F
if (e.ctrlKey && e.which != 70) {
pos = position;
len = command.length;
if (command[pos] == ' ') {
++pos;
}
for (var i=pos; i<len; ++i) {
if ((command[i] == ' ' && command[i-1] != ' ' ) ||
i==len-1) {
pos = i;
break;
}
}
position = pos;
redraw();
} else {
if (position < command.length) {
++position;
redraw();
}
}
} else if (e.which == 123) { //F12 - Allow Firebug
return true;
} else if (e.which == 36) { //HOME
self.position(0);
} else if (e.which == 35) {
//END
self.position(command.length);
} else if (e.metaKey) {
if (e.shiftKey) { // CTRL+SHIFT+??
if (e.which == 84) {
//CTRL+SHIFT+T open closed tab
return true;
}
} else if (e.altKey) { //ALT+CTRL+??
//return true;
} else {
//NOTE: in opera charCode is undefined
if (e.which == 65) {
//CTRL+A
self.position(0);
} else if (e.which == 69) {
//CTRL+E
self.position(command.length);
} else if (e.which == 88 || e.which == 67 ||
e.which == 87 || e.which == 84) {
//CTRL+X CTRL+C CTRL+W CTRL+T CTRL+T
return true;
} else if (e.which == 86) {
//CTRL+V
clip.focus();
window.setTimeout(function() {
var content = clip.val();
if (position == command.length) {
command += content;
} else if (position === 0) {
command = content + command;
} else {
command = command.slice(0, position) +
content + command.slice(postion);
}
position += content.length;
redraw();
clip.val('');
}, 1);
return true;
} else if (e.which == 75) {
//CTRL+K
if (position === 0) {
self.set('');
} else if (position != command.length) {
self.set(command.slice(0, position));
}
} else if (e.which == 70) { //CTRL+F find
return true;
} else if (e.which == 17) { //CTRL+TAB switch tab
return true;
}
}
} else if (e.altKey) {
//if (e.which == 18) { // press ALT
if (e.which == 68) { //ALT+D
pos = position;
len = command.length;
var space = null;
for (var i=pos; i<len; ++i) {
if (command[i] == ' ') {
space = i+1;
break;
}
}
if (space) {
command = command.slice(0, pos) +
command.slice(space);
} else {
command = command.slice(0, pos);
}
redraw();
}
} else {
return true;
}
return false;
}
};
})();
function append(c) {
if (position == command.length) {
command += c;
} else {
command = command.slice(0, position) + c +
command.slice(position);
}
++position;
redraw();
}
$.extend(self, {
name: function(string) {
if (string !== undefined) {
name = string;
history = new History(string, options.cookie);
} else {
return name;
}
},
history: function() {
return history;
},
set: function(string) {
if (string !== undefined) {
command = string;
position = command.length;
redraw();
}
},
commands: function(commands) {
if (commands) {
options.commands = commands;
} else {
return commands;
}
},
destroy: function() {
$(document.documentElement).unbind('.command_line');
self.find('.prompt').remove();
},
prompt: function(user_prompt) {
if (user_prompt === undefined) {
return prompt;
} else {
if (typeof user_prompt == 'string' ||
typeof user_prompt == 'function') {
prompt = user_prompt;
} else {
throw "prompt must be a function or string";
}
draw_prompt();
}
},
position: function(n) {
if (typeof n == 'number') {
position = n;
redraw();
} else {
return position;
}
},
resize: function() {
change_num_chars();
redraw();
},
enable: function() {
self.everyTime(500, 'blink', blink);
enabled = true;
},
isenabled: function() {
return enabled;
},
disable: function() {
self.stopTime('blink', blink);
self.find('.cursor').removeClass('inverted');
enabled = false;
},
mask: function(display) {
if (typeof display == 'boolean') {
mask = display;
redraw();
} else {
return mask;
}
}
});
// INIT
self.name(options.name || '');
prompt = options.prompt || '>';
draw_prompt();
if (options.enabled === undefined && !options.enabled) {
self.enable();
}
// Keystrokes
$(document.documentElement).keypress(function(e) {
if (enabled) {
if ([38, 32, 13, 40, 0, 8].has(e.which) &&
!(e.which == 40 && e.shiftKey)) {
return false;
} else if (!(e.ctrlKey || e.altKey)) {
append(String.fromCharCode(e.which));
}
}
}).keydown(keyboard_event);
// characters
return self;
};
// -----------------------------------------------------------------------
// JSON-RPC CALL
// -----------------------------------------------------------------------
$.jrpc = function(url, id, method, params, success, error) {
var request = json_stringify({
'jsonrpc': '2.0', 'method': method,
'params': params, 'id': id});
//terminals.front().echo(request);
return $.ajax({
url: url,
data: request,
/*success: function(response) {
terminals.front().echo(JSON.stringify(response));
success(response);
},*/
success: success,
error: error,
contentType: 'application/json',
dataType: 'json',
async: true,
cache: false,
//timeout: 1,
type:"POST"});
};
// -----------------------------------------------------------------------
// :: TERMINAL PLUGIN CODE
// -----------------------------------------------------------------------
//list of terminals global in this scope
var terminals = new Cycle();
$.fn.terminal = function(init_eval, options) {
var self = this;
if (self.length === 0) {
throw 'Sorry, but terminal said that "' + self.selector +
'" is not valid selector';
}
if (self.data('terminal')) {
return self.data('terminal');
}
self.addClass('terminal');
var settings = {
name: null,
greetings: "Wellcome to JQuery Terminal Emulator\n"+
"Copyright (C) 2010 Jakub Jankiewicz <http://jcubic.pl>",
prompt: '>',
history: true,
cookie: true,
exit: true,
enabled: true,
login: null
};
var terminal_id = (function() {
return terminals.length();
})();
if (options) {
if (options.width) {
self.width(options.width);
}
if (options.height) {
self.height(options.height);
}
$.extend(settings, options);
}
self.css('overflow', 'hidden');
self.append('<div class="terminal-output"></div><div></div>');
function get_num_chars() {
self.find('.terminal-output').append('<span class="__test"> </span>');
var test = self.find('.__test');
var result = Math.floor(self.width() / test.width());
test.remove();
return result;
}
//numer of chars in line
var num_chars = get_num_chars();
// display Exception on terminal
function display_exception(e, label) {
if (typeof e == 'string') {
self.echo('[' + label + ']: ' + e).addClass('error');
} else {
//display filename and line which throw exeption
self.echo('[' + label + ']: ' + e.fileName + ': ' + e.message).addClass('error');
self.pause();
$.get(e.fileName, function(file) {
self.resume();
self.echo('[' + e.lineNumber + ']' +
file.split('\n')[e.lineNumber-1]).addClass('error');
});
}
}
//validating if object is string or function, call that function and
//display exeption if any
function valid(label, object) {
try {
if (typeof object == 'function') {
object(function() {
// don't care
});
} else if (typeof object != 'string') {
var msg = label + " must be string or function";
throw msg;
}
} catch (e) {
display_exception(e, label.toUpperCase());
return false;
}
return true;
}
// ----------------------------------------------------------
// TERMINAL METHODS
(function() {
var pause = !settings.enabled;
var output = self.find('.terminal-output');
function scroll_to_bottom(terminal) {
terminal.scrollTop(self.attr("scrollHeight"));
}
$.extend(self, {
clear: function() {
output.html('');
self.command_line.set('');
self.attr({ scrollTop: 0});
},
paused: function() {
return pause;
},
pause: function() {
if (self.command_line) {
self.disable();
self.command_line.hide();
}
},
resume: function() {
if (self.command_line) {
self.enable();
self.command_line.show();
scroll_to_bottom(self);
}
},
resize: function(width, height) {
self.width(width);
self.height(height);
scroll_to_bottom(self);
num_chars = get_num_chars();
self.command_line.resize();
},
focus: function(toggle) {
if (terminals.length() == 1) {
self.oneTime(100, function() {
if (toggle === undefined || toggle) {
terminals.front().enable();
} else {
terminals.front().disable();
}
});
} else if (terminals.length() > 0) {
if (toggle === undefined || toggle) {
if (terminals.front() === self) {
self.enable();
} else {
terminals.front().disable();
terminals.set(self);
self.enable();
}
scroll_to_bottom(self);
} else {
self.disable();
//delay enabling next terminal (NOTE: when using
//single keypress to switch between terminals
//that character was put on next terminal)
self.oneTime(100, function() {
terminals.rotate().enable();
scroll_to_bottom(terminals.front());
});
}
}
},
enable: function() {
if (self.command_line) {
self.command_line.enable();
pause = false;
}
},
disable: function() {
if (self.command_line) {
pause = true;
self.command_line.disable();
}
},
set_prompt: function(prompt) {
if (valid('prompt', prompt)) {
self.command_line.prompt(prompt);
}
},
echo: function(object, options) {
var string = typeof object == 'string' ? object : String(object);
var div;
if (options && options.raw) {
div = $('<div>' + string + '</div>');
} else if (string.length > num_chars) {
var array = string.split('\n'); // string can have line break
div = $('<div></div>');
var len = array.length;
for (var i=0; i<len; ++i) {
if (array[i] === '' || array[i] == '\r') {
div.append('<div> </div>');
} else {
if (array[i].length > num_chars) { // if line is longer then width
$.each(str_parts(array[i], num_chars), function(i, string) {
div.append('<div>' + encodeHTML(string) + '</div>');
});
} else {
div.append('<div>' + encodeHTML(array[i]) + '</div>');
}
}
}
} else {
div = $('<div>' + encodeHTML(string) + '</div>');
}
output.append(div);
div.width('100%');
scroll_to_bottom(self);
return div;
},
error: function(message) {
return self.echo(message).addClass('error');
},
scroll: function(amount) {
if (amount > self.attr('scrollTop') && amount > 0) {
self.attr('scrollTop', 0);
}
var pos = self.attr('scrollTop');
self.attr('scrollTop', pos+amount);
},
logout: settings.login ? function() {
while (interpreters.size() > 1) {
interpreters.pop();
}
logout();
} : function () {
throw "You don't have login function";
},
token: settings.login ? function() {
return $.cookie('token' + (settings.name ? '_' + settings.name : ''));
} : null,
login_name: settings.login ? function() {
return $.cookie('login_' + (settings.name ? '_' + settings.name : ''));
} : null,
name: function() {
return settings.name;
},
push: function(_eval, options) {
if (!options.prompt || valid('prompt', options.prompt) &&
(options.greetings !== null || options.preetings !== false ||
valid('greetings', options.greetings))) {
if (typeof _eval == 'string') {
_eval = make_json_rpc_eval_fun(options['eval'], self);
}
interpreters.push({
name: options.name,
'eval': _eval,
prompt: options.prompt,
login: options.login,
greetings: options.greetings});
if (options.login) {
login();
} else {
prepare_top_interpreter(true);
}
}
},
pop: function() {
echo_command('');
if (interpreters.top().name === settings.name) {
if (settings.login) {
logout();
} else {
return null;
}
} else {
var current = interpreters.pop();
prepare_top_interpreter();
return current;
}
}
});
})();
//function constructor for eval
function make_json_rpc_eval_fun(url, terminal) {
var id = 1;
var service = function(method, params) {
terminal.pause();
$.jrpc(url, id++, method, params, function(json) {
if (!json.error) {
if (typeof json.result == "string") {
terminal.echo(json.result);
} else if (json.result instanceof Array) {
terminal.echo(json.result.join(" "));
} else if (typeof json.result == "object") {
var string = "";
for (var f in json.result) {
if (json.result.hasOwnProperty(f)) {
string += f + ": " + json.result[f] + "\n";
}
}
terminal.echo(string);
}
} else {
terminal.error('[RPC] ' + json.error.message);
}
terminal.resume();
}, function(xhr, status, error) {
terminal.error('[AJAX] ' + status +
' - Server reponse is: \n' +
xhr.responseText);
terminal.resume();
});
};
//this is eval function
return function(command, terminal) {
if (command === '') {
return;
}
var method, params;
if (!command.match(/[^ ]* /)) {
method = command;
params = [];
} else {
command = command.split(' ');
method = command[0];
params = command.slice(1);
}
if (!settings.login || method == 'help') {
service(method, params);
} else {
var token = terminal.token();
if (token) {
service(method, [token].concat(params));
} else {
terminal.error('[AUTH] Access denied (no token)');
}
}
};
}
// create json-rpc eval function
var url;
if (typeof init_eval == 'string') {
url = init_eval;
init_eval = make_json_rpc_eval_fun(init_eval, self);
}
// create json-rpc authentication function
if (url && typeof settings.login == 'string' || url) {
settings.login = (function(method) {
var id = 1;
return function(user, passwd, callback) {
self.pause();
$.jrpc(url,
id++,
method,
[user, passwd],
function(response) {
self.resume();
if (!response.error && response.result) {
callback(response.result);
} else {
callback(null);
}
}, function(xhr, status, error) {
self.resume();
self.error('[AJAX] Response: ' +
status + '\n' +
xhr.responseText);
});
};
//default name is login so you can pass true
})(typeof settings.login == 'boolean' ? 'login' : settings.login);
}
//display prompt and last command
function echo_command(command) {
var prompt = self.command_line.prompt();
if (self.command_line.mask()) {
command = command.replace(/./g, '*');
}
if (typeof prompt == 'function') {
prompt(function(string) {
self.echo(string + ' ' + command);
});
} else {
self.echo(prompt + ' ' + command);
}
}
// wrapper over eval it implements exit and catch all exeptions
// from user code and display them on terminal
function commands(command) {
try {
var interpreter = interpreters.top();
if (command == 'exit' && settings.exit) {
if (interpreters.size() == 1) {
if (settings.login) {
logout();
} else {
var msg = "You can exit from main interpeter";
self.echo(msg);
}
} else {
terminal.pop();
}
} else {
echo_command(command);
}
interpreter['eval'](command, self);
} catch (e) {
display_exception(e, 'USER');
throw e;
}
}
// functions change prompt of command line to login to password
// and call user login function with callback that set token
// if user call it with value that is true
function login() {
var user = null;
self.command_line.prompt('login:');
// don't stor logins in history
if (settings.history) {
self.command_line.history().disable();
}
self.command_line.commands(function(command) {
try {
echo_command(command);
if (!user) {
user = command;
self.command_line.prompt('password:');
self.command_line.mask(true);
} else {
self.command_line.mask(false);
settings.login(user, command, function(user_data) {
if (user_data) {
// if no cookie overwrite authentication methods
if (!settings.cookie) {
self.token = function() {
return user_data;
};
self.login = function() {
return user;
};
} else {
var name = (settings.name ? '_' + settings.name : '');
$.cookie('token' + name, user_data);
$.cookie('login' + name, user);
}
//restore commands and run interpreter
self.command_line.commands(commands);
prepare_top_interpreter(true);
} else {
self.error('Wrong password try again');
self.command_line.prompt('login:');
user = null;
}
self.resume();
});
}
} catch(e) {
display_exception(e, 'LOGIN', self);
throw e;
}
});
}
//logout function remove cookies disable history and run login function
//this function is call only when options.login function is defined
//check for this is in self.pop method
function logout() {
$.cookie('token' + (settings.name ? '_' + settings.name : ''), null);
$.cookie('login_' + (settings.name ? '_' + settings.name : ''), null);
if (settings.history) {
self.command_line.history().disable();
}
login();
}
//function enable history, set prompt, run eval function
function prepare_top_interpreter(greetings) {
var interpreter = interpreters.top();
var name = '';
if (interpreter.name !== undefined &&
interpreter.name !== '') {
name += interpreter.name + '_';
}
name += terminal_id;
self.command_line.name(name);
self.command_line.prompt(interpreter.prompt);
if (settings.history) {
self.command_line.history().enable();
}
self.command_line.set('');
if (greetings) {
if (typeof interpreter.greetings == 'function') {
try {
interpreter.greetings(function(user_string) {
self.echo(user_string);
});
} catch (e) {
display_exception(e, 'GREETINGS', self);
throw e;
}
} else if (typeof interpreter.greetings == 'string') {
self.echo(interpreter.greetings);
}
}
}
function key_press(e) {
if (!self.paused()) {
if (e.charCode == 100 && e.metaKey && settings.exit) {
if (settings.name == interpreters.top().name && !settings.login) {
self.echo("you can't exit from top interpreter");
} else {
self.pop();
}
return false;
} else if (e.charCode == 118 && e.ctrlKey) {
window.setTimeout(function() {
self.attr({scrollTop:
self.attr("scrollHeight")});
}, 1);
return true;
} else if (e.which == 34) { // PAGE DOWN
self.scroll(self.height());
} else if (e.which == 33) { // PAGE UP
self.scroll(-self.height());
} else {
self.attr({scrollTop: self.attr("scrollHeight")});
}
}
}
// INIT CODE
if (valid('prompt', settings.prompt) &&
(settings.greetings !== null ||
settings.greetings !== false ||
valid('greetings', settings.greetings))) {
var interpreters = new Stack({'name': settings.name,
'eval': init_eval,
'prompt': settings.prompt,
'greetings': settings.greetings});
self.command_line = self.find('.terminal-output').next().cmd({
prompt: settings.prompt,
history: settings.history,
width: '100%',
cookie: settings.cookie,
commands: commands
});
terminals.append(self);
if (settings.enabled) {
self.focus();
} else {
self.disable();
}
self.click(self.focus);
$(document.documentElement).keypress(key_press);
if (self.token && !self.token() && self.login_name && !self.login_name()) {
login();
} else {
prepare_top_interpreter(true);
}
self.mousewheel(function(event,delta){
//self.echo(dir(event));
if (delta > 0) {
self.scroll(-40);
} else {
self.scroll(40);
}
return false;
}, true);
}
self.data('terminal', self);
return self;
}; //terminal plugin
})(jQuery);
|