Просмотр исходного кода

Public/export bar and panel.

Fixed goroutine leak in wopr.
Steve Thielemann 2 лет назад
Родитель
Сommit
daf07f22c6
5 измененных файлов с 83 добавлено и 75 удалено
  1. 8 8
      door/bar.go
  2. 34 34
      door/bar_test.go
  3. 12 12
      door/box.go
  4. 10 10
      door/panel.go
  5. 19 11
      door/wopr.go

+ 8 - 8
door/bar.go

@@ -41,9 +41,9 @@ type BarLine struct {
 }
 
 type BarCharacters struct {
-	solid    string
-	half     [2]string
-	gradient [4]string
+	Solid    string
+	Half     [2]string
+	Gradient [4]string
 }
 
 var BARS BarCharacters
@@ -88,7 +88,7 @@ func (bl *BarLine) Output() string {
 		step_width = int64(100 * 100 / bl.Width)
 		var steps int = int(bl.Percent / step_width)
 
-		output += strings.Repeat(BARS.solid, steps)
+		output += strings.Repeat(BARS.Solid, steps)
 		// This will work, because we aren't trying to len(output) with unicode.
 		output += strings.Repeat(" ", int(bl.Width-steps))
 
@@ -96,9 +96,9 @@ func (bl *BarLine) Output() string {
 		step_width = int64(100 * 100 / bl.Width)
 		var steps int = int(bl.Percent * 2 / step_width)
 
-		output += strings.Repeat(BARS.half[0], steps/2)
+		output += strings.Repeat(BARS.Half[0], steps/2)
 		if steps%2 == 1 {
-			output += BARS.half[1]
+			output += BARS.Half[1]
 			steps++
 		}
 		output += strings.Repeat(" ", bl.Width-(steps/2))
@@ -107,11 +107,11 @@ func (bl *BarLine) Output() string {
 		step_width = int64(100 * 100 / bl.Width)
 		var steps int = int(bl.Percent * 4 / step_width)
 
-		output += strings.Repeat(BARS.gradient[0], steps/4)
+		output += strings.Repeat(BARS.Gradient[0], steps/4)
 		if steps%4 != 0 {
 			switch steps % 4 {
 			case 1, 2, 3:
-				output += BARS.gradient[steps%4]
+				output += BARS.Gradient[steps%4]
 			}
 			for steps%4 != 0 {
 				steps++

+ 34 - 34
door/bar_test.go

@@ -15,26 +15,26 @@ func TestBarsMatch(t *testing.T) {
 	// verify that the BARS_CP437 matches BARS_UNICODE
 	// When translated via CP437_to_Unicode().
 
-	cp437 := BARS_CP437.solid
+	cp437 := BARS_CP437.Solid
 	convert := CP437_to_Unicode(cp437)
-	unicode := BARS_UNICODE.solid
+	unicode := BARS_UNICODE.Solid
 	if convert != unicode {
 		t.Errorf("BARS solid: %#v != %#v\n", unicode, convert)
 	}
 
 	for half := 0; half < 2; half++ {
-		cp437 := BARS_CP437.half[half]
+		cp437 := BARS_CP437.Half[half]
 		convert := CP437_to_Unicode(cp437)
-		unicode := BARS_UNICODE.half[half]
+		unicode := BARS_UNICODE.Half[half]
 		if convert != unicode {
 			t.Errorf("BARS half[%d]: %#v != %#v\n", half, unicode, convert)
 		}
 	}
 
 	for grad := 0; grad < 4; grad++ {
-		cp437 := BARS_CP437.gradient[grad]
+		cp437 := BARS_CP437.Gradient[grad]
 		convert := CP437_to_Unicode(cp437)
-		unicode := BARS_UNICODE.gradient[grad]
+		unicode := BARS_UNICODE.Gradient[grad]
 		if convert != unicode {
 			t.Errorf("BARS gradient[%d]: %#v != %#v\n", grad, unicode, convert)
 		}
@@ -79,15 +79,15 @@ func testBars(t *testing.T) {
 
 	BarSolid := map[int]string{0: Bar25 + "          ",
 		5:   Bar25 + "          ",
-		10:  Bar25 + BARS.solid + "         ",
-		20:  Bar25 + strings.Repeat(BARS.solid, 2) + "        ",
-		25:  Bar25 + strings.Repeat(BARS.solid, 2) + "        ",
-		30:  Bar50 + strings.Repeat(BARS.solid, 3) + "       ",
-		50:  Bar50 + strings.Repeat(BARS.solid, 5) + "     ",
-		75:  Bar75 + strings.Repeat(BARS.solid, 7) + "   ",
-		90:  Bar95 + strings.Repeat(BARS.solid, 9) + " ",
-		95:  Bar95 + strings.Repeat(BARS.solid, 9) + " ",
-		100: Bar100 + strings.Repeat(BARS.solid, 10) + "",
+		10:  Bar25 + BARS.Solid + "         ",
+		20:  Bar25 + strings.Repeat(BARS.Solid, 2) + "        ",
+		25:  Bar25 + strings.Repeat(BARS.Solid, 2) + "        ",
+		30:  Bar50 + strings.Repeat(BARS.Solid, 3) + "       ",
+		50:  Bar50 + strings.Repeat(BARS.Solid, 5) + "     ",
+		75:  Bar75 + strings.Repeat(BARS.Solid, 7) + "   ",
+		90:  Bar95 + strings.Repeat(BARS.Solid, 9) + " ",
+		95:  Bar95 + strings.Repeat(BARS.Solid, 9) + " ",
+		100: Bar100 + strings.Repeat(BARS.Solid, 10) + "",
 	}
 
 	for pct, text := range BarSolid {
@@ -101,16 +101,16 @@ func testBars(t *testing.T) {
 	BarColor = ColorText("BLA ON WHI")
 	bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: HALF_STEP, PercentStyle: PERCENT_SPACE}
 	BarHalf := map[int]string{0: BarColor + "     0%   ",
-		5:   BarColor + BARS.half[1] + "    5%   ",
-		6:   BarColor + BARS.half[1] + "    6%   ",
-		10:  BarColor + BARS.half[0] + "   10%   ",
-		20:  BarColor + BARS.half[0] + BARS.half[0] + "  20%   ",
-		25:  BarColor + BARS.half[0] + BARS.half[0] + BARS.half[1] + " 25%   ",
-		26:  BarColor + BARS.half[0] + BARS.half[0] + BARS.half[1] + " 26%   ",
-		50:  BarColor + strings.Repeat(BARS.half[0], 3) + " 50%   ",
-		75:  BarColor + strings.Repeat(BARS.half[0], 3) + " 75%   ",
-		90:  BarColor + strings.Repeat(BARS.half[0], 3) + " 90% " + BARS.half[0] + " ",
-		100: BarColor + strings.Repeat(BARS.half[0], 3) + " 100 " + strings.Repeat(BARS.half[0], 2),
+		5:   BarColor + BARS.Half[1] + "    5%   ",
+		6:   BarColor + BARS.Half[1] + "    6%   ",
+		10:  BarColor + BARS.Half[0] + "   10%   ",
+		20:  BarColor + BARS.Half[0] + BARS.Half[0] + "  20%   ",
+		25:  BarColor + BARS.Half[0] + BARS.Half[0] + BARS.Half[1] + " 25%   ",
+		26:  BarColor + BARS.Half[0] + BARS.Half[0] + BARS.Half[1] + " 26%   ",
+		50:  BarColor + strings.Repeat(BARS.Half[0], 3) + " 50%   ",
+		75:  BarColor + strings.Repeat(BARS.Half[0], 3) + " 75%   ",
+		90:  BarColor + strings.Repeat(BARS.Half[0], 3) + " 90% " + BARS.Half[0] + " ",
+		100: BarColor + strings.Repeat(BARS.Half[0], 3) + " 100 " + strings.Repeat(BARS.Half[0], 2),
 	}
 
 	for pct, text := range BarHalf {
@@ -124,15 +124,15 @@ func testBars(t *testing.T) {
 	BarColor = ColorText("RED")
 	bar = BarLine{Line: Line{DefaultColor: BarColor}, Width: 10, Style: GRADIENT}
 	BarGrad := map[int]string{0: BarColor + "          ",
-		3:   BarColor + BARS.gradient[1] + "         ",
-		5:   BarColor + BARS.gradient[2] + "         ",
-		8:   BarColor + BARS.gradient[3] + "         ",
-		10:  BarColor + BARS.gradient[0] + "         ",
-		20:  BarColor + BARS.gradient[0] + BARS.gradient[0] + "        ",
-		25:  BarColor + BARS.gradient[0] + BARS.gradient[0] + BARS.gradient[2] + "       ",
-		50:  BarColor + strings.Repeat(BARS.gradient[0], 5) + "     ",
-		75:  BarColor + strings.Repeat(BARS.gradient[0], 7) + BARS.gradient[2] + "  ",
-		100: BarColor + strings.Repeat(BARS.gradient[0], 10),
+		3:   BarColor + BARS.Gradient[1] + "         ",
+		5:   BarColor + BARS.Gradient[2] + "         ",
+		8:   BarColor + BARS.Gradient[3] + "         ",
+		10:  BarColor + BARS.Gradient[0] + "         ",
+		20:  BarColor + BARS.Gradient[0] + BARS.Gradient[0] + "        ",
+		25:  BarColor + BARS.Gradient[0] + BARS.Gradient[0] + BARS.Gradient[2] + "       ",
+		50:  BarColor + strings.Repeat(BARS.Gradient[0], 5) + "     ",
+		75:  BarColor + strings.Repeat(BARS.Gradient[0], 7) + BARS.Gradient[2] + "  ",
+		100: BarColor + strings.Repeat(BARS.Gradient[0], 10),
 	}
 
 	var percent int64

+ 12 - 12
door/box.go

@@ -3,14 +3,14 @@ package door
 import "strings"
 
 type BoxStyle struct {
-	top_left     string
-	top_right    string
-	top          string
-	side         string
-	bottom_left  string
-	bottom_right string
-	middle_left  string
-	middle_right string
+	Top_Left     string
+	Top_Right    string
+	Top          string
+	Side         string
+	Bottom_Left  string
+	Bottom_Right string
+	Middle_Left  string
+	Middle_Right string
 }
 
 // FUTURE:
@@ -65,22 +65,22 @@ type Box struct {
 
 func (b *Box) Top() string {
 	var style *BoxStyle = &BOXES[b.Style]
-	return style.top_left + strings.Repeat(style.top, b.Width) + style.top_right
+	return style.Top_Left + strings.Repeat(style.Top, b.Width) + style.Top_Right
 }
 
 func (b *Box) Row(text string) string {
 	var style *BoxStyle = &BOXES[b.Style]
-	return style.side + text + style.side
+	return style.Side + text + style.Side
 }
 
 func (b *Box) Middle() string {
 	var style *BoxStyle = &BOXES[b.Style]
-	return style.middle_left + strings.Repeat(style.top, b.Width) + style.middle_right
+	return style.Middle_Left + strings.Repeat(style.Top, b.Width) + style.Middle_Right
 }
 
 func (b *Box) Bottom() string {
 	var style *BoxStyle = &BOXES[b.Style]
-	return style.bottom_left + strings.Repeat(style.top, b.Width) + style.bottom_right
+	return style.Bottom_Left + strings.Repeat(style.Top, b.Width) + style.Bottom_Right
 }
 
 func AlertBox(text string, style int) []string {

+ 10 - 10
door/panel.go

@@ -57,15 +57,15 @@ func (p *Panel) Output() string {
 	var row int = p.Y
 	if style > 0 {
 		// Top line / border
-		output += Goto(p.X, row) + p.BorderColor + box_style.top_left
+		output += Goto(p.X, row) + p.BorderColor + box_style.Top_Left
 		if p.Title != "" {
 			if p.TitleOffset+len(p.Title) > p.Width {
 				log.Panicf("Panel (not wide enough) Width %d : Title size %d + offset %d = %d\n",
 					p.Width, len(p.Title), p.TitleOffset, p.TitleOffset+len(p.Title))
 			}
-			output += strings.Repeat(box_style.top, p.TitleOffset) + p.TitleColor + p.Title + p.BorderColor
+			output += strings.Repeat(box_style.Top, p.TitleOffset) + p.TitleColor + p.Title + p.BorderColor
 		}
-		output += strings.Repeat(box_style.top, p.Width-(p.TitleOffset+len(p.Title))) + box_style.top_right
+		output += strings.Repeat(box_style.Top, p.Width-(p.TitleOffset+len(p.Title))) + box_style.Top_Right
 		row++
 	}
 
@@ -76,21 +76,21 @@ func (p *Panel) Output() string {
 		var joined bool = false
 
 		if style > 0 {
-			top := box_style.top
+			top := box_style.Top
 			if line.Text[0:len(top)] == top {
 				// Yes, this line needs to be joined...
-				output += p.BorderColor + box_style.middle_left + line.Output() + p.BorderColor + box_style.middle_right
+				output += p.BorderColor + box_style.Middle_Left + line.Output() + p.BorderColor + box_style.Middle_Right
 				joined = true
 			}
 		}
 
 		if !joined {
 			if style > 0 {
-				output += p.BorderColor + box_style.side
+				output += p.BorderColor + box_style.Side
 			}
 			output += line.Output()
 			if style > 0 {
-				output += p.BorderColor + box_style.side
+				output += p.BorderColor + box_style.Side
 			}
 		}
 
@@ -99,8 +99,8 @@ func (p *Panel) Output() string {
 
 	if style > 0 {
 		// Bottom / border
-		output += Goto(p.X, row) + p.BorderColor + box_style.bottom_left
-		output += strings.Repeat(box_style.top, p.Width) + box_style.bottom_right
+		output += Goto(p.X, row) + p.BorderColor + box_style.Bottom_Left
+		output += strings.Repeat(box_style.Top, p.Width) + box_style.Bottom_Right
 	}
 	return output
 }
@@ -183,7 +183,7 @@ func (p *Panel) Spacer() Line {
 		pos = 1
 	}
 
-	l.Text = strings.Repeat(BOXES[pos].top, p.Width)
+	l.Text = strings.Repeat(BOXES[pos].Top, p.Width)
 	return l
 }
 

+ 19 - 11
door/wopr.go

@@ -38,6 +38,7 @@ type WOPR struct {
 	Color          string
 	Index          int
 	Ticker         *time.Ticker
+	StopIt         chan bool
 }
 
 // Initialize, Set X, Y on Panels, Animate()
@@ -158,6 +159,7 @@ func (w *WOPR) Animate(d *Door) {
 	// This gives us consistency when resuming.
 	w.ElapsedD = time.Since(w.Elapsed)
 	w.RemainingD = time.Until(w.Remaining)
+	w.StopIt = make(chan bool)
 
 	go func(d *Door) {
 		// til := time.Now().UnixNano() % int64(time.Second)
@@ -176,24 +178,30 @@ func (w *WOPR) Animate(d *Door) {
 
 		var output string
 
-		for range w.Ticker.C {
+		for {
+			select {
+			case <-w.StopIt:
+				return
 
-			w.ElapsedPanel.Update()
-			w.RemainingPanel.Update()
+			case <-w.Ticker.C:
+				w.ElapsedPanel.Update()
+				w.RemainingPanel.Update()
 
-			output = SavePos + w.ElapsedPanel.Output() + w.RemainingPanel.Output() + RestorePos
-			if !d.Disconnect() {
-				d.Write(output)
-			} else {
-				w.Ticker.Stop()
-				return
-			}
+				output = SavePos + w.ElapsedPanel.Output() + w.RemainingPanel.Output() + RestorePos
+				if !d.Disconnect() {
+					d.Write(output)
+				} else {
+					w.Ticker.Stop()
+					return
+				}
 
-			w.Inc()
+				w.Inc()
+			}
 		}
 	}(d)
 }
 
 func (w *WOPR) Stop() {
 	w.Ticker.Stop()
+	w.StopIt <- true
 }