Issue
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. 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
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. 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.
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:
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.
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
How do i fix this issue?
This is what my package.json looks like:
{
"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"
}
}
- My NodeJS version: v16.11.1
- My NPM version: 8.1.2
- My Linux Distro: Arch Linux with XFCE 4
I tried switching to nodejs version 14.18.1-1, but that didn't help too
Solution
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
Answered By - DEVLOPR