From 6ea74c268a12325296a1d2e7fc31b02030ddb8bc Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 12 May 2023 17:42:09 -0300 Subject: use ESM module syntax & minor cleanups The biggest change here is moving various element classes under more scope-specific directories, which helps to avoid circular dependencies and is just cleaner to navigate and expand in the future. Otherwise this is a largely uncritical port to ESM module syntax! There are probably a number of changes and other cleanups that remain much needed. Whenever I make changes to tui-lib it's hard to believe it's already been years since the previous time. First commits are from January 2017, and the code originates a month earlier in KAaRMNoD! --- examples/telnet-interface.js | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 examples/telnet-interface.js (limited to 'examples/telnet-interface.js') diff --git a/examples/telnet-interface.js b/examples/telnet-interface.js new file mode 100644 index 0000000..319786f --- /dev/null +++ b/examples/telnet-interface.js @@ -0,0 +1,59 @@ +// Telnet demo: +// - Basic telnet socket handling using the TelnetInterface +// - Handling client's screen size +// - Handling socket being closed by client +// - Handling cleanly closing the socket by hand + +import net from 'node:net' + +import {Root} from 'tui-lib/ui/primitives' + +import {TelnetInterface} from 'tui-lib/util/interfaces' + +import AppElement from './basic-app.js' + +const server = new net.Server(socket => { + const telnetInterface = new TelnetInterface(socket) + + telnetInterface.getScreenSize().then(size => { + const root = new Root(telnetInterface) + root.w = size.width + root.h = size.height + + telnetInterface.on('resize', newSize => { + root.w = newSize.width + root.h = newSize.height + root.fixAllLayout() + }) + + const appElement = new AppElement() + root.addChild(appElement) + root.select(appElement) + + let closed = false + + appElement.on('quitRequested', () => { + if (!closed) { + telnetInterface.cleanTelnetOptions() + socket.write('Goodbye!\n') + socket.end() + clearInterval(interval) + closed = true + } + }) + + socket.on('close', () => { + if (!closed) { + clearInterval(interval) + closed = true + } + }) + + const interval = setInterval(() => root.render(), 100) + }).catch(error => { + console.error(error) + process.exit(1) + }) +}) + +server.listen(8008) -- cgit 1.3.0-6-gf8a5