1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-18 22:05:13 +03:00

Add auto completion

This commit is contained in:
Adem Gaygusuz 2018-05-29 22:53:35 +01:00
parent 65fd419649
commit 97775f2748
6 changed files with 3190 additions and 203 deletions

View file

@ -1,14 +1,16 @@
'use strict';
TABS.cli = {
'validateText': "",
'currentLine': "",
'sequenceElements': 0,
lineDelayMs: 15,
profileSwitchDelayMs: 100,
outputHistory: ""
outputHistory: "",
cliBuffer: ""
};
function removePromptHash(promptText) {
return promptText.replace(/^# /, '');
}
TABS.cli.initialize = function (callback) {
var self = this;
@ -59,7 +61,7 @@ TABS.cli.initialize = function (callback) {
} else {
console.log('write complete');
};
}
};
writer.truncate(0);
}, function (){
@ -68,22 +70,73 @@ TABS.cli.initialize = function (callback) {
});
});
function cliBufferCharsToDelete(command, buffer) {
var commonChars = 0;
for (var i = 0;i < buffer.length;i++) {
if (command[i] === buffer[i]) {
commonChars++;
} else {
break;
}
}
return buffer.length - commonChars;
}
function commandWithBackSpaces(command, buffer, noOfCharsToDelete) {
const backspace = String.fromCharCode(127);
return backspace.repeat(noOfCharsToDelete) + command.substring(buffer.length - noOfCharsToDelete, command.length);
}
function getCliCommand(command, cliBuffer) {
const buffer = removePromptHash(cliBuffer);
const bufferRegex = new RegExp('^' + buffer, 'g');
if (command.match(bufferRegex)) {
return command.replace(bufferRegex, '');
}
const noOfCharsToDelete = cliBufferCharsToDelete(command, buffer);
return commandWithBackSpaces(command, buffer, noOfCharsToDelete);
}
// Tab key detection must be on keydown,
// `keypress`/`keyup` happens too late, as `textarea` will have already lost focus.
textarea.keydown(function (event) {
const tabKeyCode = 9;
if (event.which == tabKeyCode) {
// prevent default tabbing behaviour
event.preventDefault();
const outString = textarea.val();
const lastCommand = outString.split("\n").pop();
const command = getCliCommand(lastCommand, self.cliBuffer);
if (command) {
self.sendAutoComplete(command);
textarea.val('');
}
}
});
textarea.keypress(function (event) {
if (event.which == 13) { // enter
const enterKeyCode = 13;
if (event.which == enterKeyCode) {
event.preventDefault(); // prevent the adding of new line
var out_string = textarea.val();
self.history.add(out_string.trim());
var outputArray = out_string.split("\n");
Promise.reduce(outputArray, function(delay, line) {
Promise.reduce(outputArray, function(delay, line, index) {
return new Promise(function (resolve) {
GUI.timeout_add('CLI_send_slowly', function () {
var processingDelay = self.lineDelayMs;
if (line.toLowerCase().startsWith('profile')) {
processingDelay = self.profileSwitchDelayMs;
}
const isLastCommand = index + 1 === outputArray.length;
if (isLastCommand && self.cliBuffer) {
line = getCliCommand(line, self.cliBuffer);
}
self.sendLine(line, function () {
resolve(processingDelay);
});
@ -134,15 +187,26 @@ TABS.cli.history.add = function (str) {
this.history.push(str);
this.index = this.history.length;
};
TABS.cli.history.prev = function () {
if (this.index > 0) this.index -= 1;
return this.history[this.index];
};
TABS.cli.history.next = function () {
if (this.index < this.history.length) this.index += 1;
return this.history[this.index - 1];
};
function writeToOutput(text) {
$('.tab-cli .window .wrapper').append(text);
$('.tab-cli .window').scrollTop($('.tab-cli .window .wrapper').height());
}
function writeToPrompt(text) {
$('.tab-cli textarea').val(text);
}
TABS.cli.read = function (readInfo) {
/* Some info about handling line feeds and carriage return
@ -152,106 +216,122 @@ TABS.cli.read = function (readInfo) {
MAC only understands CR
Linux and Unix only understand LF
Windows understands (both) CRLF
Chrome OS currenty unknown
Chrome OS currently unknown
*/
var data = new Uint8Array(readInfo.data),
text = "";
cliOutput = "",
validateText = "",
sequenceCharsToSkip = 0;
for (var i = 0; i < data.length; i++) {
if (CONFIGURATOR.cliValid) {
if (data[i] == 27 || this.sequenceElements > 0) { // ESC + other
this.sequenceElements++;
const currentChar = String.fromCharCode(data[i]);
// delete previous space
if (this.sequenceElements == 1) {
text = text.substring(0, text.length -1);
}
// Reset
if (this.sequenceElements >= 5) {
this.sequenceElements = 0;
}
}
if (this.sequenceElements == 0) {
switch (data[i]) {
case 10: // line feed
if (GUI.operating_system != "MacOS") {
text += "<br />";
}
this.currentLine = "";
break;
case 13: // carriage return
if (GUI.operating_system == "MacOS") {
text += "<br />";
}
this.currentLine = "";
break;
case 60:
text += '&lt';
break;
case 62:
text += '&gt';
break;
default:
text += String.fromCharCode(data[i]);
this.currentLine += String.fromCharCode(data[i]);
}
this.outputHistory += String.fromCharCode(data[i])
}
if (this.currentLine == 'Rebooting') {
CONFIGURATOR.cliActive = false;
CONFIGURATOR.cliValid = false;
GUI.log(i18n.getMessage('cliReboot'));
GUI.log(i18n.getMessage('deviceRebooting'));
if (BOARD.find_board_definition(CONFIG.boardIdentifier).vcp) { // VCP-based flight controls may crash old drivers, we catch and reconnect
$('a.connect').click();
GUI.timeout_add('start_connection',function start_connection() {
$('a.connect').click();
},2500);
} else {
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
MSP.send_message(MSPCodes.MSP_STATUS, false, false, function() {
GUI.log(i18n.getMessage('deviceReady'));
if (!GUI.tab_switch_in_progress) {
$('#tabs ul.mode-connected .tab_setup a').click();
}
});
},1500); // 1500 ms seems to be just the right amount of delay to prevent data request timeouts
}
}
} else {
if (!CONFIGURATOR.cliValid) {
// try to catch part of valid CLI enter message
this.validateText += String.fromCharCode(data[i]);
text += String.fromCharCode(data[i]);
validateText += currentChar;
cliOutput += currentChar;
continue;
}
const escapeSequenceCode = 27;
const escapeSequenceCharLength = 3;
if (data[i] == escapeSequenceCode && !sequenceCharsToSkip) { // ESC + other
sequenceCharsToSkip = escapeSequenceCharLength;
}
if (sequenceCharsToSkip) {
sequenceCharsToSkip--;
continue;
}
const lineFeedCode = 10;
const carriageReturnCode = 13;
const backspaceCode = 8;
switch (data[i]) {
case lineFeedCode:
if (GUI.operating_system != "MacOS") {
cliOutput += "<br />";
}
this.cliBuffer = "";
break;
case carriageReturnCode:
if (GUI.operating_system == "MacOS") {
cliOutput += "<br />";
}
this.cliBuffer = "";
break;
case 60:
cliOutput += '&lt';
break;
case 62:
cliOutput += '&gt';
break;
case backspaceCode:
cliOutput = cliOutput.slice(0, -1);
this.cliBuffer = this.cliBuffer.slice(0, -1);
break;
default:
cliOutput += currentChar;
this.cliBuffer += currentChar;
}
this.outputHistory += currentChar;
if (this.cliBuffer == 'Rebooting') {
CONFIGURATOR.cliActive = false;
CONFIGURATOR.cliValid = false;
GUI.log(i18n.getMessage('cliReboot'));
GUI.log(i18n.getMessage('deviceRebooting'));
if (BOARD.find_board_definition(CONFIG.boardIdentifier).vcp) { // VCP-based flight controls may crash old drivers, we catch and reconnect
$('a.connect').click();
GUI.timeout_add('start_connection', function start_connection() {
$('a.connect').click();
}, 2500);
} else {
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
MSP.send_message(MSPCodes.MSP_STATUS, false, false, function () {
GUI.log(i18n.getMessage('deviceReady'));
if (!GUI.tab_switch_in_progress) {
$('#tabs ul.mode-connected .tab_setup a').click();
}
});
}, 1500); // 1500 ms seems to be just the right amount of delay to prevent data request timeouts
}
}
}
if (!CONFIGURATOR.cliValid && this.validateText.indexOf('CLI') != -1) {
if (!CONFIGURATOR.cliValid && validateText.indexOf('CLI') !== -1) {
GUI.log(i18n.getMessage('cliEnter'));
CONFIGURATOR.cliValid = true;
this.validateText = "";
validateText = "";
}
$('.tab-cli .window .wrapper').append(text);
$('.tab-cli .window').scrollTop($('.tab-cli .window .wrapper').height());
writeToOutput(cliOutput);
writeToPrompt(removePromptHash(this.cliBuffer));
};
TABS.cli.sendLine = function (line, callback) {
var bufferOut = new ArrayBuffer(line.length + 1);
TABS.cli.send(line + '\n', callback);
};
TABS.cli.sendAutoComplete = function (line, callback) {
TABS.cli.send(line + '\t', callback);
};
TABS.cli.send = function (line, callback) {
var bufferOut = new ArrayBuffer(line.length);
var bufView = new Uint8Array(bufferOut);
for (var c_key = 0; c_key < line.length; c_key++) {
bufView[c_key] = line.charCodeAt(c_key);
}
bufView[line.length] = 0x0D; // enter (\n)
serial.send(bufferOut, callback);
}
};
TABS.cli.cleanup = function (callback) {
if (!(CONFIGURATOR.connectionValid && CONFIGURATOR.cliValid && CONFIGURATOR.cliActive)) {