« get me outta code hell

Separate backend from UI - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
path: root/index.js
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-07-05 21:10:32 -0300
committerFlorrie <towerofnix@gmail.com>2019-07-05 22:02:46 -0300
commit08a3150fe9c9427bdb9e46d0cde14ad30747008d (patch)
treea0ad6e20e9745997038d4d57821083ae7f21e2e5 /index.js
parentc2a89f91684df916aaf8f66eab33280ad99b6b1b (diff)
Separate backend from UI
Diffstat (limited to 'index.js')
-rwxr-xr-xindex.js47
1 files changed, 35 insertions, 12 deletions
diff --git a/index.js b/index.js
index 578fdc3..23d0449 100755
--- a/index.js
+++ b/index.js
@@ -2,19 +2,31 @@
 
 // omg I am tired of code
 
-const { AppElement } = require('./ui')
-const { updatePlaylistFormat } = require('./playlist-utils')
 const { getAllCrawlersForArg } = require('./crawlers')
-const fs = require('fs')
-const util = require('util')
+const AppElement = require('./ui')
+const Backend = require('./backend')
 const processSmartPlaylist = require('./smart-playlist')
-const ansi = require('./tui-lib/util/ansi')
-const CommandLineInterfacer = require('./tui-lib/util/CommandLineInterfacer')
-const EventEmitter = require('events')
-const Flushable = require('./tui-lib/util/Flushable')
-const Root = require('./tui-lib/ui/Root')
 
-const readFile = util.promisify(fs.readFile)
+const {
+  getItemPathString,
+  updatePlaylistFormat
+} = require('./playlist-utils')
+
+const {
+  ui: {
+    Root
+  },
+  util: {
+    ansi,
+    CommandLineInterfacer,
+    Flushable
+  }
+} = require('./tui-lib')
+
+const { promisify } = require('util')
+const fs = require('fs')
+const readFile = promisify(fs.readFile)
+const writeFile = promisify(fs.writeFile)
 
 // Hack to get around errors when piping many things to stdout/err
 // (from general-util promisifyProcess)
@@ -40,11 +52,22 @@ async function main() {
 
   const root = new Root(interfacer)
 
-  const appElement = new AppElement()
+  const backend = new Backend()
+
+  backend.on('playing', track => {
+    if (track) {
+      writeFile(backend.rootDirectory + '/current-track.txt',
+        getItemPathString(track))
+      writeFile(backend.rootDirectory + '/current-track.json',
+        JSON.stringify(track, null, 2))
+    }
+  })
+
+  const appElement = new AppElement(backend)
   root.addChild(appElement)
   root.select(appElement)
 
-  const result = await appElement.setup()
+  const result = await backend.setup()
 
   if (result.error) {
     console.error(result.error)