From 8512c44b6403c126bf961c0fd0c2798d6bfdfcea Mon Sep 17 00:00:00 2001 From: Florrie Date: Sat, 6 Jul 2019 00:16:02 -0300 Subject: Experimental telnet server --- client.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 client.js (limited to 'client.js') diff --git a/client.js b/client.js new file mode 100644 index 0000000..7f21d1a --- /dev/null +++ b/client.js @@ -0,0 +1,88 @@ +// Generic code for setting up mtui and the UI for any command line client. + +'use strict' + +const AppElement = require('./ui') +const processSmartPlaylist = require('./smart-playlist') + +const { + ui: { + Root + }, + util: { + ansi, + Flushable, + TelnetInterfacer + } +} = require('./tui-lib') + +const setupClient = async ({backend, writable, interfacer, appConfig}) => { + const cleanTerminal = () => { + writable.write(ansi.cleanCursor()) + writable.write(ansi.disableAlternateScreen()) + } + + const dirtyTerminal = () => { + writable.write(ansi.enableAlternateScreen()) + writable.write(ansi.startTrackingMouse()) + } + + dirtyTerminal() + + const root = new Root(interfacer) + + const size = await interfacer.getScreenSize() + root.w = size.width + root.h = size.height + root.fixAllLayout() + + const flushable = new Flushable(writable, true) + flushable.resizeScreen(size) + flushable.write(ansi.clearScreen()) + flushable.flush() + + interfacer.on('resize', newSize => { + root.w = newSize.width + root.h = newSize.height + flushable.resizeScreen(newSize) + root.fixAllLayout() + }) + + const appElement = new AppElement(backend, appConfig) + root.addChild(appElement) + root.select(appElement) + + appElement.on('quitRequested', () => { + cleanTerminal() + }) + + appElement.on('suspendRequested', () => { + cleanTerminal() + }) + + // TODO: Don't load a default playlist? + let grouplike = { + name: 'My ~/Music Library', + comment: ( + '(Add songs and folders to ~/Music to make them show up here,' + + ' or pass mtui your own playlist.json file!)'), + source: ['crawl-local', process.env.HOME + '/Music'] + } + grouplike = await processSmartPlaylist(grouplike) + appElement.tabber.currentElement.loadGrouplike(grouplike) + + root.select(appElement) + + // Load up initial state + appElement.queueListingElement.buildItems() + appElement.playbackInfoElement.updateTrack(backend.playingTrack) + + const renderInterval = setInterval(() => { + root.renderTo(flushable) + flushable.flush() + }, 100) + + return {appElement, cleanTerminal, renderInterval} +} + +module.exports = setupClient -- cgit 1.3.0-6-gf8a5