Prechádzať zdrojové kódy

Adding docs for Line, Render, Door.

Steve Thielemann 3 rokov pred
rodič
commit
1948b9c071
2 zmenil súbory, kde vykonal 117 pridanie a 40 odobranie
  1. 5 5
      door/door.go
  2. 112 35
      door/line.go

+ 5 - 5
door/door.go

@@ -254,7 +254,7 @@ func (d *Door) Init(doorname string) {
 
 	d.ReadDropfile(dropfile)
 
-	// doorname - node #?
+	// Logfile will be doorname - node #
 	logfilename := fmt.Sprintf("%s-%d.log", doorname, d.Config.Node)
 	logf, err := os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
 	if err != nil {
@@ -263,11 +263,7 @@ func (d *Door) Init(doorname string) {
 
 	log.SetOutput(logf)
 	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
-	// log.SetPrefix(doorname + " ")
-
-	//= log.New(logf, fmt.Sprintf("%s-%d", doorname, d.Config.Node), log.Ldate|log.Ltime|log.Lshortfile)
 	log.Printf("Loading dropfile %s\n", dropfile)
-
 	log.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
 
 	d.readerChannel = make(chan byte)
@@ -285,6 +281,10 @@ func (d *Door) Init(doorname string) {
 	}
 }
 
+// Goto X, Y - Position the cursor using ANSI Escape Codes
+//
+// Example:
+//     d.Write(door.Goto(1, 5) + "Now at X=1, Y=5.")
 func Goto(x int, y int) string {
 	return fmt.Sprintf("\x1b[%d;%dH", y, x)
 }

+ 112 - 35
door/line.go

@@ -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}