panel.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package door
  2. import "strings"
  3. type BorderStyle int
  4. const (
  5. NONE BorderStyle = iota
  6. SINGLE
  7. DOUBLE
  8. DOUBLE_SINGLE
  9. SINGLE_DOUBLE
  10. )
  11. type Panel struct {
  12. X int
  13. Y int
  14. Width int
  15. Style BorderStyle
  16. BorderColor string
  17. Lines []Line
  18. Title string
  19. TitleOffset int
  20. }
  21. // Output the panel
  22. func (p *Panel) Output() string {
  23. var style int = int(p.Style)
  24. var box_style *BoxStyle
  25. var output string
  26. if style > 0 {
  27. if Unicode {
  28. box_style = &BOXES_UNICODE[style-1]
  29. } else {
  30. box_style = &BOXES[style-1]
  31. }
  32. }
  33. row := p.Y
  34. if style > 0 {
  35. // Top line / border
  36. output += Goto(p.X, row) + p.BorderColor + box_style.top_left
  37. if p.Title != "" {
  38. output += strings.Repeat(box_style.top, p.TitleOffset) + p.Title
  39. }
  40. output += strings.Repeat(box_style.top, p.Width-(p.TitleOffset+len(p.Title))) + box_style.top_right
  41. row++
  42. }
  43. for _, line := range p.Lines {
  44. output += Goto(p.X, row)
  45. // "Joining" lines code
  46. // We are not joining lines at this time.
  47. if style > 0 {
  48. output += p.BorderColor + box_style.side
  49. }
  50. output += line.Output()
  51. if style > 0 {
  52. output += p.BorderColor + box_style.side
  53. }
  54. row++
  55. }
  56. if style > 0 {
  57. // Bottom / border
  58. output += Goto(p.X, row) + p.BorderColor + box_style.bottom_left
  59. output += strings.Repeat(box_style.top, p.Width) + box_style.bottom_right
  60. }
  61. return output
  62. }
  63. // Output anything that has updated
  64. func (p *Panel) Update() string {
  65. var output string
  66. var style int = int(p.Style)
  67. row := p.Y
  68. col := p.X
  69. if style > 0 {
  70. row++
  71. col++
  72. }
  73. for _, line := range p.Lines {
  74. if line.Update() {
  75. // Yes, line was updated
  76. output += Goto(col, row) + line.Output()
  77. }
  78. row++
  79. }
  80. return output
  81. }
  82. // Output the updated line
  83. func (p *Panel) UpdateLine(index int) string {
  84. var output string
  85. var style int = int(p.Style)
  86. row := p.Y + index
  87. col := p.X
  88. if style > 0 {
  89. row++
  90. col++
  91. }
  92. line := &p.Lines[index]
  93. output += Goto(col, row) + line.Output()
  94. return output
  95. }
  96. // Goto the end
  97. func (p *Panel) GotoEnd() string {
  98. row := p.Y
  99. col := p.X
  100. if p.Style > 0 {
  101. row++
  102. col += 2
  103. }
  104. row += len(p.Lines)
  105. col += p.Width
  106. return Goto(col, row)
  107. }
  108. // Is the top line of this style a single line?
  109. func Single(bs BorderStyle) bool {
  110. switch bs {
  111. case SINGLE, SINGLE_DOUBLE:
  112. return true
  113. default:
  114. return false
  115. }
  116. }
  117. // Create a spacer line that will be connected maybe to the sides.
  118. func (p *Panel) Spacer() Line {
  119. l := Line{}
  120. var pos int
  121. if Single(p.Style) {
  122. pos = 0
  123. } else {
  124. pos = 1
  125. }
  126. if Unicode {
  127. l.Text = strings.Repeat(BOXES[pos].top, p.Width)
  128. } else {
  129. l.Text = strings.Repeat(BOXES_UNICODE[pos].top, p.Width)
  130. }
  131. return l
  132. }