Skip to content

Commit

Permalink
Handle more color requests (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
lydell authored Oct 10, 2022
1 parent b402cee commit d2129fc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"typescript": "4.8.3"
},
"scripts": {
"start": "node run-pty.js % cat % false % echo hello 🇬🇧 🙈 world % echo hello wide 古古古古古古古古古古古 % ping localhost % node get-cursor-position.js % node test-keys.js % node signals.js % node slow-kill.js % node slow-kill.js 2000 \"Shutting down…\" % make watch % make signals % node test-clear-down.js % node colored-log.js % node test-exit-in-middle.js % node test-split-color-codes.js",
"start": "node run-pty.js % cat % false % echo hello 🇬🇧 🙈 world % echo hello wide 古古古古古古古古古古古 % ping localhost % node get-cursor-position.js % node test-keys.js % node signals.js % node slow-kill.js % node slow-kill.js 2000 \"Shutting down…\" % make watch % make signals % node test-clear-down.js % node colored-log.js % node test-exit-in-middle.js % node test-split-color-codes.js % node test-request-colors.js",
"example": "node run-pty.js example.json",
"auto-exit": "node run-pty.js --auto-exit=2 % sleep 3 % sleep 1 % sleep 2 % sleep 1 % sleep 1 && echo success",
"test": "node run-pty.js --auto-exit % prettier --check . % eslint . --report-unused-disable-directives % tsc % jest",
Expand Down
23 changes: 14 additions & 9 deletions run-pty.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,11 @@ const NOT_SIMPLE_LOG_ESCAPE = RegExp(
// - 6n and ?6n: Report Cursor Position. Reply uses `R` instead of `n`.
// - t: Report window position, size, title etc.
// - ]10;? and ]11;?: Report foreground/background color. https://unix.stackexchange.com/a/172674
// - ]4;NUM;?: Report color NUM in the palette.
const ESCAPES_REQUEST =
/(\x1B\[(?:\??6n|\d*(?:;\d*){0,2}t)|\x1B\]1[01];\?\x07)/g;
/(\x1B\[(?:\??6n|\d*(?:;\d*){0,2}t)|\x1B\](?:1[01]|4;\d+);\?(?:\x07|\x1B\\))/g;
const ESCAPES_RESPONSE =
/(\x1B\[(?:\??\d+;\d+R|\d*(?:;\d*){0,2}t)|\x1B\]1[01];[^\x07]+\x07)/g;
/(\x1B\[(?:\??\d+;\d+R|\d*(?:;\d*){0,2}t)|\x1B\](?:1[01]|4;\d+);[^\x07\x1B]+(?:\x07|\x1B\\))/g;
const CURSOR_POSITION_RESPONSE = /(\x1B\[\??)\d+;\d+R/g;
const CONPTY_CURSOR_MOVE = /\x1B\[\d+;1H/;
const CONPTY_CURSOR_MOVE_REPLACEMENT = "\n\n";
Expand All @@ -757,10 +758,10 @@ const respondToRequestFake = (request) =>
? "\x1B[1;1R"
: request.endsWith("t")
? "\x1B[3;0;0t"
: request.startsWith("\x1B]10;")
? "\x1B]10;rgb:ffff/ffff/ffff\x07"
: request.startsWith("\x1B]10;") || request.startsWith("\x1B]4;")
? request.replace("?", "rgb:ffff/ffff/ffff")
: request.startsWith("\x1B]11;")
? "\x1B]11;rgb:0000/0000/0000\x07"
? request.replace("?", "rgb:0000/0000/0000")
: "";

// Inspired by this well researched Stack Overflow answer:
Expand All @@ -772,6 +773,8 @@ const respondToRequestFake = (request) =>
// - CSI escapes have a `[` and then 0 or more parameter characters, followed by
// a final character. There is no overlap between the parameter characters and
// the final character.
// - OSC escapes have a `]` and then 1 or more characters, followed by either
// `\x07` or `\x1B\\`.
//
// So a `\x1B` followed by zero or more parameter characters at the very end
// indicates an unfinished escape. But if that’s followed by anything else
Expand All @@ -790,7 +793,7 @@ const respondToRequestFake = (request) =>
//
// Note: The terminals I’ve tested with seem to wait forever for the end of
// escape sequences – they don’t have a timeout or anything.
const UNFINISHED_ESCAPE = /\x1B(?:\[[0-?]*[ -/]*)?$/;
const UNFINISHED_ESCAPE = /\x1B(?:\[[0-?]*[ -/]*|\][^\x1B\x07]*)?$/;

const GRAPHIC_RENDITIONS = /(\x1B\[(?:\d+(?:;\d+)*)?m)/g;

Expand Down Expand Up @@ -1247,8 +1250,10 @@ class Command {
this.windowsConptyCursorMoveWorkaround = false;
}
if (index % 2 === 0) {
const statusFromRulesChanged = this.pushHistory(part);
this.onData(part, statusFromRulesChanged);
if (part !== "") {
const statusFromRulesChanged = this.pushHistory(part);
this.onData(part, statusFromRulesChanged);
}
} else {
this.onRequest(part);
}
Expand Down Expand Up @@ -1884,7 +1889,7 @@ const runInteractively = (commandDescriptions, autoExit) => {
requests.shift();
requestInFlight = false;
handleNextRequest();
} else {
} else if (part !== "") {
onStdin(
part,
current,
Expand Down
29 changes: 29 additions & 0 deletions test-request-colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

process.stdin.setRawMode(true);

let stdin = "";

process.stdin.on("data", (data) => {
const string = data.toString("utf8");
stdin += string;
if (string === "\x03") {
process.exit();
} else if (stdin.includes("]11;")) {
const results =
stdin.match(/rgb:[\da-f]{4}\/[\da-f]{4}\/[\da-f]{4}/gi) ?? [];
console.log("Results", results.length, new Set(results));
process.exit();
}
});

for (let i = 0; i <= 1024; i++) {
if (i % 2 === 0) {
process.stdout.write(`\x1B]4;${i};?\x1B\\`);
} else {
process.stdout.write(`\x1B]4;${i};?\x07`);
}
}

process.stdout.write(`\x1B]10;?\x1B\\`);
process.stdout.write(`\x1B]11;?\x07`);

0 comments on commit d2129fc

Please sign in to comment.