From 3560d7b716a049e4ae39b0276b993b3b8fbfc831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1roly=20Kiripolszky?= Date: Tue, 20 Nov 2018 11:27:05 +0100 Subject: [PATCH 1/5] add button to CLI for loading commands from file --- locales/en/messages.json | 6 ++++++ src/js/tabs/cli.js | 36 ++++++++++++++++++++++++++++++++++++ src/tabs/cli.html | 1 + 3 files changed, 43 insertions(+) diff --git a/locales/en/messages.json b/locales/en/messages.json index 32c7d722..801ca27f 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -2289,6 +2289,12 @@ "cliCopySuccessful": { "message": "Copied!" }, + "cliLoadFromFileBtn": { + "message": "Load from file" + }, + "cliCommandsLoadedNotice": { + "message": "Commands loaded from `{{filename}}`. Press ENTER to confirm execution!" + }, "loggingNote": { "message": "Data will be logged in this tab only, leaving the tab will cancel logging and application will return to its normal \"configurator\" state.
You are free to select the global update period, data will be written into the log file every 1 second for performance reasons." diff --git a/src/js/tabs/cli.js b/src/js/tabs/cli.js index bb239193..041e54cf 100644 --- a/src/js/tabs/cli.js +++ b/src/js/tabs/cli.js @@ -173,6 +173,42 @@ TABS.cli.initialize = function (callback, nwGui) { } else { $('.tab-cli .copy').hide(); } + + $('.tab-cli .load').click(function() { + var suffix = 'txt', + accepts = [{ + description: suffix.toUpperCase() + ' files', extensions: [suffix], + }]; + + chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function(entry) { + if (chrome.runtime.lastError) { + console.error(chrome.runtime.lastError.message); + return; + } + + if (!entry) { + console.log('No file selected'); + return; + } + + function setCommands(result) { + let notice = i18n.getMessage("cliCommandsLoadedNotice", { + filename: entry.name, + }); + textarea.val(result + "\n" + "# " + notice); + // scroll to the last line to show confirmation notice + textarea.scrollTop(textarea[0].scrollHeight); + } + + entry.file((file) => { + let reader = new FileReader(); + reader.onload = + () => setCommands(reader.result); + reader.onerror = () => console.error(reader.error); + reader.readAsText(file); + }); + }); + }); // Tab key detection must be on keydown, // `keypress`/`keyup` happens too late, as `textarea` will have already lost focus. diff --git a/src/tabs/cli.html b/src/tabs/cli.html index b25699ec..06b4b200 100644 --- a/src/tabs/cli.html +++ b/src/tabs/cli.html @@ -16,6 +16,7 @@
+
From 58373666cb9c49319c62ff5e39714fed6ad2541c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1roly=20Kiripolszky?= Date: Thu, 11 Apr 2019 11:17:44 +0200 Subject: [PATCH 2/5] add CLI snippet preview dialog --- locales/en/messages.json | 10 ++++- src/css/tabs/cli.css | 14 ++++++- src/js/tabs/cli.js | 91 ++++++++++++++++++++++++++-------------- src/tabs/cli.html | 15 ++++++- 4 files changed, 94 insertions(+), 36 deletions(-) diff --git a/locales/en/messages.json b/locales/en/messages.json index 801ca27f..619f356b 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -2292,8 +2292,14 @@ "cliLoadFromFileBtn": { "message": "Load from file" }, - "cliCommandsLoadedNotice": { - "message": "Commands loaded from `{{filename}}`. Press ENTER to confirm execution!" + "cliConfirmSnippetDialogTitle": { + "message": "Review loaded commands" + }, + "cliConfirmSnippetNote": { + "message": "Note: You can review and edit commands before execution." + }, + "cliConfirmSnippetBtn": { + "message": "Execute" }, "loggingNote": { diff --git a/src/css/tabs/cli.css b/src/css/tabs/cli.css index 4775a204..cba2370b 100644 --- a/src/css/tabs/cli.css +++ b/src/css/tabs/cli.css @@ -42,7 +42,7 @@ float: left; } -.tab-cli textarea { +.tab-cli textarea[name='commands'] { -webkit-box-sizing: border-box; width: 100%; margin-top: 8px; @@ -53,6 +53,18 @@ resize: none; } +.jBox-container textarea#preview { + background-color: rgba(0, 0, 0, 0.75); + width: 100%; + resize: none; + overflow-y: scroll; + overflow-x: hidden; + font-family: monospace; + color: white; + padding: 5px; + margin-bottom: 5px; +} + .tab-cli #content-watermark { z-index: 0; } diff --git a/src/js/tabs/cli.js b/src/js/tabs/cli.js index 041e54cf..093e088e 100644 --- a/src/js/tabs/cli.js +++ b/src/js/tabs/cli.js @@ -4,7 +4,10 @@ TABS.cli = { lineDelayMs: 15, profileSwitchDelayMs: 100, outputHistory: "", - cliBuffer: "" + cliBuffer: "", + GUI: { + snippetPreviewWindow: null, + }, }; function removePromptHash(promptText) { @@ -95,6 +98,31 @@ TABS.cli.initialize = function (callback, nwGui) { // nwGui variable is set in main.js const clipboardCopySupport = !(nwGui == null && !navigator.clipboard); + const enterKeyCode = 13; + + function executeCommands(out_string) { + self.history.add(out_string.trim()); + + var outputArray = out_string.split("\n"); + Promise.reduce(outputArray, function(delay, line, index) { + return new Promise(function (resolve) { + GUI.timeout_add('CLI_send_slowly', function () { + var processingDelay = self.lineDelayMs; + line = line.trim(); + if (line.toLowerCase().startsWith('profile')) { + processingDelay = self.profileSwitchDelayMs; + } + const isLastCommand = outputArray.length === index + 1; + if (isLastCommand && self.cliBuffer) { + line = getCliCommand(line, self.cliBuffer); + } + self.sendLine(line, function () { + resolve(processingDelay); + }); + }, delay) + }) + }, 0); +} $('#content').load("./tabs/cli.html", function () { // translate to user-selected language @@ -102,7 +130,7 @@ TABS.cli.initialize = function (callback, nwGui) { CONFIGURATOR.cliActive = true; - var textarea = $('.tab-cli textarea'); + var textarea = $('.tab-cli textarea[name="commands"]'); CliAutoComplete.initialize(textarea, self.sendLine.bind(self), writeToOutput); $(CliAutoComplete).on('build:start', function() { @@ -190,20 +218,37 @@ TABS.cli.initialize = function (callback, nwGui) { console.log('No file selected'); return; } + + let previewArea = $("#snippetpreviewcontent textarea#preview"); - function setCommands(result) { - let notice = i18n.getMessage("cliCommandsLoadedNotice", { - filename: entry.name, + function executeSnippet() { + const commands = previewArea.val(); + executeCommands(commands); + self.GUI.snippetPreviewWindow.destroy(); + self.GUI.snippetPreviewWindow = null; + } + + function previewCommands(result) { + self.GUI.snippetPreviewWindow = new jBox("Modal", { + width: 'auto', + height: 'auto', + closeButton: 'title', + animation: false, + attach: $('.tab-cli'), + title: i18n.getMessage("cliConfirmSnippetDialogTitle"), + content: $('#snippetpreviewcontent'), + onCreated: () => + $("#snippetpreviewcontent a.confirm").click(() => executeSnippet()) + , }); - textarea.val(result + "\n" + "# " + notice); - // scroll to the last line to show confirmation notice - textarea.scrollTop(textarea[0].scrollHeight); + previewArea.val(result); + self.GUI.snippetPreviewWindow.open(); } entry.file((file) => { let reader = new FileReader(); reader.onload = - () => setCommands(reader.result); + () => previewCommands(reader.result); reader.onerror = () => console.error(reader.error); reader.readAsText(file); }); @@ -236,7 +281,6 @@ TABS.cli.initialize = function (callback, nwGui) { }); textarea.keypress(function (event) { - const enterKeyCode = 13; if (event.which == enterKeyCode) { event.preventDefault(); // prevent the adding of new line @@ -245,28 +289,7 @@ TABS.cli.initialize = function (callback, nwGui) { } var out_string = textarea.val(); - self.history.add(out_string.trim()); - - var outputArray = out_string.split("\n"); - Promise.reduce(outputArray, function(delay, line, index) { - return new Promise(function (resolve) { - GUI.timeout_add('CLI_send_slowly', function () { - var processingDelay = self.lineDelayMs; - line = line.trim(); - if (line.toLowerCase().startsWith('profile')) { - processingDelay = self.profileSwitchDelayMs; - } - const isLastCommand = outputArray.length === index + 1; - if (isLastCommand && self.cliBuffer) { - line = getCliCommand(line, self.cliBuffer); - } - self.sendLine(line, function () { - resolve(processingDelay); - }); - }, delay) - }) - }, 0); - + executeCommands(out_string); textarea.val(''); } }); @@ -469,6 +492,10 @@ TABS.cli.send = function (line, callback) { }; TABS.cli.cleanup = function (callback) { + if (TABS.cli.GUI.snippetPreviewWindow) { + TABS.cli.GUI.snippetPreviewWindow.destroy(); + TABS.cli.GUI.snippetPreviewWindow = null; + } if (!(CONFIGURATOR.connectionValid && CONFIGURATOR.cliValid && CONFIGURATOR.cliActive)) { if (callback) { callback(); diff --git a/src/tabs/cli.html b/src/tabs/cli.html index 06b4b200..264c2e61 100644 --- a/src/tabs/cli.html +++ b/src/tabs/cli.html @@ -21,4 +21,17 @@
- + + + + \ No newline at end of file From f386b0ce1a9ca17a01ed057b53fe65ed0428c1e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1roly=20Kiripolszky?= Date: Thu, 11 Apr 2019 12:30:13 +0200 Subject: [PATCH 3/5] fix CLI unit tests --- test/tabs/cli.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/tabs/cli.js b/test/tabs/cli.js index c953b12b..44cbaf1d 100644 --- a/test/tabs/cli.js +++ b/test/tabs/cli.js @@ -14,7 +14,7 @@ describe('TABS.cli', () => { describe('output', () => { const cliTab = $('
').addClass('tab-cli'); const cliOutput = $('
').addClass('wrapper') - const cliPrompt = $('