clear.js 489 B

12345678910111213141516171819202122
  1. 'use strict';
  2. const strip = require('./strip');
  3. const { erase, cursor } = require('sisteransi');
  4. const width = str => [...strip(str)].length;
  5. /**
  6. * @param {string} prompt
  7. * @param {number} perLine
  8. */
  9. module.exports = function(prompt, perLine) {
  10. if (!perLine) return erase.line + cursor.to(0);
  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. return erase.lines(rows);
  17. };