Skip to content

Commit

Permalink
Use a function to parse escape sequences in the text stream
Browse files Browse the repository at this point in the history
Signed-off-by: RD WebDesign <[email protected]>
  • Loading branch information
rdwebdesign committed Oct 8, 2023
1 parent dfb76f5 commit d62077c
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions scripts/pi-hole/js/gravity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]);
}
}

0 comments on commit d62077c

Please sign in to comment.