line.go 4.1 KB

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