2013-11-01 12:33:19 +00:00
|
|
|
var CliHistory = function () {
|
2013-11-01 14:04:55 +00:00
|
|
|
this.history = [];
|
|
|
|
this.index = 0;
|
2013-11-01 12:33:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CliHistory.prototype = {
|
2013-11-01 14:04:55 +00:00
|
|
|
add: function (str) {
|
|
|
|
this.history.push(str);
|
|
|
|
this.index = this.history.length;
|
|
|
|
},
|
|
|
|
prev: function () {
|
|
|
|
if (this.index > 0) this.index -= 1;
|
|
|
|
return this.history[this.index];
|
|
|
|
},
|
|
|
|
next: function () {
|
|
|
|
if (this.index < this.history.length) this.index += 1;
|
|
|
|
return this.history[this.index - 1];
|
|
|
|
}
|
2013-12-12 16:10:10 +00:00
|
|
|
};
|
2013-11-01 12:33:19 +00:00
|
|
|
|
|
|
|
cli_history = new CliHistory();
|
|
|
|
|
|
|
|
|
2013-04-12 17:17:27 +00:00
|
|
|
function tab_initialize_cli() {
|
2013-08-26 13:16:49 +00:00
|
|
|
ga_tracker.sendAppView('CLI Page');
|
2013-11-09 04:57:05 +00:00
|
|
|
GUI.active_tab = 'cli';
|
2013-08-26 13:16:49 +00:00
|
|
|
|
2013-04-12 17:17:27 +00:00
|
|
|
CLI_active = true;
|
|
|
|
|
|
|
|
// Enter CLI mode
|
|
|
|
var bufferOut = new ArrayBuffer(1);
|
|
|
|
var bufView = new Uint8Array(bufferOut);
|
|
|
|
|
|
|
|
bufView[0] = 0x23; // #
|
|
|
|
|
2014-01-18 11:14:18 +00:00
|
|
|
serial.send(bufferOut, function(writeInfo) {});
|
2013-04-12 17:17:27 +00:00
|
|
|
|
2013-11-01 12:33:19 +00:00
|
|
|
var textarea = $('.tab-cli textarea');
|
|
|
|
textarea.keypress(function(event) {
|
2013-04-12 17:17:27 +00:00
|
|
|
if (event.which == 13) { // enter
|
2013-09-05 10:34:02 +00:00
|
|
|
var out_string = $('.tab-cli textarea').val();
|
|
|
|
var out_arr = out_string.split("\n");
|
2013-11-01 12:33:19 +00:00
|
|
|
cli_history.add(out_string.trim());
|
2013-09-05 10:34:02 +00:00
|
|
|
var timeout_needle = 0;
|
2013-04-12 17:17:27 +00:00
|
|
|
|
2013-09-05 10:34:02 +00:00
|
|
|
for (var i = 0; i < out_arr.length; i++) {
|
|
|
|
send_slowly(out_arr, i, timeout_needle++);
|
2013-04-12 17:17:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 10:34:02 +00:00
|
|
|
$('.tab-cli textarea').val('');
|
2013-04-12 17:17:27 +00:00
|
|
|
}
|
|
|
|
});
|
2013-11-01 12:33:19 +00:00
|
|
|
|
|
|
|
textarea.keyup(function(event) {
|
|
|
|
var keyUp = { 38: true }, keyDown = { 40: true };
|
|
|
|
|
|
|
|
if (event.keyCode in keyUp)
|
|
|
|
textarea.val(cli_history.prev());
|
|
|
|
|
|
|
|
if (event.keyCode in keyDown)
|
|
|
|
textarea.val(cli_history.next());
|
|
|
|
});
|
2013-04-12 17:17:27 +00:00
|
|
|
|
2013-12-18 13:57:27 +00:00
|
|
|
// handle smaller resolutions
|
|
|
|
if (screen.height <= 600) {
|
|
|
|
$('div.tab-cli .window').height(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply dynamic width to the textarea element according to cli window width (minus padding and border width)
|
|
|
|
$('div.tab-cli textarea').width($('div.tab-cli .window').outerWidth() - 7);
|
|
|
|
|
2013-04-12 17:17:27 +00:00
|
|
|
// give input element user focus
|
2013-09-05 10:34:02 +00:00
|
|
|
$('.tab-cli textarea').focus();
|
2013-07-29 06:14:49 +00:00
|
|
|
|
|
|
|
// if user clicks inside the console window, input element gets re-focused
|
|
|
|
$('.tab-cli .window').click(function() {
|
2013-09-05 10:34:02 +00:00
|
|
|
$('.tab-cli textarea').focus();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.tab-cli .copy').click(function() {
|
|
|
|
var text = $('.tab-cli .window .wrapper').html();
|
|
|
|
text = text.replace(/<br\s*\/?>/mg,"\n"); // replacing br tags with \n to keep some of the formating
|
|
|
|
|
|
|
|
var copyFrom = $('<textarea/>');
|
|
|
|
|
|
|
|
copyFrom.text(text);
|
|
|
|
$('body').append(copyFrom);
|
|
|
|
copyFrom.select();
|
|
|
|
document.execCommand('copy');
|
|
|
|
copyFrom.remove();
|
2013-07-29 06:14:49 +00:00
|
|
|
});
|
2013-04-12 17:17:27 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 10:34:02 +00:00
|
|
|
function send_slowly(out_arr, i, timeout_needle) {
|
2013-12-11 17:57:29 +00:00
|
|
|
GUI.timeout_add('CLI_send_slowly', function() {
|
2013-09-05 10:34:02 +00:00
|
|
|
var bufferOut = new ArrayBuffer(out_arr[i].length + 1);
|
|
|
|
var bufView = new Uint8Array(bufferOut);
|
|
|
|
|
|
|
|
for (var c_key = 0; c_key < out_arr[i].length; c_key++) {
|
|
|
|
bufView[c_key] = out_arr[i].charCodeAt(c_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bufView[out_arr[i].length] = 0x0D; // enter (\n)
|
|
|
|
|
2014-01-18 11:14:18 +00:00
|
|
|
serial.send(bufferOut, function(writeInfo) {});
|
2013-09-05 10:34:02 +00:00
|
|
|
}, timeout_needle * 5);
|
|
|
|
}
|
|
|
|
|
2013-04-13 10:38:06 +00:00
|
|
|
/* Some info about handling line feeds and carriage return
|
|
|
|
|
|
|
|
line feed = LF = \n = 0x0A = 10
|
|
|
|
carriage return = CR = \r = 0x0D = 13
|
|
|
|
|
|
|
|
MAC only understands CR
|
|
|
|
Linux and Unix only understand LF
|
|
|
|
Windows understands (both) CRLF
|
2013-11-15 16:17:22 +00:00
|
|
|
Chrome OS currenty unknown
|
2013-04-13 10:38:06 +00:00
|
|
|
*/
|
2013-05-19 17:36:57 +00:00
|
|
|
|
|
|
|
var sequence_elements = 0;
|
|
|
|
|
2013-04-12 17:17:27 +00:00
|
|
|
function handle_CLI(data) {
|
2013-05-19 17:36:57 +00:00
|
|
|
if (data == 27 || sequence_elements > 0) { // ESC + other
|
|
|
|
sequence_elements++;
|
|
|
|
|
|
|
|
// delete previous space
|
|
|
|
if (sequence_elements == 1) {
|
|
|
|
var content_string = $('.tab-cli .window .wrapper').html();
|
|
|
|
var new_string = content_string.substring(0, content_string.length -1);
|
|
|
|
$('.tab-cli .window .wrapper').html(new_string);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
if (sequence_elements >= 5) {
|
|
|
|
sequence_elements = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-11-01 14:04:55 +00:00
|
|
|
|
2013-05-19 17:36:57 +00:00
|
|
|
if (sequence_elements == 0) {
|
|
|
|
switch (data) {
|
|
|
|
case 10: // line feed
|
2013-11-15 16:17:22 +00:00
|
|
|
if (GUI.operating_system == "Windows" || GUI.operating_system == "Linux" || GUI.operating_system == "UNIX") {
|
2013-05-19 17:36:57 +00:00
|
|
|
$('.tab-cli .window .wrapper').append("<br />");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 13: // carriage return
|
2013-11-15 16:17:22 +00:00
|
|
|
if (GUI.operating_system == "MacOS") {
|
2013-05-19 17:36:57 +00:00
|
|
|
$('.tab-cli .window .wrapper').append("<br />");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$('.tab-cli .window .wrapper').append(String.fromCharCode(data));
|
|
|
|
$('.tab-cli .window').scrollTop($('.tab-cli .window .wrapper').height());
|
|
|
|
}
|
2013-04-12 17:17:27 +00:00
|
|
|
}
|
2013-11-01 12:33:19 +00:00
|
|
|
}
|