box.go 2.2 KB

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