« get me outta code hell

Add compression statistic monitor to Flushable - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/util
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2017-12-10 00:20:17 -0400
committerFlorrie <towerofnix@gmail.com>2017-12-10 00:20:17 -0400
commita0c864e531cc793855f0d3462d9f9ef2aca7affa (patch)
tree98d08b0d53db7426beb186f5bd4517195257d4a1 /util
parent1de4c9c10530eb6efbb9af06d5a2cf881ccd85af (diff)
Add compression statistic monitor to Flushable
Diffstat (limited to 'util')
-rw-r--r--util/Flushable.js22
1 files changed, 13 insertions, 9 deletions
diff --git a/util/Flushable.js b/util/Flushable.js
index b031677..2248fec 100644
--- a/util/Flushable.js
+++ b/util/Flushable.js
@@ -11,6 +11,10 @@ module.exports = class Flushable {
     // doesn't *quite* work but should drastically decrease write size?
     this.shouldCompress = shouldCompress
 
+    // Whether or not to show compression statistics (original written size
+    // and ANSI-interpreted compressed size) in the output of flush.
+    this.shouldShowCompressionStatistics = false
+
     // Update these if you plan on using the ANSI compressor!
     this.screenLines = 24
     this.screenCols = 80
@@ -82,15 +86,15 @@ module.exports = class Flushable {
 
   compress(toWrite) {
     // TODO: customize screen size
-    const screen = ansi.interpret(toWrite, this.screenLines, this.screenCols)
-
-    /*
-    const pcSaved = Math.round(100 - (100 / toWrite.length * screen.length))
-    console.log(
-      '\x1b[1A' +
-      `${toWrite.length} - ${screen.length} ${pcSaved}% saved   `
-    )
-    */
+    let screen = ansi.interpret(toWrite, this.screenLines, this.screenCols)
+
+    if (this.shouldShowCompressionStatistics) {
+      const pcSaved = Math.round(100 - (100 / toWrite.length * screen.length))
+      screen += (
+        '\x1b[1A (ANSI-interpret: ' +
+        `${toWrite.length} -> ${screen.length} ${pcSaved}% saved)`
+      )
+    }
 
     return screen
   }