« get me outta code hell

mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xindex.js25
-rw-r--r--ui.js5
2 files changed, 26 insertions, 4 deletions
diff --git a/index.js b/index.js
index 6a71611..578fdc3 100755
--- a/index.js
+++ b/index.js
@@ -51,12 +51,26 @@ async function main() {
     process.exit(1)
   }
 
-  appElement.on('quitRequested', () => {
+  const cleanTerminal = () => {
     process.stdout.write(ansi.cleanCursor())
     process.stdout.write(ansi.disableAlternateScreen())
+  }
+
+  const dirtyTerminal = () => {
+    process.stdout.write(ansi.enableAlternateScreen())
+    process.stdout.write(ansi.startTrackingMouse())
+  }
+
+  appElement.on('quitRequested', () => {
+    cleanTerminal()
     process.exit(0)
   })
 
+  appElement.on('suspendRequested', () => {
+    cleanTerminal()
+    process.kill(process.pid, 'SIGTSTP')
+  })
+
   let grouplike = {
     name: 'My ~/Music Library',
     comment: (
@@ -77,8 +91,13 @@ async function main() {
   root.h = size.height
   root.fixAllLayout()
 
-  process.stdout.write(ansi.enableAlternateScreen())
-  process.stdout.write(ansi.startTrackingMouse())
+  dirtyTerminal()
+  process.on('SIGCONT', () => {
+    flushable.resizeScreen({lines: flushable.screenLines, cols: flushable.screenCols})
+    process.stdin.setRawMode(false)
+    process.stdin.setRawMode(true)
+    dirtyTerminal()
+  })
 
   const flushable = new Flushable(process.stdout, true)
   flushable.resizeScreen(size)
diff --git a/ui.js b/ui.js
index eb4a0a9..d9b652d 100644
--- a/ui.js
+++ b/ui.js
@@ -413,9 +413,12 @@ class AppElement extends FocusElement {
   }
 
   keyPressed(keyBuf) {
-    if (keyBuf[0] === 0x03) {
+    if (keyBuf[0] === 0x03) { // Ctrl-C
       this.shutdown()
       return
+    } else if (keyBuf[0] === 0x1a) { // Ctrl-Z
+      this.emit('suspendRequested')
+      return
     }
 
     if (telc.isRight(keyBuf)) {