box.go 2.1 KB

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