tabby/terminus-terminal/src/bufferizedPTY.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-03-07 01:05:26 +00:00
/** @hidden */
module.exports = function patchPTYModule (mod) {
2018-03-28 22:25:57 +00:00
const oldSpawn = mod.spawn
if (mod.patched) {
2019-05-24 18:02:22 +00:00
return
}
mod.patched = true
2018-03-28 22:25:57 +00:00
mod.spawn = (file, args, opt) => {
let terminal = oldSpawn(file, args, opt)
let timeout = null
let buffer = ''
let lastFlush = 0
let nextTimeout = 0
2018-11-10 20:10:47 +00:00
// Minimum prebuffering window (ms) if the input is non-stop flowing
const minWindow = 10
// Maximum buffering time (ms) until output must be flushed unconditionally
const maxWindow = 100
2018-03-28 22:25:57 +00:00
function flush () {
if (buffer) {
terminal.emit('data-buffered', buffer)
}
lastFlush = Date.now()
buffer = ''
}
function reschedule () {
if (timeout) {
clearTimeout(timeout)
}
nextTimeout = Date.now() + minWindow
timeout = setTimeout(() => {
timeout = null
flush()
}, minWindow)
}
terminal.on('data', data => {
buffer += data
if (Date.now() - lastFlush > maxWindow) {
2018-11-10 20:10:47 +00:00
// Taking too much time buffering, flush to keep things interactive
2018-03-28 22:25:57 +00:00
flush()
} else {
2018-11-10 20:10:47 +00:00
if (Date.now() > nextTimeout - (maxWindow / 10)) {
// Extend the window if it's expiring
2018-03-28 22:25:57 +00:00
reschedule()
}
}
})
return terminal
}
}