From a7e974da44ecb74b861bcb970b33344fae476e00 Mon Sep 17 00:00:00 2001 From: mhmdkrmabd Date: Tue, 29 Oct 2024 14:05:55 +0300 Subject: [PATCH] Fix for the bug in ticket #520, terminals corruption on macOS in specific conditions --- custom_node_modules/main/pty.js | 103 +++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 2 deletions(-) diff --git a/custom_node_modules/main/pty.js b/custom_node_modules/main/pty.js index bcef2bc7..99b42f05 100755 --- a/custom_node_modules/main/pty.js +++ b/custom_node_modules/main/pty.js @@ -127,6 +127,36 @@ class Pty { // If we've got SSH tunneling info this.ssh = data.ssh || null + this.chars = [] + this.writeCharTimeout = null + + this.writeChar = () => { + let currentCharIndex = 0, + writeCharInner = () => { + this.writeCharTimeout = setTimeout(() => { + let char = this.chars[currentCharIndex] + + if (char == undefined) + return; + + if (char !== -1) { + this.processBasic.write(`${char}`) + this.chars[currentCharIndex] = -1 + } + + ++currentCharIndex + + writeCharInner() + }) + } + + try { + clearTimeout(this.writeCharTimeout) + } catch (e) {} + + writeCharInner() + } + // To be more comfortable about pointing at the main instance let instance = this, count = 0 // Counter to neglect data (like Python warnings) @@ -386,8 +416,66 @@ class Pty { // Update the latest received block ID as well this.latestBlockID = blockID || '' + try { + if (OS.platform() != 'darwin') + throw 0 + + let multipleCommands = `${command}`.split(OS.EOL), + currentCommandIndex = 0, + splitCommand = (command, length) => { + let result = [] + + for (let i = 0; i < command.length; i += length) + result.push(command.slice(i, i + length)) + + return result + }, + writeCommand = () => { + let singleCommand = multipleCommands[currentCommandIndex] + + if (singleCommand == undefined) + return + + // Split the signle command to fixed-length chunks + let commandChunks = splitCommand(`${singleCommand}`, 50), + currentChunkIndex = -1 + + let writeChunks = () => { + setTimeout(() => { + ++currentChunkIndex; + + let chunk = commandChunks[currentChunkIndex] + + if (currentChunkIndex >= commandChunks.length - 1) { + // Send the command to the pty instance + try { + this[process].write(chunk + OS.EOL) + } catch (e) {} + + ++currentCommandIndex + + writeCommand() + + return + } + + this[process].write(chunk) + + writeChunks() + }, 30) + } + + writeChunks() + } + + writeCommand() + } catch (e) {} + // Send the command to the pty instance try { + if (OS.platform() == 'darwin') + throw 0 + this[process].write(command + OS.EOL) } catch (e) {} @@ -412,6 +500,17 @@ class Pty { * {string} `char` the typed character to be written in the pty instance */ realtimeData(char) { + try { + if (OS.platform() != 'darwin') + throw 0 + + this.chars = this.chars.concat(char.split('')) + this.chars = this.chars.filter((char) => char !== -1) + this.writeChar() + + return + } catch (e) {} + this.processBasic.write(char) } @@ -561,8 +660,8 @@ class Pty { try { // If the host OS is macOS then skip this try-catch block - if (Platform == 'darwin') - throw 0 + // if (Platform == 'darwin') + // throw 0 // Run the cqlsh's executing command in the main process this.command(command.replace(/\-SPLIT\-/g, '') + keepTempArgument)