Skip to content

Commit

Permalink
fix(core): temporary use forked portable_pty to inherit cursor positi…
Browse files Browse the repository at this point in the history
…on for windows (#21683)
  • Loading branch information
Cammisuli authored Feb 7, 2024
1 parent a86b629 commit e0d8eab
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 36 deletions.
8 changes: 3 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@jest/reporters": "^29.4.1",
"@jest/test-result": "^29.4.1",
"@jest/types": "^29.4.1",
"@monodon/rust": "1.3.2",
"@monodon/rust": "1.3.3",
"@napi-rs/cli": "2.14.0",
"@nestjs/cli": "^9.0.0",
"@nestjs/common": "^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = '0.1.0'
edition = '2021'

[dependencies]
portable-pty = { git = "https://github.com/cammisuli/wezterm" }
anyhow = "1.0.71"
colored = "2"
crossbeam-channel = '0.5'
Expand All @@ -17,7 +18,6 @@ ignore-files = "2.0.0"
itertools = "0.10.5"
once_cell = "1.18.0"
parking_lot = { version = "0.12.1", features = ["send_guard"] }
portable-pty = "0.8.1"
napi = { version = '2.12.6', default-features = false, features = [
'anyhow',
'napi4',
Expand Down
35 changes: 11 additions & 24 deletions packages/nx/src/native/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ use napi::{Env, JsFunction};
use portable_pty::{ChildKiller, CommandBuilder, NativePtySystem, PtySize, PtySystem};
use tracing::trace;

#[cfg(target_os = "windows")]
static CURSOR_POSITION: std::sync::OnceLock<regex::Regex> = std::sync::OnceLock::new();

fn command_builder() -> CommandBuilder {
if cfg!(target_os = "windows") {
let comspec = std::env::var("COMSPEC");
Expand Down Expand Up @@ -98,6 +95,12 @@ impl ChildProcess {

std::thread::spawn(move || {
while let Ok(content) = rx.recv() {
// windows will add `ESC[6n` to the beginning of the output,
// we dont want to store this ANSI code in cache, because replays will cause issues
// remove it before sending it to js
#[cfg(windows)]
let content = content.replace("\x1B[6n", "");

callback_tsfn.call(content, NonBlocking);
}
});
Expand Down Expand Up @@ -159,34 +162,18 @@ pub fn run_command(
let mut reader = BufReader::new(reader);
let mut buffer = [0; 8 * 1024];

let mut strip_clear_code = cfg!(target_os = "windows");

while let Ok(n) = reader.read(&mut buffer) {
if n == 0 {
break;
}

let mut content = String::from_utf8_lossy(&buffer[..n]).to_string();
if strip_clear_code {
strip_clear_code = false;
// remove clear screen
content = content.replacen("\x1B[2J", "", 1);
// remove cursor position 1,1
content = content.replacen("\x1B[H", "", 1);
}

#[cfg(target_os = "windows")]
{
let regex = CURSOR_POSITION.get_or_init(|| {
regex::Regex::new(r"\x1B\[\d+;\d+H")
.expect("failed to compile CURSOR ansi regex")
});
content = regex.replace_all(&content, "").to_string();
}
let content = &buffer[..n];
message_tx
.send(String::from_utf8_lossy(content).to_string())
.ok();

message_tx.send(content.to_string()).ok();
if !quiet {
stdout.write_all(content.as_bytes()).ok();
stdout.write_all(content).ok();
stdout.flush().ok();
}
}
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e0d8eab

Please sign in to comment.