瀏覽代碼

Line has Width. It will auto set length.

No more fmt.Sprintf("%*s", -width, string).
Steve Thielemann 2 年之前
父節點
當前提交
93748a4c04
共有 1 個文件被更改,包括 18 次插入0 次删除
  1. 18 0
      door/line.go

+ 18 - 0
door/line.go

@@ -1,6 +1,7 @@
 package door
 
 import (
+	"strings"
 	"unicode"
 )
 
@@ -76,6 +77,7 @@ type Line struct {
 	DefaultColor string              // Default Color to use
 	RenderF      func(string) string // Render function (displays string with colors)
 	UpdateF      func() string       // Update function updates the text
+	Width        int                 // Line length
 }
 
 /*
@@ -88,6 +90,7 @@ func (l *Line) Update() bool {
 		return false
 	}
 	var NewText string = l.UpdateF()
+	l.LineLength(&NewText)
 	if NewText != l.Text {
 		l.Text = NewText
 		return true
@@ -95,6 +98,20 @@ func (l *Line) Update() bool {
 	return false
 }
 
+func (l *Line) LineLength(text *string) {
+	var length int
+	if l.Width == 0 {
+		return
+	}
+
+	if Unicode {
+		length = len([]rune(*text))
+	} else {
+		length = len([]byte(*text))
+	}
+	*text += strings.Repeat(" ", l.Width-length)
+}
+
 /*
 Line Output - returns a string with ANSI Color codes.
 
@@ -102,6 +119,7 @@ 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 {
+	l.LineLength(&l.Text)
 	if l.RenderF == nil {
 		return l.DefaultColor + l.Text
 	} else {