index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. const path = require('path');
  3. const execa = require('execa');
  4. const create = (columns, rows) => ({
  5. columns: parseInt(columns, 10),
  6. rows: parseInt(rows, 10)
  7. });
  8. module.exports = () => {
  9. const env = process.env;
  10. const stdout = process.stdout;
  11. const stderr = process.stderr;
  12. if (stdout && stdout.columns && stdout.rows) {
  13. return create(stdout.columns, stdout.rows);
  14. }
  15. if (stderr && stderr.columns && stderr.rows) {
  16. return create(stderr.columns, stderr.rows);
  17. }
  18. // These values are static, so not the first choice
  19. if (env.COLUMNS && env.LINES) {
  20. return create(env.COLUMNS, env.LINES);
  21. }
  22. if (process.platform === 'win32') {
  23. try {
  24. // Binary: https://github.com/sindresorhus/win-term-size
  25. const size = execa.sync(path.join(__dirname, 'vendor/windows/term-size.exe')).stdout.split(/\r?\n/);
  26. if (size.length === 2) {
  27. return create(size[0], size[1]);
  28. }
  29. } catch (err) {}
  30. } else {
  31. if (process.platform === 'darwin') {
  32. try {
  33. // Binary: https://github.com/sindresorhus/macos-term-size
  34. const size = execa.shellSync(path.join(__dirname, 'vendor/macos/term-size')).stdout.split(/\r?\n/);
  35. if (size.length === 2) {
  36. return create(size[0], size[1]);
  37. }
  38. } catch (err) {}
  39. }
  40. // `resize` is preferred as it works even when all file descriptors are redirected
  41. // https://linux.die.net/man/1/resize
  42. try {
  43. const size = execa.sync('resize', ['-u']).stdout.match(/\d+/g);
  44. if (size.length === 2) {
  45. return create(size[0], size[1]);
  46. }
  47. } catch (err) {}
  48. try {
  49. const columns = execa.sync('tput', ['cols']).stdout;
  50. const rows = execa.sync('tput', ['lines']).stdout;
  51. if (columns && rows) {
  52. return create(columns, rows);
  53. }
  54. } catch (err) {}
  55. }
  56. return create(80, 24);
  57. };