plumbing.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict'
  2. var consoleControl = require('console-control-strings')
  3. var renderTemplate = require('./render-template.js')
  4. var validate = require('aproba')
  5. var Plumbing = module.exports = function (theme, template, width) {
  6. if (!width) width = 80
  7. validate('OAN', [theme, template, width])
  8. this.showing = false
  9. this.theme = theme
  10. this.width = width
  11. this.template = template
  12. }
  13. Plumbing.prototype = {}
  14. Plumbing.prototype.setTheme = function (theme) {
  15. validate('O', [theme])
  16. this.theme = theme
  17. }
  18. Plumbing.prototype.setTemplate = function (template) {
  19. validate('A', [template])
  20. this.template = template
  21. }
  22. Plumbing.prototype.setWidth = function (width) {
  23. validate('N', [width])
  24. this.width = width
  25. }
  26. Plumbing.prototype.hide = function () {
  27. return consoleControl.gotoSOL() + consoleControl.eraseLine()
  28. }
  29. Plumbing.prototype.hideCursor = consoleControl.hideCursor
  30. Plumbing.prototype.showCursor = consoleControl.showCursor
  31. Plumbing.prototype.show = function (status) {
  32. var values = Object.create(this.theme)
  33. for (var key in status) {
  34. values[key] = status[key]
  35. }
  36. return renderTemplate(this.width, this.template, values).trim() +
  37. consoleControl.color('reset') +
  38. consoleControl.eraseLine() + consoleControl.gotoSOL()
  39. }