panel.go 3.0 KB

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