| 1 |
package renderer |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/fatih/color" |
| 7 |
"github.com/olekukonko/tablewriter/tw" |
| 8 |
) |
| 9 |
|
| 10 |
// defaultBlueprint returns a default Rendition for ASCII table rendering with borders and light symbols. |
| 11 |
func defaultBlueprint() tw.Rendition { |
| 12 |
return tw.Rendition{ |
| 13 |
Borders: tw.Border{ |
| 14 |
Left: tw.On, |
| 15 |
Right: tw.On, |
| 16 |
Top: tw.On, |
| 17 |
Bottom: tw.On, |
| 18 |
}, |
| 19 |
Settings: tw.Settings{ |
| 20 |
Separators: tw.Separators{ |
| 21 |
ShowHeader: tw.On, |
| 22 |
ShowFooter: tw.On, |
| 23 |
BetweenRows: tw.Off, |
| 24 |
BetweenColumns: tw.On, |
| 25 |
}, |
| 26 |
Lines: tw.Lines{ |
| 27 |
ShowTop: tw.On, |
| 28 |
ShowBottom: tw.On, |
| 29 |
ShowHeaderLine: tw.On, |
| 30 |
ShowFooterLine: tw.On, |
| 31 |
}, |
| 32 |
CompactMode: tw.Off, |
| 33 |
// Cushion: tw.On, |
| 34 |
}, |
| 35 |
Symbols: tw.NewSymbols(tw.StyleLight), |
| 36 |
Streaming: true, |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// defaultColorized returns a default ColorizedConfig optimized for dark terminal backgrounds with colored headers, rows, and borders. |
| 41 |
func defaultColorized() ColorizedConfig { |
| 42 |
return ColorizedConfig{ |
| 43 |
Borders: tw.Border{Left: tw.On, Right: tw.On, Top: tw.On, Bottom: tw.On}, |
| 44 |
Settings: tw.Settings{ |
| 45 |
Separators: tw.Separators{ |
| 46 |
ShowHeader: tw.On, |
| 47 |
ShowFooter: tw.On, |
| 48 |
BetweenRows: tw.Off, |
| 49 |
BetweenColumns: tw.On, |
| 50 |
}, |
| 51 |
Lines: tw.Lines{ |
| 52 |
ShowTop: tw.On, |
| 53 |
ShowBottom: tw.On, |
| 54 |
ShowHeaderLine: tw.On, |
| 55 |
ShowFooterLine: tw.On, |
| 56 |
}, |
| 57 |
|
| 58 |
CompactMode: tw.Off, |
| 59 |
}, |
| 60 |
Header: Tint{ |
| 61 |
FG: Colors{color.FgWhite, color.Bold}, |
| 62 |
BG: Colors{color.BgBlack}, |
| 63 |
}, |
| 64 |
Column: Tint{ |
| 65 |
FG: Colors{color.FgCyan}, |
| 66 |
BG: Colors{color.BgBlack}, |
| 67 |
}, |
| 68 |
Footer: Tint{ |
| 69 |
FG: Colors{color.FgYellow}, |
| 70 |
BG: Colors{color.BgBlack}, |
| 71 |
}, |
| 72 |
Border: Tint{ |
| 73 |
FG: Colors{color.FgWhite}, |
| 74 |
BG: Colors{color.BgBlack}, |
| 75 |
}, |
| 76 |
Separator: Tint{ |
| 77 |
FG: Colors{color.FgWhite}, |
| 78 |
BG: Colors{color.BgBlack}, |
| 79 |
}, |
| 80 |
Symbols: tw.NewSymbols(tw.StyleLight), |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// defaultOceanRendererConfig returns a base tw.Rendition for the Ocean renderer. |
| 85 |
func defaultOceanRendererConfig() tw.Rendition { |
| 86 |
return tw.Rendition{ |
| 87 |
Borders: tw.Border{ |
| 88 |
Left: tw.On, Right: tw.On, Top: tw.On, Bottom: tw.On, |
| 89 |
}, |
| 90 |
Settings: tw.Settings{ |
| 91 |
Separators: tw.Separators{ |
| 92 |
ShowHeader: tw.On, |
| 93 |
ShowFooter: tw.Off, |
| 94 |
BetweenRows: tw.Off, |
| 95 |
BetweenColumns: tw.On, |
| 96 |
}, |
| 97 |
Lines: tw.Lines{ |
| 98 |
ShowTop: tw.On, |
| 99 |
ShowBottom: tw.On, |
| 100 |
ShowHeaderLine: tw.On, |
| 101 |
ShowFooterLine: tw.Off, |
| 102 |
}, |
| 103 |
|
| 104 |
CompactMode: tw.Off, |
| 105 |
}, |
| 106 |
Symbols: tw.NewSymbols(tw.StyleDefault), |
| 107 |
Streaming: true, |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// getHTMLStyle remains the same |
| 112 |
func getHTMLStyle(align tw.Align) string { |
| 113 |
styleContent := tw.Empty |
| 114 |
switch align { |
| 115 |
case tw.AlignRight: |
| 116 |
styleContent = "text-align: right;" |
| 117 |
case tw.AlignCenter: |
| 118 |
styleContent = "text-align: center;" |
| 119 |
case tw.AlignLeft: |
| 120 |
styleContent = "text-align: left;" |
| 121 |
} |
| 122 |
if styleContent != tw.Empty { |
| 123 |
return fmt.Sprintf(` style="%s"`, styleContent) |
| 124 |
} |
| 125 |
return tw.Empty |
| 126 |
} |
| 127 |
|
| 128 |
// mergeLines combines default and override line settings, preserving defaults for unset (zero) overrides. |
| 129 |
func mergeLines(defaults, overrides tw.Lines) tw.Lines { |
| 130 |
if overrides.ShowTop != 0 { |
| 131 |
defaults.ShowTop = overrides.ShowTop |
| 132 |
} |
| 133 |
if overrides.ShowBottom != 0 { |
| 134 |
defaults.ShowBottom = overrides.ShowBottom |
| 135 |
} |
| 136 |
if overrides.ShowHeaderLine != 0 { |
| 137 |
defaults.ShowHeaderLine = overrides.ShowHeaderLine |
| 138 |
} |
| 139 |
if overrides.ShowFooterLine != 0 { |
| 140 |
defaults.ShowFooterLine = overrides.ShowFooterLine |
| 141 |
} |
| 142 |
return defaults |
| 143 |
} |
| 144 |
|
| 145 |
// mergeSeparators combines default and override separator settings, preserving defaults for unset (zero) overrides. |
| 146 |
func mergeSeparators(defaults, overrides tw.Separators) tw.Separators { |
| 147 |
if overrides.ShowHeader != 0 { |
| 148 |
defaults.ShowHeader = overrides.ShowHeader |
| 149 |
} |
| 150 |
if overrides.ShowFooter != 0 { |
| 151 |
defaults.ShowFooter = overrides.ShowFooter |
| 152 |
} |
| 153 |
if overrides.BetweenRows != 0 { |
| 154 |
defaults.BetweenRows = overrides.BetweenRows |
| 155 |
} |
| 156 |
if overrides.BetweenColumns != 0 { |
| 157 |
defaults.BetweenColumns = overrides.BetweenColumns |
| 158 |
} |
| 159 |
return defaults |
| 160 |
} |
| 161 |
|
| 162 |
// mergeSettings combines default and override settings, preserving defaults for unset (zero) overrides. |
| 163 |
func mergeSettings(defaults, overrides tw.Settings) tw.Settings { |
| 164 |
if overrides.Separators.ShowHeader != tw.Unknown { |
| 165 |
defaults.Separators.ShowHeader = overrides.Separators.ShowHeader |
| 166 |
} |
| 167 |
if overrides.Separators.ShowFooter != tw.Unknown { |
| 168 |
defaults.Separators.ShowFooter = overrides.Separators.ShowFooter |
| 169 |
} |
| 170 |
if overrides.Separators.BetweenRows != tw.Unknown { |
| 171 |
defaults.Separators.BetweenRows = overrides.Separators.BetweenRows |
| 172 |
} |
| 173 |
if overrides.Separators.BetweenColumns != tw.Unknown { |
| 174 |
defaults.Separators.BetweenColumns = overrides.Separators.BetweenColumns |
| 175 |
} |
| 176 |
if overrides.Lines.ShowTop != tw.Unknown { |
| 177 |
defaults.Lines.ShowTop = overrides.Lines.ShowTop |
| 178 |
} |
| 179 |
if overrides.Lines.ShowBottom != tw.Unknown { |
| 180 |
defaults.Lines.ShowBottom = overrides.Lines.ShowBottom |
| 181 |
} |
| 182 |
if overrides.Lines.ShowHeaderLine != tw.Unknown { |
| 183 |
defaults.Lines.ShowHeaderLine = overrides.Lines.ShowHeaderLine |
| 184 |
} |
| 185 |
if overrides.Lines.ShowFooterLine != tw.Unknown { |
| 186 |
defaults.Lines.ShowFooterLine = overrides.Lines.ShowFooterLine |
| 187 |
} |
| 188 |
|
| 189 |
if overrides.CompactMode != tw.Unknown { |
| 190 |
defaults.CompactMode = overrides.CompactMode |
| 191 |
} |
| 192 |
|
| 193 |
// if overrides.Cushion != tw.Unknown { |
| 194 |
// defaults.Cushion = overrides.Cushion |
| 195 |
//} |
| 196 |
|
| 197 |
return defaults |
| 198 |
} |
| 199 |
|
| 200 |
// MergeRendition merges the 'override' rendition into the 'current' rendition. |
| 201 |
// It only updates fields in 'current' if they are explicitly set (non-zero/non-nil) in 'override'. |
| 202 |
// This allows for partial updates to a renderer's configuration. |
| 203 |
func mergeRendition(current, override tw.Rendition) tw.Rendition { |
| 204 |
// Merge Borders: Only update if override border states are explicitly set (not 0). |
| 205 |
// A tw.State's zero value is 0, which is distinct from tw.On (1) or tw.Off (-1). |
| 206 |
// So, if override.Borders.Left is 0, it means "not specified", so we keep current. |
| 207 |
if override.Borders.Left != 0 { |
| 208 |
current.Borders.Left = override.Borders.Left |
| 209 |
} |
| 210 |
if override.Borders.Right != 0 { |
| 211 |
current.Borders.Right = override.Borders.Right |
| 212 |
} |
| 213 |
if override.Borders.Top != 0 { |
| 214 |
current.Borders.Top = override.Borders.Top |
| 215 |
} |
| 216 |
if override.Borders.Bottom != 0 { |
| 217 |
current.Borders.Bottom = override.Borders.Bottom |
| 218 |
} |
| 219 |
|
| 220 |
// Merge Symbols: Only update if override.Symbols is not nil. |
| 221 |
if override.Symbols != nil { |
| 222 |
current.Symbols = override.Symbols |
| 223 |
} |
| 224 |
|
| 225 |
// Merge Settings: Use the existing mergeSettings for granular control. |
| 226 |
// mergeSettings already handles preserving defaults for unset (zero) overrides. |
| 227 |
current.Settings = mergeSettings(current.Settings, override.Settings) |
| 228 |
|
| 229 |
// Streaming flag: typically set at renderer creation, but can be overridden if needed. |
| 230 |
// For now, let's assume it's not commonly changed post-creation by a generic rendition merge. |
| 231 |
// If override provides a different streaming capability, it might indicate a fundamental |
| 232 |
// change that a simple merge shouldn't handle without more context. |
| 233 |
// current.Streaming = override.Streaming // Or keep current.Streaming |
| 234 |
|
| 235 |
return current |
| 236 |
} |