Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env node
  2. var assert = require('assert')
  3. , ansi = require('../../')
  4. function Progress (stream, width) {
  5. this.cursor = ansi(stream)
  6. this.delta = this.cursor.newlines
  7. this.width = width | 0 || 10
  8. this.open = '['
  9. this.close = ']'
  10. this.complete = '█'
  11. this.incomplete = '_'
  12. // initial render
  13. this.progress = 0
  14. }
  15. Object.defineProperty(Progress.prototype, 'progress', {
  16. get: get
  17. , set: set
  18. , configurable: true
  19. , enumerable: true
  20. })
  21. function get () {
  22. return this._progress
  23. }
  24. function set (v) {
  25. this._progress = Math.max(0, Math.min(v, 100))
  26. var w = this.width - this.complete.length - this.incomplete.length
  27. , n = w * (this._progress / 100) | 0
  28. , i = w - n
  29. , com = c(this.complete, n)
  30. , inc = c(this.incomplete, i)
  31. , delta = this.cursor.newlines - this.delta
  32. assert.equal(com.length + inc.length, w)
  33. if (delta > 0) {
  34. this.cursor.up(delta)
  35. this.delta = this.cursor.newlines
  36. }
  37. this.cursor
  38. .horizontalAbsolute(0)
  39. .eraseLine(2)
  40. .fg.white()
  41. .write(this.open)
  42. .fg.grey()
  43. .bold()
  44. .write(com)
  45. .resetBold()
  46. .write(inc)
  47. .fg.white()
  48. .write(this.close)
  49. .fg.reset()
  50. .write('\n')
  51. }
  52. function c (char, length) {
  53. return Array.apply(null, Array(length)).map(function () {
  54. return char
  55. }).join('')
  56. }
  57. // Usage
  58. var width = parseInt(process.argv[2], 10) || process.stdout.getWindowSize()[0] / 2
  59. , p = new Progress(process.stdout, width)
  60. ;(function tick () {
  61. p.progress += Math.random() * 5
  62. p.cursor
  63. .eraseLine(2)
  64. .write('Progress: ')
  65. .bold().write(p.progress.toFixed(2))
  66. .write('%')
  67. .resetBold()
  68. .write('\n')
  69. if (p.progress < 100)
  70. setTimeout(tick, 100)
  71. })()