box.go 2.3 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_CP437 = [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{"\xd5", "\xb8", "\xcd", "\xb3", "\xd4", "\xbe", "\xc6", "\xb5"},
  39. /*
  40. ╓──╖
  41. ║ ║
  42. ╟──╢
  43. ╙──╜
  44. */
  45. BoxStyle{"\xd6", "\xb7", "\xc4", "\xba", "\xd3", "\xbd", "\xc7", "\xb6"},
  46. }
  47. var BOXES_UNICODE = [4]BoxStyle{
  48. BoxStyle{"\u250c", "\u2510", "\u2500", "\u2502", "\u2514", "\u2518", "\u251c", "\u2524"},
  49. BoxStyle{"\u2554", "\u2557", "\u2550", "\u2551", "\u255a", "\u255d", "\u2560", "\u2563"},
  50. BoxStyle{"\u2552", "\u2555", "\u2550", "\u2502", "\u2558", "\u255b", "\u255e", "\u2561"},
  51. BoxStyle{"\u2553", "\u2556", "\u2500", "\u2551", "\u2559", "\u255c", "\u255f", "\u2562"},
  52. }
  53. var BOXES *[4]BoxStyle = &BOXES_CP437
  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([]byte(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. }