JAVASCRIPT   36

clear.js

Guest on 10th October 2021 06:43:45 AM

  1. 'use strict';
  2.  
  3. const strip = require('./strip');
  4. const { erase, cursor } = require('sisteransi');
  5.  
  6. const width = str => [...strip(str)].length;
  7.  
  8. module.exports = function(prompt, perLine = process.stdout.columns) {
  9.   if (!perLine) return erase.line + cursor.to(0);
  10.  
  11.   let rows = 0;
  12.   const lines = prompt.split(/\r?\n/);
  13.   for (let line of lines) {
  14.     rows += 1 + Math.floor(Math.max(width(line) - 1, 0) / perLine);
  15.   }
  16.  
  17.   return (erase.line + cursor.prevLine()).repeat(rows - 1) + erase.line + cursor.to(0);
  18. };

Raw Paste


Login or Register to edit or fork this paste. It's free.