|
@@ -1,19 +1,10 @@
|
|
|
package door
|
|
|
|
|
|
-// This was the way I did it in door++...
|
|
|
-// Don't
|
|
|
-/*
|
|
|
-type RenderOutput struct {
|
|
|
- Color string
|
|
|
- Pos int
|
|
|
- Len int
|
|
|
-}
|
|
|
-
|
|
|
-type Render struct {
|
|
|
- Text string
|
|
|
- Outputs []RenderOutput
|
|
|
-}
|
|
|
-*/
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "strings"
|
|
|
+ "unicode"
|
|
|
+)
|
|
|
|
|
|
type Line struct {
|
|
|
Text string
|
|
@@ -34,12 +25,184 @@ func (l *Line) Update() bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
-func (l *Line) Output(d *Door) {
|
|
|
+func (l *Line) Output() string {
|
|
|
if l.RenderF == nil {
|
|
|
- output := l.DefaultColor + l.Text
|
|
|
- d.Write(output)
|
|
|
+ return l.DefaultColor + l.Text
|
|
|
} else {
|
|
|
- output := l.RenderF(l.Text)
|
|
|
- d.Write(output)
|
|
|
+ return l.RenderF(l.Text)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// Sample RenderF function
|
|
|
+// Upcase is BOLD Blue, everything else is Yellow
|
|
|
+func RenderBlueYellow(text string) string {
|
|
|
+ var output string
|
|
|
+ var lastColor string
|
|
|
+
|
|
|
+ blue := ColorText("BOLD BLUE")
|
|
|
+ yellow := ColorText("BOLD YELLOW")
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ output += string(letter)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return output
|
|
|
+}
|
|
|
+
|
|
|
+type BarStyle int
|
|
|
+
|
|
|
+const (
|
|
|
+ SOLID BarStyle = iota
|
|
|
+ HALF_STEP
|
|
|
+ GRADIENT
|
|
|
+ PERCENTAGE
|
|
|
+ PERCENT_SPACE
|
|
|
+)
|
|
|
+
|
|
|
+type BarRange struct {
|
|
|
+ Percent int64
|
|
|
+ Color string
|
|
|
+}
|
|
|
+
|
|
|
+type BarLine struct {
|
|
|
+ Width int
|
|
|
+ Style BarStyle
|
|
|
+ Percent int64 // percentage * 100
|
|
|
+ ColorRange []BarRange
|
|
|
+ UpdateP func() int64
|
|
|
+ Line
|
|
|
+}
|
|
|
+
|
|
|
+func (bl *BarLine) CheckRange() {
|
|
|
+ if len(bl.ColorRange) != 0 {
|
|
|
+ // Ok, there is a color range. Get checking
|
|
|
+
|
|
|
+ for _, br := range bl.ColorRange {
|
|
|
+ if bl.Percent <= br.Percent {
|
|
|
+ bl.DefaultColor = br.Color
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (bl *BarLine) Output() string {
|
|
|
+ var output string
|
|
|
+ var step_width int64
|
|
|
+
|
|
|
+ if bl.UpdateP != nil {
|
|
|
+ bl.Percent = bl.UpdateP()
|
|
|
+ }
|
|
|
+
|
|
|
+ bl.CheckRange()
|
|
|
+
|
|
|
+ switch bl.Style {
|
|
|
+ case SOLID, PERCENTAGE, PERCENT_SPACE:
|
|
|
+ step_width = int64(100 * 100 / bl.Width)
|
|
|
+ var steps int = int(bl.Percent / step_width)
|
|
|
+
|
|
|
+ if Unicode {
|
|
|
+ output += strings.Repeat("\u2588", steps)
|
|
|
+ } else {
|
|
|
+ output += strings.Repeat("\xdb", steps)
|
|
|
+ }
|
|
|
+ // This will work, because we aren't trying to len(output) with unicode.
|
|
|
+ output += strings.Repeat(" ", int(bl.Width-steps))
|
|
|
+
|
|
|
+ if bl.Style == PERCENTAGE || bl.Style == PERCENT_SPACE {
|
|
|
+ percent := fmt.Sprintf("%d", bl.Percent/100)
|
|
|
+
|
|
|
+ var pos int = bl.Width/2 - 1
|
|
|
+ if percent != "100" {
|
|
|
+ percent += "%"
|
|
|
+ if len(percent) < 3 {
|
|
|
+ percent = " " + percent
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if bl.Style == PERCENT_SPACE {
|
|
|
+ percent = " " + percent + " "
|
|
|
+ pos--
|
|
|
+ }
|
|
|
+ // to process/slice the string (with unicode) do this:
|
|
|
+ // convert to []rune, slice that, convert back to string
|
|
|
+ //
|
|
|
+ // sliceable := []rune(output)
|
|
|
+ // newString := string(sliceable[:5]) + " new content " + (sliceable[10:])
|
|
|
+ // fmt.Printf("%d %d [%s] %d [%s]\n", bl.Width, pos, percent, len(output), output)
|
|
|
+
|
|
|
+ if Unicode {
|
|
|
+ runes := []rune(output)
|
|
|
+ output = string(runes[:pos]) + percent + string(runes[pos+len(percent):])
|
|
|
+ } else {
|
|
|
+ output = output[:pos] + percent + output[pos+len(percent):]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case HALF_STEP:
|
|
|
+ step_width = int64(100 * 100 / bl.Width)
|
|
|
+ var steps int = int(bl.Percent * 2 / step_width)
|
|
|
+
|
|
|
+ if Unicode {
|
|
|
+ output += strings.Repeat("\u2588", steps/2)
|
|
|
+ } else {
|
|
|
+ output += strings.Repeat("\xdb", steps/2)
|
|
|
+ }
|
|
|
+ if steps%1 == 1 {
|
|
|
+ if Unicode {
|
|
|
+ output += "\u258c"
|
|
|
+ } else {
|
|
|
+ output += "\xdd"
|
|
|
+ }
|
|
|
+ steps++
|
|
|
+ }
|
|
|
+ output += strings.Repeat(" ", bl.Width-(steps/2))
|
|
|
+
|
|
|
+ case GRADIENT:
|
|
|
+ step_width = int64(100 * 100 / bl.Width)
|
|
|
+ var steps int = int(bl.Percent * 4 / step_width)
|
|
|
+ if Unicode {
|
|
|
+ output += strings.Repeat("\u2588", steps/4)
|
|
|
+ } else {
|
|
|
+ output += strings.Repeat("\xb0", steps/4)
|
|
|
+ }
|
|
|
+ if steps%4 != 0 {
|
|
|
+ switch steps % 4 {
|
|
|
+ case 1:
|
|
|
+ if Unicode {
|
|
|
+ output += "\u2591"
|
|
|
+ } else {
|
|
|
+ output += "\xb0"
|
|
|
+ }
|
|
|
+ case 2:
|
|
|
+ if Unicode {
|
|
|
+ output += "\u2592"
|
|
|
+ } else {
|
|
|
+ output += "\xb1"
|
|
|
+ }
|
|
|
+ case 3:
|
|
|
+ if Unicode {
|
|
|
+ output += "\u2593"
|
|
|
+ } else {
|
|
|
+ output += "\xb2"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for steps%4 != 0 {
|
|
|
+ steps++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ output += strings.Repeat(" ", bl.Width-(steps/4))
|
|
|
+ }
|
|
|
+
|
|
|
+ return bl.DefaultColor + output
|
|
|
+}
|