WrapLabel.js 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import * as ansi from 'tui-lib/util/ansi'
  2. import Label from './Label.js'
  3. export default class WrapLabel extends Label {
  4. // A word-wrapping text display. Given a width, wraps text to fit.
  5. constructor(...args) {
  6. super(...args)
  7. }
  8. fixLayout() {
  9. // Override Label.fixLayout to do nothing. We don't want to make the
  10. // width of this label be set to the content of the text! (That would
  11. // defeat the entire point of word wrapping.)
  12. }
  13. writeTextTo(writable) {
  14. const lines = this.getWrappedLines()
  15. for (let i = 0; i < lines.length; i++) {
  16. writable.write(ansi.moveCursor(this.absTop + i, this.absLeft))
  17. writable.write(this.processFormatting(lines[i]))
  18. }
  19. }
  20. getWrappedLines() {
  21. if (this.text.trim().length === 0) {
  22. return []
  23. }
  24. return ansi.wrapToColumns(this.text, this.w - 1).map(l => l.trim())
  25. }
  26. get h() {
  27. return this.getWrappedLines().length
  28. }
  29. set h(newHeight) {
  30. // Do nothing. Height is computed on the fly.
  31. }
  32. }