« get me outta code hell

Merge branch 'master' of ssh://ed1.club/home/florrie/git/tui-lib - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-04-02 08:19:45 -0300
committerFlorrie <towerofnix@gmail.com>2019-04-02 08:19:45 -0300
commitdff1213ae0069afcc0aa1b87daa6658732f742b8 (patch)
tree371ab6a99ba3928bdb52210dd2c1d7eb77c35917
parentdf0e9afe7aedd3dc6a1d0db72530a52e135af649 (diff)
parent72f417acb5543f24f1de937cef455d8be8add917 (diff)
Merge branch 'master' of ssh://ed1.club/home/florrie/git/tui-lib
-rw-r--r--util/Flushable.js4
-rw-r--r--util/tui-app.js31
2 files changed, 29 insertions, 6 deletions
diff --git a/util/Flushable.js b/util/Flushable.js
index 318bb52..058d186 100644
--- a/util/Flushable.js
+++ b/util/Flushable.js
@@ -31,6 +31,10 @@ module.exports = class Flushable {
   resizeScreen({lines, cols}) {
     this.screenLines = lines
     this.screenCols = cols
+    this.clearLastFrame()
+  }
+
+  clearLastFrame() {
     this.lastFrame = undefined
   }
 
diff --git a/util/tui-app.js b/util/tui-app.js
index 7ee0480..0b845ea 100644
--- a/util/tui-app.js
+++ b/util/tui-app.js
@@ -28,18 +28,36 @@ module.exports = async function tuiApp(callback) {
         root.fixAllLayout();
     });
 
-    const clean = function() {
+    const cleanTerminal = function () {
         process.stdout.write(ansi.cleanCursor());
         process.stdout.write(ansi.disableAlternateScreen());
     };
 
-    const quitProgram = function(status = 0) {
-        clean();
+    const dirtyTerminal = function () {
+        process.stdout.write(ansi.enableAlternateScreen());
+        process.stdout.write(ansi.startTrackingMouse());
+    };
+
+    const quitProgram = function (status = 0) {
+        cleanTerminal();
         process.exit(status);
     };
 
-    const startRenderLoop = function() {
-        process.stdout.write(ansi.enableAlternateScreen());
+    const suspendProgram = function () {
+        cleanTerminal();
+        process.kill(process.pid, 'SIGTSTP');
+    };
+
+    const startRenderLoop = function () {
+        dirtyTerminal();
+
+        process.on('SIGCONT', () => {
+            flushable.clearLastFrame();
+            process.stdin.setRawMode(false);
+            process.stdin.setRawMode(true);
+            dirtyTerminal();
+        });
+
         setInterval(() => {
             root.renderTo(flushable);
             flushable.flush();
@@ -50,10 +68,11 @@ module.exports = async function tuiApp(callback) {
         return await callback({
             root,
             startRenderLoop,
+            suspendProgram,
             quitProgram
         });
     } catch (error) {
-        clean();
+        cleanTerminal();
         console.error(error);
         process.exit(1);
     };