From dfb76f509c146ed38c28ea9e8142cb31ed6843cf Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Sat, 7 Oct 2023 03:07:08 -0300 Subject: [PATCH 1/3] Fix the gravity output to use the ${OVER} sequence Signed-off-by: RD WebDesign --- scripts/pi-hole/js/gravity.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/pi-hole/js/gravity.js b/scripts/pi-hole/js/gravity.js index 7c7a6832..9c354d28 100644 --- a/scripts/pi-hole/js/gravity.js +++ b/scripts/pi-hole/js/gravity.js @@ -47,9 +47,18 @@ function eventsource() { // Enqueue the next data chunk into our target stream controller.enqueue(value); var string = new TextDecoder().decode(value); - // Remove ${OVER} from the string - string = string.replaceAll("\r", "\n"); - ta.append(string); + + // If a Carriage Return is found ... + if (string.indexOf("\r") === -1) { + ta.append(string); + } else { + // ... remove the last line from the output ... + ta.text(ta.text().substring(0, ta.text().lastIndexOf("\n")) + "\n"); + // ... and append the new text to the end of the output, + // without ${OVER} ("CR + ESC[K") or Carriage Return. + ta.append(string.replaceAll("\r", "").replaceAll("\r", "")); + } + if (string.indexOf("Pi-hole blocking is") !== -1) { alSuccess.show(); } From d62077c8f10a5c14a49b0cd39d968fb6c57b5417 Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Sun, 8 Oct 2023 17:55:05 -0300 Subject: [PATCH 2/3] Use a function to parse escape sequences in the text stream Signed-off-by: RD WebDesign --- scripts/pi-hole/js/gravity.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/scripts/pi-hole/js/gravity.js b/scripts/pi-hole/js/gravity.js index 9c354d28..2f5f55dc 100644 --- a/scripts/pi-hole/js/gravity.js +++ b/scripts/pi-hole/js/gravity.js @@ -47,17 +47,7 @@ function eventsource() { // Enqueue the next data chunk into our target stream controller.enqueue(value); var string = new TextDecoder().decode(value); - - // If a Carriage Return is found ... - if (string.indexOf("\r") === -1) { - ta.append(string); - } else { - // ... remove the last line from the output ... - ta.text(ta.text().substring(0, ta.text().lastIndexOf("\n")) + "\n"); - // ... and append the new text to the end of the output, - // without ${OVER} ("CR + ESC[K") or Carriage Return. - ta.append(string.replaceAll("\r", "").replaceAll("\r", "")); - } + parseLines(ta, string); if (string.indexOf("Pi-hole blocking is") !== -1) { alSuccess.show(); @@ -93,3 +83,24 @@ $(function () { eventsource(); } }); + +function parseLines(ta, str) { + // str can contain multiple lines. + // We want to split the text before an "OVER" escape sequence to allow overwriting previous line when needed + + // Splitting the text on "\r" + var lines = str.split(/(?=\r)/g); + + for (let i = 0; i < lines.length; i++) { + if (lines[i][0] === "\r") { + // This line starts with the "OVER" sequence. Replace them with "\n" before print + lines[i] = lines[i].replaceAll("\r", "\n").replaceAll("\r", "\n"); + + // Last line from the textarea will be overwritten, so we remove it + ta.text(ta.text().substring(0, ta.text().lastIndexOf("\n"))); + } + + // Append the new text to the end of the output + ta.append(lines[i]); + } +} From d969adee0f6dbf528e64dfa07527eae50dd165ab Mon Sep 17 00:00:00 2001 From: RD WebDesign Date: Sun, 8 Oct 2023 18:30:20 -0300 Subject: [PATCH 3/3] Adding a final message when gravity is done Signed-off-by: RD WebDesign --- scripts/pi-hole/js/gravity.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/pi-hole/js/gravity.js b/scripts/pi-hole/js/gravity.js index 2f5f55dc..e95fa7c0 100644 --- a/scripts/pi-hole/js/gravity.js +++ b/scripts/pi-hole/js/gravity.js @@ -38,6 +38,7 @@ function eventsource() { return reader.read().then(({ done, value }) => { // When no more data needs to be consumed, close the stream if (done) { + ta.append("Done."); controller.close(); alInfo.hide(); $("#gravityBtn").prop("disabled", false);