box.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package door
  2. import "strings"
  3. type BoxStyle struct {
  4. top_left string
  5. top_right string
  6. top string
  7. side string
  8. bottom_left string
  9. bottom_right string
  10. middle_left string
  11. middle_right string
  12. }
  13. // FUTURE:
  14. // Once we detect CP437 or Unicode, set BOXES to point to
  15. // BOXES_CP437 or BOXES_UNICODE. That'll save us from having
  16. // to if Unicode when building boxes. ;)
  17. var BOXES = [4]BoxStyle{
  18. /*
  19. ┌──┐
  20. │ │
  21. ├──┤
  22. └──┘
  23. */
  24. BoxStyle{"\xda", "\xbf", "\xc4", "\xb3", "\xc0", "\xd9", "\xc3", "\xb4"},
  25. /*
  26. ╔══╗
  27. ║ ║
  28. ╠══╣
  29. ╚══╝
  30. */
  31. BoxStyle{"\xc9", "\xbb", "\xcd", "\xba", "\xc8", "\xbc", "\xcc", "\xb9"},
  32. /*
  33. ╒══╕
  34. │ │
  35. ╞══╡
  36. ╘══╛
  37. */
  38. BoxStyle{"\xd6", "\xb8", "\xcd", "\xb3", "\xd4", "\xbe", "\xc6", "\xb5"},
  39. /*
  40. ╓──╖
  41. ║ ║
  42. ╟──╢
  43. ╙──╜
  44. */
  45. BoxStyle{"\xd6", "\xb7", "\xc4", "\xba", "\xd3", "\xbd", "\xc7", "\xb6"}}
  46. var BOXES_UNICODE = []BoxStyle{
  47. BoxStyle{"\u250c", "\u2510", "\u2500", "\u2502", "\u2514", "\u2518", "\u251c", "\u2524"},
  48. BoxStyle{"\u2554", "\u2557", "\u2550", "\u2551", "\u255a", "\u255d", "\u2560", "\u2563"},
  49. BoxStyle{"\u2553", "\u2556", "\u2500", "\u2551", "\u2559", "\u255c", "\u255f", "\u2562"},
  50. BoxStyle{"\u2552", "\u2555", "\u2550", "\u2502", "\u2558", "\u255b", "\u255e", "\u2561"},
  51. }
  52. type Box struct {
  53. Width int
  54. Style int
  55. }
  56. func (b *Box) Top() string {
  57. var style *BoxStyle
  58. if Unicode {
  59. style = &BOXES_UNICODE[b.Style]
  60. } else {
  61. style = &BOXES[b.Style]
  62. }
  63. return style.top_left + strings.Repeat(style.top, b.Width) + style.top_right
  64. }
  65. func (b *Box) Row(text string) string {
  66. var style *BoxStyle
  67. if Unicode {
  68. style = &BOXES_UNICODE[b.Style]
  69. } else {
  70. style = &BOXES[b.Style]
  71. }
  72. return style.side + text + style.side
  73. }
  74. func (b *Box) Middle() string {
  75. var style *BoxStyle
  76. if Unicode {
  77. style = &BOXES_UNICODE[b.Style]
  78. } else {
  79. style = &BOXES[b.Style]
  80. }
  81. return style.middle_left + strings.Repeat(style.top, b.Width) + style.middle_right
  82. }
  83. func (b *Box) Bottom() string {
  84. var style *BoxStyle
  85. if Unicode {
  86. style = &BOXES_UNICODE[b.Style]
  87. } else {
  88. style = &BOXES[b.Style]
  89. }
  90. return style.bottom_left + strings.Repeat(style.top, b.Width) + style.bottom_right
  91. }