Pane.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {DisplayElement} from 'tui-lib/ui/primitives'
  2. import * as ansi from 'tui-lib/util/ansi'
  3. import unic from 'tui-lib/util/unichars'
  4. import Label from './Label.js'
  5. export default class Pane extends DisplayElement {
  6. // A simple rectangular framed pane.
  7. constructor() {
  8. super()
  9. this.frameColor = null
  10. this.hPadding = 1
  11. this.vPadding = 1
  12. }
  13. drawTo(writable) {
  14. this.drawFrame(writable)
  15. super.drawTo(writable)
  16. }
  17. drawFrame(writable, debug=false) {
  18. writable.write(ansi.setForeground(this.frameColor))
  19. const left = this.absLeft
  20. const right = this.absRight
  21. const top = this.absTop
  22. const bottom = this.absBottom
  23. // Background
  24. // (TODO) Transparent background (that dimmed everything behind it) would
  25. // be cool at some point!
  26. for (let y = top + 1; y <= bottom - 1; y++) {
  27. writable.write(ansi.moveCursor(y, left))
  28. writable.write(' '.repeat(this.w))
  29. }
  30. // Left/right edges
  31. for (let x = left + 1; x <= right - 1; x++) {
  32. writable.write(ansi.moveCursor(top, x))
  33. writable.write(unic.BOX_H)
  34. writable.write(ansi.moveCursor(bottom, x))
  35. writable.write(unic.BOX_H)
  36. }
  37. // Top/bottom edges
  38. for (let y = top + 1; y <= bottom - 1; y++) {
  39. writable.write(ansi.moveCursor(y, left))
  40. writable.write(unic.BOX_V)
  41. writable.write(ansi.moveCursor(y, right))
  42. writable.write(unic.BOX_V)
  43. }
  44. // Corners
  45. writable.write(ansi.moveCursor(top, left))
  46. writable.write(unic.BOX_CORNER_TL)
  47. writable.write(ansi.moveCursor(top, right))
  48. writable.write(unic.BOX_CORNER_TR)
  49. writable.write(ansi.moveCursor(bottom, left))
  50. writable.write(unic.BOX_CORNER_BL)
  51. writable.write(ansi.moveCursor(bottom, right))
  52. writable.write(unic.BOX_CORNER_BR)
  53. // Debug info
  54. if (debug) {
  55. writable.write(ansi.moveCursor(6, 8))
  56. writable.write(
  57. `x: ${this.x}; y: ${this.y}; w: ${this.w}; h: ${this.h}`)
  58. writable.write(ansi.moveCursor(7, 8))
  59. writable.write(`AbsX: ${this.absX}; AbsY: ${this.absY}`)
  60. writable.write(ansi.moveCursor(8, 8))
  61. writable.write(`Left: ${this.left}; Right: ${this.right}`)
  62. writable.write(ansi.moveCursor(9, 8))
  63. writable.write(`Top: ${this.top}; Bottom: ${this.bottom}`)
  64. }
  65. writable.write(ansi.setForeground(ansi.C_RESET))
  66. }
  67. static alert(parent, text) {
  68. // Show an alert pane in the bottom left of the given parent element for
  69. // a couple seconds.
  70. const pane = new Pane()
  71. pane.frameColor = ansi.C_WHITE
  72. pane.w = ansi.measureColumns(text) + 2
  73. pane.h = 3
  74. parent.addChild(pane)
  75. const label = new Label(text)
  76. label.textAttributes = [ansi.C_WHITE]
  77. pane.addChild(label)
  78. setTimeout(() => {
  79. parent.removeChild(pane)
  80. }, 2000)
  81. }
  82. }