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:
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"
}
}
from Node.JS - Backspace not working in Node-Pty Terminal

No comments:
Post a Comment