clear.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const strip = require('./strip');
  3. var _require = require('sisteransi');
  4. const erase = _require.erase,
  5. cursor = _require.cursor;
  6. const width = str => [...strip(str)].length;
  7. module.exports = function (prompt, perLine = process.stdout.columns) {
  8. if (!perLine) return erase.line + cursor.to(0);
  9. let rows = 0;
  10. const lines = prompt.split(/\r?\n/);
  11. var _iteratorNormalCompletion = true;
  12. var _didIteratorError = false;
  13. var _iteratorError = undefined;
  14. try {
  15. for (var _iterator = lines[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  16. let line = _step.value;
  17. rows += 1 + Math.floor(Math.max(width(line) - 1, 0) / perLine);
  18. }
  19. } catch (err) {
  20. _didIteratorError = true;
  21. _iteratorError = err;
  22. } finally {
  23. try {
  24. if (!_iteratorNormalCompletion && _iterator.return) {
  25. _iterator.return();
  26. }
  27. } finally {
  28. if (_didIteratorError) {
  29. throw _iteratorError;
  30. }
  31. }
  32. }
  33. return (erase.line + cursor.prevLine()).repeat(rows - 1) + erase.line + cursor.to(0);
  34. };