index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const ansiEscapes = require('ansi-escapes');
  3. const cliCursor = require('cli-cursor');
  4. const wrapAnsi = require('wrap-ansi');
  5. const getWidth = stream => {
  6. const columns = stream.columns;
  7. if (!columns) {
  8. return 80;
  9. }
  10. // Windows appears to wrap a character early
  11. // I hate Windows so much
  12. if (process.platform === 'win32') {
  13. return columns - 1;
  14. }
  15. return columns;
  16. };
  17. const main = (stream, options) => {
  18. options = Object.assign({
  19. showCursor: false
  20. }, options);
  21. let prevLineCount = 0;
  22. const render = function () {
  23. if (!options.showCursor) {
  24. cliCursor.hide();
  25. }
  26. let out = [].join.call(arguments, ' ') + '\n';
  27. out = wrapAnsi(out, getWidth(stream), {
  28. trim: false,
  29. hard: true,
  30. wordWrap: false
  31. });
  32. stream.write(ansiEscapes.eraseLines(prevLineCount) + out);
  33. prevLineCount = out.split('\n').length;
  34. };
  35. render.clear = () => {
  36. stream.write(ansiEscapes.eraseLines(prevLineCount));
  37. prevLineCount = 0;
  38. };
  39. render.done = () => {
  40. prevLineCount = 0;
  41. if (!options.showCursor) {
  42. cliCursor.show();
  43. }
  44. };
  45. return render;
  46. };
  47. module.exports = main(process.stdout);
  48. module.exports.stderr = main(process.stderr);
  49. module.exports.create = main;