resize.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var c = require('../')();
  2. c.pipe(process.stdout);
  3. c.on('^C', process.exit);
  4. var queue = (function () {
  5. var tasks = [];
  6. var pending = false;
  7. return {
  8. abort : function () {
  9. tasks = [];
  10. next();
  11. },
  12. push : function (t) {
  13. tasks.push(t);
  14. if (!pending) next();
  15. }
  16. };
  17. function next () {
  18. pending = true;
  19. process.nextTick(function () {
  20. if (tasks.length === 0) return;
  21. var t = tasks.shift();
  22. t();
  23. pending = false;
  24. next();
  25. });
  26. }
  27. })();
  28. process.stdout.on('resize', draw);
  29. draw();
  30. setInterval(function () {}, 1e8);
  31. function draw () {
  32. var cols = process.stdout.columns;
  33. var rows = process.stdout.rows;
  34. queue.abort();
  35. queue.push(function () {
  36. c.reset();
  37. c.background('blue');
  38. c.position(1, 1);
  39. c.write(Array(cols + 1).join(' '));
  40. });
  41. for (var y = 1; y < rows; y++) {
  42. queue.push(function () {
  43. c.position(1, y);
  44. c.write(' ');
  45. c.position(cols, y);
  46. c.write(' ');
  47. });
  48. }
  49. queue.push(function () {
  50. c.position(1, rows);
  51. c.write(Array(cols + 1).join(' '));
  52. c.display('reset');
  53. });
  54. }