11package asciiroute
22
33import (
4+ "context"
45 "fmt"
6+ "log/slog"
57 "math"
68
79 "oss.terrastruct.com/d2/d2target"
810 "oss.terrastruct.com/d2/lib/geo"
11+ "oss.terrastruct.com/d2/lib/log"
912)
1013
11- func drawSegmentBetweenPoints (rd RouteDrawer , start , end * geo.Point , segmentIndex int , conn d2target.Connection ,
14+ func drawSegmentBetweenPoints (ctx context. Context , rd RouteDrawer , start , end * geo.Point , segmentIndex int , conn d2target.Connection ,
1215 corners , arrows , turnDir map [string ]string , frmBoundary , toBoundary Boundary , labelPos * RouteLabelPosition , label string ) {
1316
1417 ax , ay := start .X , start .Y
1518 cx , cy := end .X , end .Y
1619
17- fmt .Printf ("[D2ASCII] Drawing segment %d: (%.2f,%.2f) -> (%.2f,%.2f)\n " ,
18- segmentIndex - 1 , ax , ay , cx , cy )
20+ log .Debug (ctx , "drawing segment" , slog .Int ("index" , segmentIndex - 1 ), slog .Float64 ("x1" , ax ), slog .Float64 ("y1" , ay ), slog .Float64 ("x2" , cx ), slog .Float64 ("y2" , cy ))
1921
2022 sx := cx - ax
2123 sy := cy - ay
2224 step := math .Max (math .Abs (sx ), math .Abs (sy ))
2325 if step == 0 {
24- fmt . Printf ( "[D2ASCII] Zero -length segment, skipping\n " )
26+ log . Debug ( ctx , "zero -length segment, skipping" )
2527 return
2628 }
2729 sx /= step
2830 sy /= step
2931
30- fmt . Printf ( "[D2ASCII] Step vector: (%.2f, %.2f ), total steps: %.0f \n " , sx , sy , step )
32+ log . Debug ( ctx , "step vector" , slog . Float64 ( "x" , sx ), slog . Float64 ( "y" , sy ), slog . Float64 ( "steps" , step ) )
3133
3234 fx , fy := ax , ay
3335 attempt := 0
@@ -38,9 +40,9 @@ func drawSegmentBetweenPoints(rd RouteDrawer, start, end *geo.Point, segmentInde
3840 attempt ++
3941 if x == int (math .Round (cx )) && y == int (math .Round (cy )) || attempt == MaxRouteAttempts {
4042 if attempt == MaxRouteAttempts {
41- fmt . Printf ( "[D2ASCII] Max route attempts (%d) reached\n " , MaxRouteAttempts )
43+ log . Debug ( ctx , "max route attempts reached" , slog . Int ( "attempts" , MaxRouteAttempts ) )
4244 } else {
43- fmt . Printf ( "[D2ASCII] Reached segment endpoint at (%d, %d) \n " , x , y )
45+ log . Debug ( ctx , "reached segment endpoint" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
4446 }
4547 break
4648 }
@@ -49,13 +51,13 @@ func drawSegmentBetweenPoints(rd RouteDrawer, start, end *geo.Point, segmentInde
4951
5052 // Skip if out of bounds or contains alphanumeric character
5153 if ! isInBounds (rd , x , y ) {
52- fmt . Printf ( "[D2ASCII] Position (%d, %d) out of bounds, skipping\n " , x , y )
54+ log . Debug ( ctx , "position out of bounds, skipping" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
5355 fx += sx
5456 fy += sy
5557 continue
5658 }
5759 if containsAlphaNumeric (rd , x , y ) {
58- fmt . Printf ( "[D2ASCII] Position (%d, %d) contains alphanumeric, skipping\n " , x , y )
60+ log . Debug ( ctx , "position contains alphanumeric, skipping" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
5961 fx += sx
6062 fy += sy
6163 continue
@@ -85,41 +87,40 @@ func drawRoutePoint(rd RouteDrawer, x, y int, sx, sy float64, segmentIndex, rout
8587
8688 // Check for corners first
8789 if char , ok := corners [turnDir [key ]]; ok {
88- fmt . Printf ( "[D2ASCII] Drawing corner at (%d, %d): '%s' (direction: %s) \n " , x , y , char , turnDir [key ])
90+ log . Debug ( rd . GetContext (), "drawing corner" , slog . Int ( "x" , x ), slog . Int ( "y" , y ), slog . String ( "char" , char ), slog . String ( "direction" , turnDir [key ]) )
8991 canvas .Set (x , y , char )
9092 return
9193 }
9294
9395 // Check for destination arrow
9496 if segmentIndex == routeLen - 1 && x == int (math .Round (cx )) && y == int (math .Round (cy )) && conn .DstArrow != d2target .NoArrowhead {
95- fmt . Printf ( "[D2ASCII] Drawing destination arrow at (%d, %d) \n " , x , y )
97+ log . Debug ( rd . GetContext (), "drawing destination arrow" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
9698 drawArrowhead (rd , x , y , sx , sy , arrows )
9799 if conn .DstLabel != nil {
98- fmt . Printf ( "[D2ASCII] Drawing destination label: %s \n " , conn .DstLabel .Label )
100+ log . Debug ( rd . GetContext (), "drawing destination label" , slog . String ( "label" , conn .DstLabel .Label ) )
99101 drawDestinationLabel (rd , conn .DstLabel .Label , cx , cy , sx , sy )
100102 }
101103 return
102104 }
103105
104106 // Check for source arrow
105107 if segmentIndex == 1 && x == int (math .Round (ax )) && y == int (math .Round (ay )) && conn .SrcArrow != d2target .NoArrowhead {
106- fmt . Printf ( "[D2ASCII] Drawing source arrow at (%d, %d) \n " , x , y )
108+ log . Debug ( rd . GetContext (), "drawing source arrow" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
107109 arrowKey := fmt .Sprintf ("%d%d" , geo .Sign (sx )* - 1 , geo .Sign (sy )* - 1 )
108110 canvas .Set (x , y , arrows [arrowKey ])
109111 if conn .SrcLabel != nil {
110- fmt . Printf ( "[D2ASCII] Drawing source label: %s \n " , conn .SrcLabel .Label )
112+ log . Debug ( rd . GetContext (), "drawing source label" , slog . String ( "label" , conn .SrcLabel .Label ) )
111113 drawSourceLabel (rd , conn .SrcLabel .Label , ax , cy , cx , sx , sy )
112114 }
113115 return
114116 }
115117
116118 // Default: draw route segment
117- fmt .Printf ("[D2ASCII] Drawing route segment at (%d, %d), existing: '%s'\n " ,
118- x , y , existingChar )
119- drawRouteSegment (rd , x , y , sx , sy , frmBoundary , toBoundary )
119+ log .Debug (rd .GetContext (), "drawing route segment" , slog .Int ("x" , x ), slog .Int ("y" , y ), slog .String ("existing" , string (existingChar )))
120+ drawRouteSegment (rd .GetContext (), rd , x , y , sx , sy , frmBoundary , toBoundary )
120121}
121122
122- func drawRouteSegment (rd RouteDrawer , x , y int , sx , sy float64 , frmBoundary , toBoundary Boundary ) {
123+ func drawRouteSegment (ctx context. Context , rd RouteDrawer , x , y int , sx , sy float64 , frmBoundary , toBoundary Boundary ) {
123124 if ! isInBounds (rd , x , y ) {
124125 return
125126 }
@@ -129,56 +130,53 @@ func drawRouteSegment(rd RouteDrawer, x, y int, sx, sy float64, frmBoundary, toB
129130 overWrite := existingChar != " "
130131
131132 if sx == 0 { // Vertical line
132- fmt .Printf ("[D2ASCII] Drawing vertical segment at (%d, %d), overwrite=%t, existing='%s'\n " ,
133- x , y , overWrite , existingChar )
134- drawVerticalSegment (rd , x , y , sy , overWrite , frmBoundary , toBoundary )
133+ log .Debug (ctx , "drawing vertical segment" , slog .Int ("x" , x ), slog .Int ("y" , y ), slog .Bool ("overwrite" , overWrite ), slog .String ("existing" , string (existingChar )))
134+ drawVerticalSegment (ctx , rd , x , y , sy , overWrite , frmBoundary , toBoundary )
135135 } else { // Horizontal line
136- fmt .Printf ("[D2ASCII] Drawing horizontal segment at (%d, %d), overwrite=%t, existing='%s'\n " ,
137- x , y , overWrite , existingChar )
138- drawHorizontalSegment (rd , x , y , sx , overWrite , frmBoundary , toBoundary )
136+ log .Debug (ctx , "drawing horizontal segment" , slog .Int ("x" , x ), slog .Int ("y" , y ), slog .Bool ("overwrite" , overWrite ), slog .String ("existing" , string (existingChar )))
137+ drawHorizontalSegment (ctx , rd , x , y , sx , overWrite , frmBoundary , toBoundary )
139138 }
140139
141140 newChar := canvas .Get (x , y )
142141 if newChar != existingChar {
143- fmt .Printf ("[D2ASCII] Character placed: '%s' -> '%s' at (%d, %d)\n " ,
144- existingChar , newChar , x , y )
142+ log .Debug (ctx , "character placed" , slog .String ("from" , string (existingChar )), slog .String ("to" , string (newChar )), slog .Int ("x" , x ), slog .Int ("y" , y ))
145143 }
146144}
147145
148- func drawVerticalSegment (rd RouteDrawer , x , y int , sy float64 , overWrite bool , frmBoundary , toBoundary Boundary ) {
146+ func drawVerticalSegment (ctx context. Context , rd RouteDrawer , x , y int , sy float64 , overWrite bool , frmBoundary , toBoundary Boundary ) {
149147 canvas := rd .GetCanvas ()
150148 chars := rd .GetChars ()
151149
152150 if overWrite && shouldDrawTJunction (rd , x , y , frmBoundary , toBoundary , true ) {
153151 if sy > 0 {
154- fmt . Printf ( "[D2ASCII] Drawing T-junction down at (%d, %d) \n " , x , y )
152+ log . Debug ( ctx , "drawing T-junction down" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
155153 canvas .Set (x , y , chars .TDown ())
156154 } else {
157- fmt . Printf ( "[D2ASCII] Drawing T-junction up at (%d, %d) \n " , x , y )
155+ log . Debug ( ctx , "drawing T-junction up" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
158156 canvas .Set (x , y , chars .TUp ())
159157 }
160158 } else if overWrite && shouldSkipOverwrite (rd , x , y , frmBoundary , toBoundary ) {
161- fmt . Printf ( "[D2ASCII] Skipping overwrite at (%d, %d) \n " , x , y )
159+ log . Debug ( ctx , "skipping overwrite" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
162160 } else {
163- fmt . Printf ( "[D2ASCII] Drawing vertical line at (%d, %d) \n " , x , y )
161+ log . Debug ( ctx , "drawing vertical line" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
164162 canvas .Set (x , y , chars .Vertical ())
165163 }
166164}
167165
168- func drawHorizontalSegment (rd RouteDrawer , x , y int , sx float64 , overWrite bool , frmBoundary , toBoundary Boundary ) {
166+ func drawHorizontalSegment (ctx context. Context , rd RouteDrawer , x , y int , sx float64 , overWrite bool , frmBoundary , toBoundary Boundary ) {
169167 canvas := rd .GetCanvas ()
170168 chars := rd .GetChars ()
171169
172170 if overWrite && shouldDrawTJunction (rd , x , y , frmBoundary , toBoundary , false ) {
173171 if sx > 0 {
174- fmt . Printf ( "[D2ASCII] Drawing T-junction right at (%d, %d) \n " , x , y )
172+ log . Debug ( ctx , "drawing T-junction right" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
175173 canvas .Set (x , y , chars .TRight ())
176174 } else {
177- fmt . Printf ( "[D2ASCII] Drawing T-junction left at (%d, %d) \n " , x , y )
175+ log . Debug ( ctx , "drawing T-junction left" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
178176 canvas .Set (x , y , chars .TLeft ())
179177 }
180178 } else {
181- fmt . Printf ( "[D2ASCII] Drawing horizontal line at (%d, %d) \n " , x , y )
179+ log . Debug ( ctx , "drawing horizontal line" , slog . Int ( "x" , x ), slog . Int ( "y" , y ) )
182180 canvas .Set (x , y , chars .Horizontal ())
183181 }
184182}
0 commit comments