Debian Codegraph @

renderer/junction.go

raw
1 package renderer
2
3 import (
4 "github.com/olekukonko/ll"
5 "github.com/olekukonko/tablewriter/tw"
6 )
7
8 // Junction handles rendering of table junction points (corners, intersections) with color support.
9 type Junction struct {
10 sym tw.Symbols // Symbols used for rendering junctions and lines
11 ctx tw.Formatting // Current table formatting context
12 colIdx int // Index of the column being processed
13 debugging bool // Enables debug logging
14 borderTint Tint // Colors for border symbols
15 separatorTint Tint // Colors for separator symbols
16 logger *ll.Logger
17 }
18
19 type JunctionContext struct {
20 Symbols tw.Symbols
21 Ctx tw.Formatting
22 ColIdx int
23 Logger *ll.Logger
24 BorderTint Tint
25 SeparatorTint Tint
26 }
27
28 // NewJunction initializes a Junction with the given symbols, context, and tints.
29 // If debug is nil, a no-op debug function is used.
30 func NewJunction(ctx JunctionContext) *Junction {
31 return &Junction{
32 sym: ctx.Symbols,
33 ctx: ctx.Ctx,
34 colIdx: ctx.ColIdx,
35 logger: ctx.Logger.Namespace("junction"),
36 borderTint: ctx.BorderTint,
37 separatorTint: ctx.SeparatorTint,
38 }
39 }
40
41 // getMergeState retrieves the merge state for a specific column in a row, returning an empty state if not found.
42 func (jr *Junction) getMergeState(row map[int]tw.CellContext, colIdx int) tw.MergeState {
43 if row == nil || colIdx < 0 {
44 return tw.MergeState{}
45 }
46 return row[colIdx].Merge
47 }
48
49 // GetSegment determines whether to render a colored horizontal line or an empty space based on merge states.
50 func (jr *Junction) GetSegment() string {
51 currentMerge := jr.getMergeState(jr.ctx.Row.Current, jr.colIdx)
52 nextMerge := jr.getMergeState(jr.ctx.Row.Next, jr.colIdx)
53
54 vPassThruStrict := (currentMerge.Vertical.Present && nextMerge.Vertical.Present && !currentMerge.Vertical.End && !nextMerge.Vertical.Start) ||
55 (currentMerge.Hierarchical.Present && nextMerge.Hierarchical.Present && !currentMerge.Hierarchical.End && !nextMerge.Hierarchical.Start)
56
57 if vPassThruStrict {
58 jr.logger.Debugf("GetSegment col %d: VPassThruStrict=%v -> Empty segment", jr.colIdx, vPassThruStrict)
59 return tw.Empty
60 }
61 symbol := jr.sym.Row()
62 coloredSymbol := jr.borderTint.Apply(symbol)
63 jr.logger.Debugf("GetSegment col %d: VPassThruStrict=%v -> Colored row symbol '%s'", jr.colIdx, vPassThruStrict, coloredSymbol)
64 return coloredSymbol
65 }
66
67 // RenderLeft selects and colors the leftmost junction symbol for the current row line based on position and merges.
68 func (jr *Junction) RenderLeft() string {
69 mergeAbove := jr.getMergeState(jr.ctx.Row.Current, 0)
70 mergeBelow := jr.getMergeState(jr.ctx.Row.Next, 0)
71
72 jr.logger.Debugf("RenderLeft: Level=%v, Location=%v, Previous=%v", jr.ctx.Level, jr.ctx.Row.Location, jr.ctx.Row.Previous)
73
74 isTopBorder := (jr.ctx.Level == tw.LevelHeader && jr.ctx.Row.Location == tw.LocationFirst) ||
75 (jr.ctx.Level == tw.LevelBody && jr.ctx.Row.Location == tw.LocationFirst && jr.ctx.Row.Previous == nil)
76 if isTopBorder {
77 symbol := jr.sym.TopLeft()
78 return jr.borderTint.Apply(symbol)
79 }
80
81 isBottom := jr.ctx.Level == tw.LevelBody && jr.ctx.Row.Location == tw.LocationEnd && !jr.ctx.HasFooter
82 isFooter := jr.ctx.Level == tw.LevelFooter && jr.ctx.Row.Location == tw.LocationEnd
83 if isBottom || isFooter {
84 symbol := jr.sym.BottomLeft()
85 return jr.borderTint.Apply(symbol)
86 }
87
88 isVPassThruStrict := (mergeAbove.Vertical.Present && mergeBelow.Vertical.Present && !mergeAbove.Vertical.End && !mergeBelow.Vertical.Start) ||
89 (mergeAbove.Hierarchical.Present && mergeBelow.Hierarchical.Present && !mergeAbove.Hierarchical.End && !mergeBelow.Hierarchical.Start)
90 if isVPassThruStrict {
91 symbol := jr.sym.Column()
92 return jr.separatorTint.Apply(symbol)
93 }
94
95 symbol := jr.sym.MidLeft()
96 return jr.borderTint.Apply(symbol)
97 }
98
99 // RenderRight selects and colors the rightmost junction symbol for the row line based on position, merges, and last column index.
100 func (jr *Junction) RenderRight(lastColIdx int) string {
101 jr.logger.Debugf("RenderRight: lastColIdx=%d, Level=%v, Location=%v, Previous=%v", lastColIdx, jr.ctx.Level, jr.ctx.Row.Location, jr.ctx.Row.Previous)
102
103 if lastColIdx < 0 {
104 switch jr.ctx.Level {
105 case tw.LevelHeader:
106 symbol := jr.sym.TopRight()
107 return jr.borderTint.Apply(symbol)
108 case tw.LevelFooter:
109 symbol := jr.sym.BottomRight()
110 return jr.borderTint.Apply(symbol)
111 default:
112 if jr.ctx.Row.Location == tw.LocationFirst {
113 symbol := jr.sym.TopRight()
114 return jr.borderTint.Apply(symbol)
115 }
116 if jr.ctx.Row.Location == tw.LocationEnd {
117 symbol := jr.sym.BottomRight()
118 return jr.borderTint.Apply(symbol)
119 }
120 symbol := jr.sym.MidRight()
121 return jr.borderTint.Apply(symbol)
122 }
123 }
124
125 mergeAbove := jr.getMergeState(jr.ctx.Row.Current, lastColIdx)
126 mergeBelow := jr.getMergeState(jr.ctx.Row.Next, lastColIdx)
127
128 isTopBorder := (jr.ctx.Level == tw.LevelHeader && jr.ctx.Row.Location == tw.LocationFirst) ||
129 (jr.ctx.Level == tw.LevelBody && jr.ctx.Row.Location == tw.LocationFirst && jr.ctx.Row.Previous == nil)
130 if isTopBorder {
131 symbol := jr.sym.TopRight()
132 return jr.borderTint.Apply(symbol)
133 }
134
135 isBottom := jr.ctx.Level == tw.LevelBody && jr.ctx.Row.Location == tw.LocationEnd && !jr.ctx.HasFooter
136 isFooter := jr.ctx.Level == tw.LevelFooter && jr.ctx.Row.Location == tw.LocationEnd
137 if isBottom || isFooter {
138 symbol := jr.sym.BottomRight()
139 return jr.borderTint.Apply(symbol)
140 }
141
142 isVPassThruStrict := (mergeAbove.Vertical.Present && mergeBelow.Vertical.Present && !mergeAbove.Vertical.End && !mergeBelow.Vertical.Start) ||
143 (mergeAbove.Hierarchical.Present && mergeBelow.Hierarchical.Present && !mergeAbove.Hierarchical.End && !mergeBelow.Hierarchical.Start)
144 if isVPassThruStrict {
145 symbol := jr.sym.Column()
146 return jr.separatorTint.Apply(symbol)
147 }
148
149 symbol := jr.sym.MidRight()
150 return jr.borderTint.Apply(symbol)
151 }
152
153 // RenderJunction selects and colors the junction symbol between two adjacent columns based on merge states and table position.
154 func (jr *Junction) RenderJunction(leftColIdx, rightColIdx int) string {
155 mergeCurrentL := jr.getMergeState(jr.ctx.Row.Current, leftColIdx)
156 mergeCurrentR := jr.getMergeState(jr.ctx.Row.Current, rightColIdx)
157 mergeNextL := jr.getMergeState(jr.ctx.Row.Next, leftColIdx)
158 mergeNextR := jr.getMergeState(jr.ctx.Row.Next, rightColIdx)
159
160 isSpannedCurrent := mergeCurrentL.Horizontal.Present && !mergeCurrentL.Horizontal.End
161 isSpannedNext := mergeNextL.Horizontal.Present && !mergeNextL.Horizontal.End
162
163 vPassThruLStrict := (mergeCurrentL.Vertical.Present && mergeNextL.Vertical.Present && !mergeCurrentL.Vertical.End && !mergeNextL.Vertical.Start) ||
164 (mergeCurrentL.Hierarchical.Present && mergeNextL.Hierarchical.Present && !mergeCurrentL.Hierarchical.End && !mergeNextL.Hierarchical.Start)
165 vPassThruRStrict := (mergeCurrentR.Vertical.Present && mergeNextR.Vertical.Present && !mergeCurrentR.Vertical.End && !mergeNextR.Vertical.Start) ||
166 (mergeCurrentR.Hierarchical.Present && mergeNextR.Hierarchical.Present && !mergeCurrentR.Hierarchical.End && !mergeNextR.Hierarchical.Start)
167
168 isTop := (jr.ctx.Level == tw.LevelHeader && jr.ctx.Row.Location == tw.LocationFirst) ||
169 (jr.ctx.Level == tw.LevelBody && jr.ctx.Row.Location == tw.LocationFirst && len(jr.ctx.Row.Previous) == 0)
170 isBottom := (jr.ctx.Level == tw.LevelFooter && jr.ctx.Row.Location == tw.LocationEnd) ||
171 (jr.ctx.Level == tw.LevelBody && jr.ctx.Row.Location == tw.LocationEnd && !jr.ctx.HasFooter)
172 isPreFooter := jr.ctx.Level == tw.LevelFooter && (jr.ctx.Row.Position == tw.Row || jr.ctx.Row.Position == tw.Header)
173
174 if isTop {
175 if isSpannedNext {
176 symbol := jr.sym.Row()
177 return jr.borderTint.Apply(symbol)
178 }
179 symbol := jr.sym.TopMid()
180 return jr.borderTint.Apply(symbol)
181 }
182
183 if isBottom {
184 if vPassThruLStrict && vPassThruRStrict {
185 symbol := jr.sym.Column()
186 return jr.separatorTint.Apply(symbol)
187 }
188 if vPassThruLStrict {
189 symbol := jr.sym.MidLeft()
190 return jr.borderTint.Apply(symbol)
191 }
192 if vPassThruRStrict {
193 symbol := jr.sym.MidRight()
194 return jr.borderTint.Apply(symbol)
195 }
196 if isSpannedCurrent {
197 symbol := jr.sym.Row()
198 return jr.borderTint.Apply(symbol)
199 }
200 symbol := jr.sym.BottomMid()
201 return jr.borderTint.Apply(symbol)
202 }
203
204 if isPreFooter {
205 if vPassThruLStrict && vPassThruRStrict {
206 symbol := jr.sym.Column()
207 return jr.separatorTint.Apply(symbol)
208 }
209 if vPassThruLStrict {
210 symbol := jr.sym.MidLeft()
211 return jr.borderTint.Apply(symbol)
212 }
213 if vPassThruRStrict {
214 symbol := jr.sym.MidRight()
215 return jr.borderTint.Apply(symbol)
216 }
217 if mergeCurrentL.Horizontal.Present {
218 if !mergeCurrentL.Horizontal.End && mergeCurrentR.Horizontal.Present && !mergeCurrentR.Horizontal.End {
219 jr.logger.Debugf("Footer separator: H-merge continues from col %d to %d (mid-span), using BottomMid", leftColIdx, rightColIdx)
220 symbol := jr.sym.BottomMid()
221 return jr.borderTint.Apply(symbol)
222 }
223 if !mergeCurrentL.Horizontal.End && mergeCurrentR.Horizontal.Present && mergeCurrentR.Horizontal.End {
224 jr.logger.Debugf("Footer separator: H-merge ends at col %d, using BottomMid", rightColIdx)
225 symbol := jr.sym.BottomMid()
226 return jr.borderTint.Apply(symbol)
227 }
228 if mergeCurrentL.Horizontal.End && !mergeCurrentR.Horizontal.Present {
229 jr.logger.Debugf("Footer separator: H-merge ends at col %d, next col %d not merged, using Center", leftColIdx, rightColIdx)
230 symbol := jr.sym.Center()
231 return jr.borderTint.Apply(symbol)
232 }
233 }
234 if isSpannedNext {
235 symbol := jr.sym.BottomMid()
236 return jr.borderTint.Apply(symbol)
237 }
238 if isSpannedCurrent {
239 symbol := jr.sym.TopMid()
240 return jr.borderTint.Apply(symbol)
241 }
242 symbol := jr.sym.Center()
243 return jr.borderTint.Apply(symbol)
244 }
245
246 if vPassThruLStrict && vPassThruRStrict {
247 symbol := jr.sym.Column()
248 return jr.separatorTint.Apply(symbol)
249 }
250 if vPassThruLStrict {
251 symbol := jr.sym.MidLeft()
252 return jr.borderTint.Apply(symbol)
253 }
254 if vPassThruRStrict {
255 symbol := jr.sym.MidRight()
256 return jr.borderTint.Apply(symbol)
257 }
258 if isSpannedCurrent && isSpannedNext {
259 symbol := jr.sym.Row()
260 return jr.borderTint.Apply(symbol)
261 }
262 if isSpannedCurrent {
263 symbol := jr.sym.TopMid()
264 return jr.borderTint.Apply(symbol)
265 }
266 if isSpannedNext {
267 symbol := jr.sym.BottomMid()
268 return jr.borderTint.Apply(symbol)
269 }
270
271 symbol := jr.sym.Center()
272 return jr.borderTint.Apply(symbol)
273 }