stdio: Update the stdio implementation

This commit is contained in:
He4eT 2021-07-18 23:33:28 +05:00
commit 38e8e34ba8

View file

@ -16,6 +16,7 @@ const rl = readline.createInterface({
}) })
let currentWindow = null let currentWindow = null
let currentInputType = null
let send = _ => _ let send = _ => _
@ -37,13 +38,18 @@ const onUpdateWindows = windows => {
.slice(-1)[0] .slice(-1)[0]
} }
const onUpdateInputs = type => { const onUpdateInputs = data => {
type const { type } = data[0]
? attach_handlers(type)
: detach_handlers() if (['char', 'line'].includes(type)) {
detach_handlers()
attach_handlers(type)
}
} }
const onUpdateContent = allMessages => { const onUpdateContent = allMessages => {
detach_handlers()
const messages = allMessages.filter( const messages = allMessages.filter(
content => content.id === currentWindow.id content => content.id === currentWindow.id
)[0] )[0]
@ -73,10 +79,9 @@ const onUpdateContent = allMessages => {
}) })
} }
const onDisable = disable => const onDisable = disable => {
disable if (disable) detach_handlers()
? detach_handlers() }
: attach_handlers()
const onExit = () => { const onExit = () => {
detach_handlers() detach_handlers()
@ -105,8 +110,6 @@ const handle_char_input = (str, key) => {
'\t': 'tab', '\t': 'tab',
} }
detach_handlers()
// Make sure this char isn't being remembered for the next line input // Make sure this char isn't being remembered for the next line input
rl._line_buffer = null rl._line_buffer = null
rl.line = '' rl.line = ''
@ -117,10 +120,12 @@ const handle_char_input = (str, key) => {
str || str ||
key.name.replace(/f(\d+)/, 'func$1') key.name.replace(/f(\d+)/, 'func$1')
send(res, currentWindow) send(res, currentInputType, currentWindow)
detach_handlers()
} }
const attach_handlers = type => { const attach_handlers = type => {
currentInputType = type
if (type === 'char') { if (type === 'char') {
stdout.mute() stdout.mute()
stdin.on('keypress', handle_char_input) stdin.on('keypress', handle_char_input)
@ -134,15 +139,15 @@ const detach_handlers = () => {
stdin.removeListener('keypress', handle_char_input) stdin.removeListener('keypress', handle_char_input)
rl.removeListener('line', handle_line_input) rl.removeListener('line', handle_line_input)
stdout.unmute() stdout.unmute()
currentInputType = null
} }
const handle_line_input = line => { const handle_line_input = line => {
if (stdout.isTTY) { if (stdout.isTTY) {
stdout.write(ansiEscapes.eraseLines(1)) stdout.write(ansiEscapes.eraseLines(1))
} }
send(line, currentInputType, currentWindow)
detach_handlers() detach_handlers()
send(line, currentWindow)
} }
module.exports.handlers = { module.exports.handlers = {