[英]Node JS - Backspace not working in Node-Pty Terminal
I have this simple code我有这个简单的代码
const os = require('os')
const pty = require('node-pty')
const process = require('process')
const { msleep } = require('sleep');
const { readFileSync, writeFileSync } = require('fs');
const { exit } = require('process');
const usage = `Usage: term-record [OPTION]
OPTION:
play [Filename] Play a recorded .json file
record [Filename] Record your terminal session to a .json file`
var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'
var lastRecordTimestamp = null
var recording = []
var args = process.argv
args.splice(0, 2)
function getDuration() {
var now = new Date().getMilliseconds()
var duration = now - lastRecordTimestamp
lastRecordTimestamp = new Date().getMilliseconds()
return duration
}
function play(filename) {
try {
var data = readFileSync(filename, { encoding: 'utf8', flag: 'r'})
} catch (err) {
if (err.code == 'ENOENT') {
console.error("Error: File Not Found!")
exit(1)
} else {
console.error(err)
exit(1)
}
}
try {
data = JSON.parse(data)
} catch (err) {
console.error("Error: Invalid File!");
exit(1)
}
console.log("------------ STARTING ------------");
for (let i = 0; i < data.length; i++) {
process.stdout.write(data[i].content);
msleep(data[i].delay)
}
console.log("-------------- END ---------------");
}
function record(filename) {
var ptyProcess = pty.spawn(shell, [], {
name: 'TermRecord Session',
cols: process.stdout.columns,
rows: process.stdout.rows,
cwd: process.env.HOME,
env: process.env
});
process.stdout.setDefaultEncoding('utf8');
process.stdin.setEncoding('utf8')
process.stdin.setRawMode(true)
process.stdin.resume();
ptyProcess.on('data', function(data) {
process.stdout.write(data)
var duration = getDuration();
if (duration < 5) {
duration = 100
}
recording.push({
delay: Math.abs(duration),
content: data
});
});
ptyProcess.on('exit', () => {
process.stdin.setRawMode(false);
process.stdin.pause()
recording[0].delay = 1000
try {
writeFileSync(filename, JSON.stringify(recording, null, '\t')); // JSON.stringify(recording, null, '\t') For Tabs
} catch (err) {
console.log(err);
}
})
var onInput = ptyProcess.write.bind(ptyProcess)
process.stdin.on('data', onInput)
}
if (args.length === 2) {
var file = args[1]
if (args[0] == "record") {
console.info("Setting App Mode to 'Record'")
console.info("Setting Output file To '" + file + "'")
record(file)
}
if (args[0] == "play") {
console.info("Setting App Mode to 'Play'")
console.info("Setting Input file To '" + file + "'")
play(file)
}
} else {
console.log(usage);
}
The record
function takes a argument filename
and then starts a new terminal using node-pty module, and when on data
event occurs it simply calculates the the milliseconds from the last time this on data
event triggered, and pushes a object into recording
array, and this object has two properties, 1st is delay, and second is the text. record
函数接受一个参数filename
,然后使用 node-pty 模块启动一个新终端,当发生on data
事件时,它简单地计算距离上次触发on data
事件的毫秒数,并将对象推入recording
数组,以及这个对象有两个属性,第一个是延迟,第二个是文本。 and when the on exit
event triggers, it simply closes the terminal and saves the recording
array to a json file with name equal to the variable filename
当
on exit
事件触发时,它只是关闭终端并将recording
数组保存到一个名称等于变量filename
的 json 文件中
The play
function takes a argument filename
and then reads the data from the file and parses it to a JavaScript Array which contains multiple objects, and if something goes wrong it throws an error. play
函数接受一个参数filename
,然后从文件中读取数据并将其解析为一个包含多个对象的 JavaScript 数组,如果出现问题,它会抛出一个错误。 after parsing it simply uses a for loop to iterate over the array and writes the data to the console and waits for some milliseconds.解析后,它只是使用 for 循环遍历数组并将数据写入控制台并等待几毫秒。
Problem is, when I record my session, and when i press Backspace
key to remove a character, then it weirdly puts a space between it, like shown below:问题是,当我记录我的会话时,当我按
Backspace
键删除一个字符时,它奇怪地在它之间放置了一个空格,如下所示:
In the gif, after I ran the first command and typed out ls -ls
then i pressed Backspace 2 Times, which resulted a weird blank space 2 times.在 gif 中,在我运行第一个命令并输入
ls -ls
之后,我按了 2 次Backspace ,这导致了 2 次奇怪的空格。 and after i pressed enter it showed a error ls: cannot access '-': No such file or directory
which meant the Backspace key removed 2 characters from the input but and it executed ls -
instead of ls -ls
but for some reason those 2 characters were not removed from the console when i pressed Backspace twice instead it added a weird blank space在我按下回车键后,它显示了一个错误
ls: cannot access '-': No such file or directory
这意味着Backspace键从输入中删除了 2 个字符,但它执行了ls -
而不是ls -ls
但出于某种原因那些 2当我按两次Backspace时,没有从控制台中删除字符,而是添加了一个奇怪的空格
How do i fix this issue?我该如何解决这个问题?
This is what my package.json looks like:这是我的 package.json 的样子:
{
"name": "term-record",
"version": "0.0.1",
"description": "A Simple Terminal Session Recorder",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js"
},
"author": "ADITYA MISHRA",
"license": "MIT",
"dependencies": {
"node-pty": "^0.10.1",
"sleep": "^6.3.0"
}
}
I tried switching to nodejs version 14.18.1-1, but that didn't help too我尝试切换到 nodejs 版本 14.18.1-1,但这也无济于事
Since i had my keyboard layout poorly selected, which caused the backspace key to add a space.由于我的键盘布局选择不当,导致退格键添加了一个空格。
I ran the following command setxkbmap -layout us
, to change my keyboard layout to US and now it works我运行了以下命令
setxkbmap -layout us
,将我的键盘布局更改为美国,现在它可以工作了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.