From db94360c344874eaef589f2f83ab426d00614008 Mon Sep 17 00:00:00 2001 From: Lonny Wong Date: Sun, 29 Oct 2023 23:29:30 +0800 Subject: [PATCH] sync with go version --- src/comm.ts | 14 ++++++++------ src/transfer.ts | 14 ++++++++------ 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/comm.ts b/src/comm.ts index 114a16d..1ec3081 100644 --- a/src/comm.ts +++ b/src/comm.ts @@ -14,7 +14,7 @@ export const trzszVersion = "[VersionInject]{version}[/VersionInject]"; /* eslint-disable require-jsdoc */ -export const isRunningInWindows = (function() { +export const isRunningInWindows = (function () { try { return process.platform === "win32"; } catch (err) { @@ -22,13 +22,13 @@ export const isRunningInWindows = (function() { } })(); -export const isRunningInBrowser = (function() { +export const isRunningInBrowser = (function () { try { if (require.resolve("fs") === "fs") { require("fs"); return false; } - } catch (err) { } + } catch (err) {} return true; })(); @@ -247,8 +247,10 @@ export async function checkTmux() { const fs = require("fs"); const [tmuxTty, controlMode, paneWidth] = tokens; + const tmuxPaneWidth = parseInt(paneWidth, 10); + if (controlMode == "1" || !tmuxTty.startsWith("/") || !fs.existsSync(tmuxTty)) { - return [TmuxMode.TmuxControlMode, stdoutWriter, -1]; + return [TmuxMode.TmuxControlMode, stdoutWriter, tmuxPaneWidth]; } const fd = fs.openSync(tmuxTty, "w"); @@ -256,11 +258,11 @@ export async function checkTmux() { const statusInterval = await getTmuxStatusInterval(); await setTmuxStatusInterval("0"); - process.on("exit", async function() { + process.on("exit", async function () { await setTmuxStatusInterval(statusInterval); }); - return [TmuxMode.TmuxNormalMode, tmuxRealWriter, parseInt(paneWidth, 10)]; + return [TmuxMode.TmuxNormalMode, tmuxRealWriter, tmuxPaneWidth]; } async function getTmuxStatusInterval() { diff --git a/src/transfer.ts b/src/transfer.ts index c6ed4bd..3b8525b 100644 --- a/src/transfer.ts +++ b/src/transfer.ts @@ -206,7 +206,7 @@ export class TrzszTransfer { setTimeout(() => { this.cleanTimeoutInMilliseconds = 3000; reject(new TrzszError("Receive data timeout")); - }, timeoutInMilliseconds) + }, timeoutInMilliseconds), ), (async () => { if (!binary) { @@ -238,7 +238,7 @@ export class TrzszTransfer { } public async recvAction() { - const buf = await this.recvString("ACT"); + const buf = await this.recvString("ACT", true); const action = JSON.parse(buf); if (action.newline) { this.protocolNewline = action.newline; @@ -269,10 +269,12 @@ export class TrzszTransfer { } if (tmuxMode == TmuxMode.TmuxNormalMode) { config.tmux_output_junk = true; + } + if (tmuxPaneWidth > 0) { config.tmux_pane_width = tmuxPaneWidth; } let jsonStr = JSON.stringify(config); - jsonStr = jsonStr.replace(/[\u007F-\uFFFF]/g, function(chr) { + jsonStr = jsonStr.replace(/[\u007F-\uFFFF]/g, function (chr) { return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).slice(-4); }); this.transferConfig = config; @@ -390,7 +392,7 @@ export class TrzszTransfer { binary: boolean, escapeCodes: Array, maxBufSize: number, - progressCallback: ProgressCallback + progressCallback: ProgressCallback, ) { let step = 0; if (progressCallback) { @@ -480,7 +482,7 @@ export class TrzszTransfer { openSaveFile: OpenSaveFile, directory: boolean, overwrite: boolean, - progressCallback: ProgressCallback + progressCallback: ProgressCallback, ) { const fileName = await this.recvString("NAME"); const file = await openSaveFile(saveParam, fileName, directory, overwrite); @@ -506,7 +508,7 @@ export class TrzszTransfer { binary: boolean, escapeCodes: Array, timeoutInMilliseconds: number, - progressCallback: ProgressCallback + progressCallback: ProgressCallback, ) { let step = 0; if (progressCallback) {