index.js 907 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. exports = module.exports = cliWidth;
  3. function normalizeOpts(options) {
  4. let defaultOpts = {
  5. defaultWidth: 0,
  6. output: process.stdout,
  7. tty: require("tty"),
  8. };
  9. if (!options) {
  10. return defaultOpts;
  11. }
  12. Object.keys(defaultOpts).forEach(function (key) {
  13. if (!options[key]) {
  14. options[key] = defaultOpts[key];
  15. }
  16. });
  17. return options;
  18. }
  19. function cliWidth(options) {
  20. let opts = normalizeOpts(options);
  21. if (opts.output.getWindowSize) {
  22. return opts.output.getWindowSize()[0] || opts.defaultWidth;
  23. }
  24. if (opts.tty.getWindowSize) {
  25. return opts.tty.getWindowSize()[1] || opts.defaultWidth;
  26. }
  27. if (opts.output.columns) {
  28. return opts.output.columns;
  29. }
  30. if (process.env.CLI_WIDTH) {
  31. let width = parseInt(process.env.CLI_WIDTH, 10);
  32. if (!isNaN(width) && width !== 0) {
  33. return width;
  34. }
  35. }
  36. return opts.defaultWidth;
  37. }