diff --git a/bin/stdio.js b/bin/stdio.js index e0e1d9d..3b1ebcc 100644 --- a/bin/stdio.js +++ b/bin/stdio.js @@ -15,6 +15,8 @@ const rl = readline.createInterface({ prompt: '' }) +let currentWindow = null + let send = _ => _ const setSend = fn => { @@ -29,8 +31,24 @@ const onInit = () => { rl.resume() } -const onUpdateContent = messages => - messages.text.forEach(({ append, content }) => { +const onUpdateWindows = windows => { + currentWindow = windows + .filter(x => x.type === 'buffer') + .slice(-1)[0] +} + +const onUpdateInputs = type => { + type + ? attach_handlers(type) + : detach_handlers() +} + +const onUpdateContent = allMessages => { + const messages = allMessages.filter( + content => content.id === currentWindow.id + )[0] + + return messages.text.forEach(({ append, content }) => { if (!append) { stdout.write('\n') } @@ -53,17 +71,6 @@ const onUpdateContent = messages => }) } }) - -const onUpdateInputs = type => { - type - ? attach_handlers(type) - : detach_handlers() -} - -const onExit = () => { - detach_handlers() - rl.close() - stdout.write('\n') } const onDisable = disable => @@ -71,6 +78,12 @@ const onDisable = disable => ? detach_handlers() : attach_handlers() +const onExit = () => { + detach_handlers() + rl.close() + stdout.write('\n') +} + const onFileNameRequest = (tosave, usage, gameid, callback) => { stdout.write('\n') rl.question( @@ -104,7 +117,7 @@ const handle_char_input = (str, key) => { str || key.name.replace(/f(\d+)/, 'func$1') - send(res) + send(res, currentWindow) } const attach_handlers = type => { @@ -129,14 +142,15 @@ const handle_line_input = line => { } detach_handlers() - send(line) + send(line, currentWindow) } module.exports.handlers = { onInit, + onUpdateWindows, + onUpdateInputs, onUpdateContent, onDisable, - onUpdateInputs, onFileNameRequest, onFileRead, onFileWrite, diff --git a/src/cheapGlkOte.js b/src/cheapGlkOte.js index 73c9657..fbcb931 100644 --- a/src/cheapGlkOte.js +++ b/src/cheapGlkOte.js @@ -8,28 +8,20 @@ class CheapGlkOte extends GlkOte { constructor(handlers, loggers) { super() - this.window = null this.current_input_type = null this.handlers = handlers } - sendFn(message) { + sendFn(message, window) { this.send_response( this.current_input_type, - this.window, + window, message) this.current_input_type = null } init(iface) { - /* Only one window can be opened */ - const glk_window_open = iface.Glk.glk_window_open - iface.Glk.glk_window_open = (splitwin, ...args) => - splitwin - ? null - : glk_window_open(splitwin, ...args) - this.handlers.onInit() super.init(iface) } @@ -62,11 +54,7 @@ class CheapGlkOte extends GlkOte { } update_content(messages) { - const filtered = messages.filter( - content => content.id === this.window.id - )[0] - - this.handlers.onUpdateContent(filtered) + this.handlers.onUpdateContent(messages) } exit() { @@ -86,12 +74,10 @@ class CheapGlkOte extends GlkOte { this.handlers.onDisable(disable) } - update_windows(data) { - data.forEach(win => { - if (win.type === 'buffer') { - this.window = win - } - }) + update_windows(windows) { + if (windows.length) { + this.handlers.onUpdateWindows(windows) + } } log(message) { diff --git a/src/index.js b/src/index.js index 6e4e85c..b832a4d 100644 --- a/src/index.js +++ b/src/index.js @@ -5,9 +5,10 @@ const noop = () => void null const noopHandlers = [ 'onInit', + 'onUpdateWindows', + 'onUpdateInputs', 'onUpdateContent', 'onDisable', - 'onUpdateInputs', 'onFileNameRequest', 'onFileRead', 'onFileWrite',