Skip to content

Commit dd406ab

Browse files
committed
[devtools] [bugfix] Circ command is printed incorrectly to stdout
pi.Circ and pi.CircFill functions have different parameters than other shape functions like Rect, RectFill and Line.
1 parent c96cc0b commit dd406ab

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

devtools/internal/inspector/shape.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ import (
1212
)
1313

1414
type Shape struct {
15-
start pi.Position
16-
started bool
17-
draw func(x0, y0, x1, y1 int, color byte)
18-
function string
19-
icon byte
15+
start pi.Position
16+
started bool
17+
draw func(x0, y0, x1, y1 int, color byte) string
18+
icon byte
2019
}
2120

2221
func (l *Shape) Update() {
@@ -28,8 +27,8 @@ func (l *Shape) Update() {
2827
x, y := pi.MousePos()
2928
l.started = false
3029
snapshot.Draw()
31-
l.draw(l.start.X, l.start.Y, x, y, FgColor)
32-
fmt.Printf("pi.%s(%d, %d, %d, %d, %d)\n", l.function, l.start.X, l.start.Y, x, y, FgColor)
30+
command := l.draw(l.start.X, l.start.Y, x, y, FgColor)
31+
fmt.Println(command)
3332
snapshot.Take()
3433
}
3534
}

devtools/internal/inspector/tool.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package inspector
55

66
import (
7+
"fmt"
78
"math"
89

910
"github.com/elgopher/pi"
@@ -26,39 +27,42 @@ func selectTool(icon byte) {
2627
tool = &Pset{}
2728
case icons.LineTool:
2829
tool = &Shape{
29-
draw: pi.Line,
30-
function: "Line",
31-
icon: icons.LineTool,
30+
draw: drawShape("Line", pi.Line),
31+
icon: icons.LineTool,
3232
}
3333
case icons.RectTool:
3434
tool = &Shape{
35-
draw: pi.Rect,
36-
function: "Rect",
37-
icon: icons.RectTool,
35+
draw: drawShape("Rect", pi.Rect),
36+
icon: icons.RectTool,
3837
}
3938
case icons.RectFillTool:
4039
tool = &Shape{
41-
draw: pi.RectFill,
42-
function: "RectFill",
43-
icon: icons.RectFillTool,
40+
draw: drawShape("RectFill", pi.RectFill),
41+
icon: icons.RectFillTool,
4442
}
4543
case icons.CircTool:
4644
tool = &Shape{
47-
draw: drawCirc(pi.Circ),
48-
function: "Circ",
49-
icon: icons.CircTool,
45+
draw: drawCirc("Circ", pi.Circ),
46+
icon: icons.CircTool,
5047
}
5148
case icons.CircFillTool:
5249
tool = &Shape{
53-
draw: drawCirc(pi.CircFill),
54-
function: "CircFill",
55-
icon: icons.CircFillTool,
50+
draw: drawCirc("CircFill", pi.CircFill),
51+
icon: icons.CircFillTool,
5652
}
5753
}
5854
}
5955

60-
func drawCirc(f func(cx, cy, r int, color byte)) func(x0 int, y0 int, x1 int, y1 int, color byte) {
61-
return func(x0, y0, x1, y1 int, color byte) {
56+
func drawShape(name string, f func(x0, y0, x1, y1 int, color byte)) func(x0, y0, x1, y1 int, color byte) string {
57+
return func(x0, y0, x1, y1 int, color byte) string {
58+
f(x0, y0, x1, y1, color)
59+
command := fmt.Sprintf("pi.%s(%d, %d, %d, %d, %d)", name, x0, y0, x1, y1, color)
60+
return command
61+
}
62+
}
63+
64+
func drawCirc(name string, f func(cx, cy, r int, color byte)) func(x0 int, y0 int, x1 int, y1 int, color byte) string {
65+
return func(x0, y0, x1, y1 int, color byte) string {
6266
dx := x1 - x0
6367
cx := x0 + dx
6468
if dx < 0 {
@@ -73,5 +77,8 @@ func drawCirc(f func(cx, cy, r int, color byte)) func(x0 int, y0 int, x1 int, y1
7377
r := int(math.Sqrt(float64(dx*dx + dy*dy)))
7478

7579
f(cx, cy, r, FgColor)
80+
81+
command := fmt.Sprintf("pi.%s(%d, %d, %d, %d)", name, cx, cy, r, color)
82+
return command
7683
}
7784
}

0 commit comments

Comments
 (0)