« get me outta code hell

Experimental telnet server - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/client.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-07-06 00:16:02 -0300
committerFlorrie <towerofnix@gmail.com>2019-07-06 11:23:55 -0300
commit8512c44b6403c126bf961c0fd0c2798d6bfdfcea (patch)
tree626ac64af2fb889fd7ec1a1032d14df5a87d4ad6 /client.js
parentf96eeceefc94256babe016b0e18b95f0cf467880 (diff)
Experimental telnet server
Diffstat (limited to 'client.js')
-rw-r--r--client.js88
1 files changed, 88 insertions, 0 deletions
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