plumbing.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.eraseLine() + consoleControl.gotoSOL()
  38. }