« get me outta code hell

Less-buggy resize detection - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2018-10-08 11:36:47 -0300
committerFlorrie <towerofnix@gmail.com>2018-10-08 11:36:49 -0300
commit4207cf479f91d4cad9cad5583d069efc8fd24c31 (patch)
tree0700cee83f0581265e93c5d18bf2a6b64ac16375
parent5f00f624a647fbf7838a5ff1c8d72156ae0ad38f (diff)
Less-buggy resize detection
Moved .on('resize') to be after the flushable is actually created, so we
don't get an error (flushable is not defined) when resizing before the
flushable has been created. Also checked for size only immediately
before we actually display, so that the size is accurate right away.
-rwxr-xr-xindex.js23
1 files changed, 13 insertions, 10 deletions
diff --git a/index.js b/index.js
index 3d7adf8..9682e95 100755
--- a/index.js
+++ b/index.js
@@ -28,18 +28,8 @@ process.on('unhandledRejection', error => {
 
 async function main() {
   const interfacer = new CommandLineInterfacer()
-  const size = await interfacer.getScreenSize()
 
   const root = new Root(interfacer)
-  root.w = size.width
-  root.h = size.height
-
-  interfacer.on('resize', newSize => {
-    root.w = newSize.width
-    root.h = newSize.height
-    flushable.resizeScreen(newSize)
-    root.fixAllLayout()
-  })
 
   const appElement = new AppElement()
   root.addChild(appElement)
@@ -71,12 +61,25 @@ async function main() {
 
   root.select(appElement)
 
+  // Check size, now that we're about to display.
+  const size = await interfacer.getScreenSize()
+  root.w = size.width
+  root.h = size.height
+  root.fixAllLayout()
+
   const flushable = new Flushable(process.stdout, true)
   flushable.resizeScreen(size)
   flushable.shouldShowCompressionStatistics = process.argv.includes('--show-ansi-stats')
   flushable.write(ansi.clearScreen())
   flushable.flush()
 
+  interfacer.on('resize', newSize => {
+    root.w = newSize.width
+    root.h = newSize.height
+    flushable.resizeScreen(newSize)
+    root.fixAllLayout()
+  })
+
   setInterval(() => {
     root.renderTo(flushable)
     flushable.flush()