line.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package door
  2. import (
  3. "log"
  4. "strings"
  5. "unicode"
  6. )
  7. /*
  8. door.Line - Display a line of text
  9. Example:
  10. var basicLine door.Line = {Text: "Welcome",
  11. DefaultColor: door.Color("BRIGHT YELLOW"),
  12. }
  13. d.Write(basicLine.Output() + door.Reset + door.CRNL)
  14. This outputs Welcome in Bright/Bold Yellow.
  15. Example Render:
  16. var renderAlphaDigit func(string) string = func(text string) string {
  17. var r door.Render{Text: text}
  18. var alpha string = door.ColorText("BOLD YELLOW")
  19. var digit string = door.ColorText("BOLD GREEN")
  20. var other string = door.Reset
  21. for _, letter := range text {
  22. if unicode.IsAlpha(letter) {
  23. r.Append(alpha, 1)
  24. } else {
  25. if unicode.IsDigit(letter) {
  26. r.Append(digit, 1)
  27. } else {
  28. r.Append(other, 1)
  29. }
  30. }
  31. }
  32. return r.Result
  33. }
  34. var renderLine door.Line = {Text: "Render - 12345",
  35. RenderF: renderAlphaDigit,
  36. }
  37. d.Write(renderLine.Output() + door.Reset + door.CRNL)
  38. This outputs "Render" in Yellow, "12345" in Green, and " - " in White.
  39. Example Update:
  40. var updateTime() string {
  41. var now time.Time = time.Now()
  42. var result string = now.Format("3:04:05 PM")
  43. return result
  44. }
  45. var timeLine door.Line = {Text: updateTime(),
  46. UpdateF: updateTime,
  47. }
  48. d.Write(timeLine.Output() + door.CRNL)
  49. time.Sleep(time.Second)
  50. // Check timeLine for update
  51. if timeLine.Update() {
  52. // Yes, there's an update to the time, output it.
  53. d.Write(timeLine.Output() + door.CRNL)
  54. }
  55. if timeLine.Update() {
  56. // This isn't called. There were no changes.
  57. d.Write(timeLine.Output() + door.CRNL)
  58. }
  59. This outputs the Current time in 12 hour format. It pauses for a second,
  60. and outputs the new time.
  61. */
  62. type Line struct {
  63. Text string // Text to be displayed
  64. DefaultColor string // Default Color to use
  65. RenderF func(string) string // Render function (displays string with colors)
  66. UpdateF func() string // Update function updates the text
  67. Width int // Line length
  68. }
  69. /*
  70. Line Update - This calls the UpdateF if present.
  71. Returns true if the line has been updated, and there's an Update function.
  72. */
  73. func (l *Line) Update() bool {
  74. if l.UpdateF == nil {
  75. return false
  76. }
  77. var NewText string = l.UpdateF()
  78. l.LineLength(&NewText)
  79. if NewText != l.Text {
  80. l.Text = NewText
  81. return true
  82. }
  83. return false
  84. }
  85. func (l *Line) LineLength(text *string) {
  86. var length int
  87. if l.Width == 0 {
  88. return
  89. }
  90. if Unicode {
  91. length = len([]rune(*text))
  92. } else {
  93. length = len([]byte(*text))
  94. }
  95. if length > l.Width {
  96. log.Printf("ERROR: Line Width %d: Have %d\n", l.Width, length)
  97. } else {
  98. *text += strings.Repeat(" ", l.Width-length)
  99. }
  100. }
  101. /*
  102. Line Output - returns a string with ANSI Color codes.
  103. If there is no RenderF, we use the DefaultColor. Otherwise we pass the text
  104. to RenderF and return what it returns.
  105. */
  106. func (l *Line) Output() string {
  107. l.LineLength(&l.Text)
  108. if l.RenderF == nil {
  109. return l.DefaultColor + l.Text
  110. } else {
  111. return l.RenderF(l.Text)
  112. }
  113. }
  114. /*
  115. door.Render - Helper for Line RenderF (Render Function)
  116. Example:
  117. // RenderStatus - KEY_COLOR[key] COLON_COLOR[:] VALUE_COLOR[value]
  118. func RenderStatus(text string) string {
  119. var r door.Render = Render{Line: text}
  120. var bool inValue = false
  121. var key string = door.ColorText("BLUE")
  122. var colon string = door.ColorText("BRIGHT WHITE")
  123. var value string = door.ColorText("MAGENTA")
  124. for _, letter := range text {
  125. if letter == ':' {
  126. r.Append(colon, 1)
  127. inValue = true
  128. } else {
  129. if inValue {
  130. r.Append(value, 1)
  131. } else {
  132. r.Append(key, 1)
  133. }
  134. }
  135. }
  136. return r.Result
  137. }
  138. */
  139. type Render struct {
  140. Line string // Original Text
  141. Result string // Output Result
  142. Pos int // Current Position
  143. LastColor string // LastColor code sent
  144. }
  145. /*
  146. Render.Append - Output len number of characters in the color.
  147. This uses Render.LastColor to tell if we've already sent that color before.
  148. */
  149. func (r *Render) Append(color string, len int) {
  150. if color != r.LastColor {
  151. r.LastColor = color
  152. r.Result += color
  153. }
  154. if Unicode {
  155. // Treat unicode as []rune.
  156. r.Result += string([]rune(r.Line)[r.Pos : r.Pos+len])
  157. } else {
  158. r.Result += r.Line[r.Pos : r.Pos+len]
  159. }
  160. r.Pos += len
  161. }
  162. // RenderBlueYellow - Uppercase is Bold Blue, everything else is Yellow.
  163. // This is an example of using the door.Render routines.
  164. func RenderBlueYellow(text string) string {
  165. var r Render = Render{Line: text}
  166. var blue string = ColorText("BOLD BLUE")
  167. var yellow string = ColorText("BOLD YELLOW")
  168. for _, letter := range text {
  169. if unicode.IsUpper(letter) {
  170. r.Append(blue, 1)
  171. } else {
  172. r.Append(yellow, 1)
  173. }
  174. }
  175. return r.Result
  176. }