|
@@ -4,13 +4,84 @@ import (
|
|
|
"unicode"
|
|
|
)
|
|
|
|
|
|
+/*
|
|
|
+door.Line - Display a line of text
|
|
|
+
|
|
|
+Example:
|
|
|
+
|
|
|
+ var basicLine door.Line = {Text: "Welcome",
|
|
|
+ DefaultColor: door.Color("BRIGHT YELLOW"),
|
|
|
+ }
|
|
|
+
|
|
|
+ d.Write(basicLine.Output() + door.Reset + door.CRNL)
|
|
|
+
|
|
|
+This outputs Welcome in Bright/Bold Yellow.
|
|
|
+
|
|
|
+Example Render:
|
|
|
+
|
|
|
+ var renderAlphaDigit func(string) string = func(text string) string {
|
|
|
+ var r door.Render{Text: text}
|
|
|
+ var alpha string = door.ColorText("BOLD YELLOW")
|
|
|
+ var digit string = door.ColorText("BOLD GREEN")
|
|
|
+ var other string = door.Reset
|
|
|
+ for _, letter := range text {
|
|
|
+ if unicode.IsAlpha(letter) {
|
|
|
+ r.Append(alpha, 1)
|
|
|
+ } else {
|
|
|
+ if unicode.IsDigit(letter) {
|
|
|
+ r.Append(digit, 1)
|
|
|
+ } else {
|
|
|
+ r.Append(other, 1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return r.Result
|
|
|
+ }
|
|
|
+ var renderLine door.Line = {Text: "Render - 12345",
|
|
|
+ RenderF: renderAlphaDigit,
|
|
|
+ }
|
|
|
+ d.Write(renderLine.Output() + door.Reset + door.CRNL)
|
|
|
+
|
|
|
+This outputs "Render" in Yellow, "12345" in Green, and " - " in White.
|
|
|
+
|
|
|
+Example Update:
|
|
|
+
|
|
|
+ var updateTime() string {
|
|
|
+ var now time.Time = time.Now()
|
|
|
+ var result string = now.Format("3:04:05 PM")
|
|
|
+ }
|
|
|
+ var timeLine door.Line = {Text: updateTime(),
|
|
|
+ UpdateF: updateTime,
|
|
|
+ }
|
|
|
+ d.Write(timeLine.Output() + door.CRNL)
|
|
|
+ time.Sleep(time.Second)
|
|
|
+
|
|
|
+ // Check timeLine for update
|
|
|
+ if timeLine.Update() {
|
|
|
+ // Yes, there's an update to the time, output it.
|
|
|
+ d.Write(timeLine.Output() + door.CRNL)
|
|
|
+ }
|
|
|
+
|
|
|
+ if timeLine.Update() {
|
|
|
+ // This isn't called. There were no changes.
|
|
|
+ d.Write(timeLine.Output() + door.CRNL)
|
|
|
+ }
|
|
|
+
|
|
|
+This outputs the Current time in 12 hour format. It pauses for a second,
|
|
|
+and outputs the new time.
|
|
|
+*/
|
|
|
type Line struct {
|
|
|
- Text string
|
|
|
- DefaultColor string
|
|
|
- RenderF func(string) string
|
|
|
- UpdateF func() string
|
|
|
+ Text string // Text to be displayed
|
|
|
+ DefaultColor string // Default Color to use
|
|
|
+ RenderF func(string) string // Render function (displays string with colors)
|
|
|
+ UpdateF func() string // Update function updates the text
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+Line Update - This calls the UpdateF if present.
|
|
|
+
|
|
|
+Returns true if the line has been updated, and there's an Update function.
|
|
|
+*/
|
|
|
func (l *Line) Update() bool {
|
|
|
if l.UpdateF == nil {
|
|
|
return false
|
|
@@ -23,6 +94,12 @@ func (l *Line) Update() bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+Line Output - returns a string with ANSI Color codes.
|
|
|
+
|
|
|
+If there is no RenderF, we use the DefaultColor. Otherwise we pass the text
|
|
|
+to RenderF and return what it returns.
|
|
|
+*/
|
|
|
func (l *Line) Output() string {
|
|
|
if l.RenderF == nil {
|
|
|
return l.DefaultColor + l.Text
|
|
@@ -31,45 +108,45 @@ func (l *Line) Output() string {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//
|
|
|
-// The old example of a render function (before Render struct)
|
|
|
-//
|
|
|
-// Sample RenderF function
|
|
|
-// Upcase is BOLD Blue, everything else is Yellow
|
|
|
/*
|
|
|
-func Render_BlueYellow(text string) string {
|
|
|
- var output string
|
|
|
- var lastColor string
|
|
|
+door.Render - Helper for Line RenderF (Render Function)
|
|
|
|
|
|
- blue := ColorText("BOLD BLUE")
|
|
|
- yellow := ColorText("BOLD YELLOW")
|
|
|
+Example:
|
|
|
+ // RenderStatus - KEY_COLOR[key] COLON_COLOR[:] VALUE_COLOR[value]
|
|
|
+ func RenderStatus(text string) string {
|
|
|
+ var r door.Render = Render{Line: text}
|
|
|
+ var bool inValue = false
|
|
|
+ var key string = door.ColorText("BLUE")
|
|
|
+ var colon string = door.ColorText("BRIGHT WHITE")
|
|
|
+ var value string = door.ColorText("MAGENTA")
|
|
|
|
|
|
- for _, letter := range text {
|
|
|
- if unicode.IsUpper(letter) {
|
|
|
- if lastColor != blue {
|
|
|
- lastColor = blue
|
|
|
- output += blue
|
|
|
- }
|
|
|
- output += string(letter)
|
|
|
- } else {
|
|
|
- if lastColor != yellow {
|
|
|
- lastColor = yellow
|
|
|
- output += yellow
|
|
|
+ for _, letter := range text {
|
|
|
+ if letter == ':' {
|
|
|
+ r.Append(colon, 1)
|
|
|
+ inValue = true
|
|
|
+ } else {
|
|
|
+ if inValue {
|
|
|
+ r.Append(value, 1)
|
|
|
+ } else {
|
|
|
+ r.Append(key, 1)
|
|
|
+ }
|
|
|
}
|
|
|
- output += string(letter)
|
|
|
}
|
|
|
+ return r.Result
|
|
|
}
|
|
|
- return output
|
|
|
-}
|
|
|
*/
|
|
|
-
|
|
|
type Render struct {
|
|
|
- Line string
|
|
|
- Result string
|
|
|
- Pos int
|
|
|
- LastColor string
|
|
|
+ Line string // Original Text
|
|
|
+ Result string // Output Result
|
|
|
+ Pos int // Current Position
|
|
|
+ LastColor string // LastColor code sent
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+Render.Append - Output len number of characters in the color.
|
|
|
+
|
|
|
+This uses Render.LastColor to tell if we've already sent that color before.
|
|
|
+*/
|
|
|
func (r *Render) Append(color string, len int) {
|
|
|
if color != r.LastColor {
|
|
|
r.LastColor = color
|
|
@@ -85,8 +162,8 @@ func (r *Render) Append(color string, len int) {
|
|
|
r.Pos += len
|
|
|
}
|
|
|
|
|
|
-// Sample RenderF function
|
|
|
-// Upcase is BOLD Blue, everything else is Yellow
|
|
|
+// RenderBlueYellow - Uppercase is Bold Blue, everything else is Yellow.
|
|
|
+// This is an example of using the door.Render routines.
|
|
|
func RenderBlueYellow(text string) string {
|
|
|
var r Render = Render{Line: text}
|
|
|
|