render-template.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. 'use strict'
  2. var align = require('wide-align')
  3. var validate = require('aproba')
  4. var wideTruncate = require('./wide-truncate')
  5. var error = require('./error')
  6. var TemplateItem = require('./template-item')
  7. function renderValueWithValues (values) {
  8. return function (item) {
  9. return renderValue(item, values)
  10. }
  11. }
  12. var renderTemplate = module.exports = function (width, template, values) {
  13. var items = prepareItems(width, template, values)
  14. var rendered = items.map(renderValueWithValues(values)).join('')
  15. return align.left(wideTruncate(rendered, width), width)
  16. }
  17. function preType (item) {
  18. var cappedTypeName = item.type[0].toUpperCase() + item.type.slice(1)
  19. return 'pre' + cappedTypeName
  20. }
  21. function postType (item) {
  22. var cappedTypeName = item.type[0].toUpperCase() + item.type.slice(1)
  23. return 'post' + cappedTypeName
  24. }
  25. function hasPreOrPost (item, values) {
  26. if (!item.type) return
  27. return values[preType(item)] || values[postType(item)]
  28. }
  29. function generatePreAndPost (baseItem, parentValues) {
  30. var item = Object.assign({}, baseItem)
  31. var values = Object.create(parentValues)
  32. var template = []
  33. var pre = preType(item)
  34. var post = postType(item)
  35. if (values[pre]) {
  36. template.push({value: values[pre]})
  37. values[pre] = null
  38. }
  39. item.minLength = null
  40. item.length = null
  41. item.maxLength = null
  42. template.push(item)
  43. values[item.type] = values[item.type]
  44. if (values[post]) {
  45. template.push({value: values[post]})
  46. values[post] = null
  47. }
  48. return function ($1, $2, length) {
  49. return renderTemplate(length, template, values)
  50. }
  51. }
  52. function prepareItems (width, template, values) {
  53. function cloneAndObjectify (item, index, arr) {
  54. var cloned = new TemplateItem(item, width)
  55. var type = cloned.type
  56. if (cloned.value == null) {
  57. if (!(type in values)) {
  58. if (cloned.default == null) {
  59. throw new error.MissingTemplateValue(cloned, values)
  60. } else {
  61. cloned.value = cloned.default
  62. }
  63. } else {
  64. cloned.value = values[type]
  65. }
  66. }
  67. if (cloned.value == null || cloned.value === '') return null
  68. cloned.index = index
  69. cloned.first = index === 0
  70. cloned.last = index === arr.length - 1
  71. if (hasPreOrPost(cloned, values)) cloned.value = generatePreAndPost(cloned, values)
  72. return cloned
  73. }
  74. var output = template.map(cloneAndObjectify).filter(function (item) { return item != null })
  75. var remainingSpace = width
  76. var variableCount = output.length
  77. function consumeSpace (length) {
  78. if (length > remainingSpace) length = remainingSpace
  79. remainingSpace -= length
  80. }
  81. function finishSizing (item, length) {
  82. if (item.finished) throw new error.Internal('Tried to finish template item that was already finished')
  83. if (length === Infinity) throw new error.Internal('Length of template item cannot be infinity')
  84. if (length != null) item.length = length
  85. item.minLength = null
  86. item.maxLength = null
  87. --variableCount
  88. item.finished = true
  89. if (item.length == null) item.length = item.getBaseLength()
  90. if (item.length == null) throw new error.Internal('Finished template items must have a length')
  91. consumeSpace(item.getLength())
  92. }
  93. output.forEach(function (item) {
  94. if (!item.kerning) return
  95. var prevPadRight = item.first ? 0 : output[item.index - 1].padRight
  96. if (!item.first && prevPadRight < item.kerning) item.padLeft = item.kerning - prevPadRight
  97. if (!item.last) item.padRight = item.kerning
  98. })
  99. // Finish any that have a fixed (literal or intuited) length
  100. output.forEach(function (item) {
  101. if (item.getBaseLength() == null) return
  102. finishSizing(item)
  103. })
  104. var resized = 0
  105. var resizing
  106. var hunkSize
  107. do {
  108. resizing = false
  109. hunkSize = Math.round(remainingSpace / variableCount)
  110. output.forEach(function (item) {
  111. if (item.finished) return
  112. if (!item.maxLength) return
  113. if (item.getMaxLength() < hunkSize) {
  114. finishSizing(item, item.maxLength)
  115. resizing = true
  116. }
  117. })
  118. } while (resizing && resized++ < output.length)
  119. if (resizing) throw new error.Internal('Resize loop iterated too many times while determining maxLength')
  120. resized = 0
  121. do {
  122. resizing = false
  123. hunkSize = Math.round(remainingSpace / variableCount)
  124. output.forEach(function (item) {
  125. if (item.finished) return
  126. if (!item.minLength) return
  127. if (item.getMinLength() >= hunkSize) {
  128. finishSizing(item, item.minLength)
  129. resizing = true
  130. }
  131. })
  132. } while (resizing && resized++ < output.length)
  133. if (resizing) throw new error.Internal('Resize loop iterated too many times while determining minLength')
  134. hunkSize = Math.round(remainingSpace / variableCount)
  135. output.forEach(function (item) {
  136. if (item.finished) return
  137. finishSizing(item, hunkSize)
  138. })
  139. return output
  140. }
  141. function renderFunction (item, values, length) {
  142. validate('OON', arguments)
  143. if (item.type) {
  144. return item.value(values, values[item.type + 'Theme'] || {}, length)
  145. } else {
  146. return item.value(values, {}, length)
  147. }
  148. }
  149. function renderValue (item, values) {
  150. var length = item.getBaseLength()
  151. var value = typeof item.value === 'function' ? renderFunction(item, values, length) : item.value
  152. if (value == null || value === '') return ''
  153. var alignWith = align[item.align] || align.left
  154. var leftPadding = item.padLeft ? align.left('', item.padLeft) : ''
  155. var rightPadding = item.padRight ? align.right('', item.padRight) : ''
  156. var truncated = wideTruncate(String(value), length)
  157. var aligned = alignWith(truncated, length)
  158. return leftPadding + aligned + rightPadding
  159. }