| 1 |
package tw |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
|
| 6 |
"github.com/olekukonko/ll" |
| 7 |
) |
| 8 |
|
| 9 |
// Renderer defines the interface for rendering tables to an io.Writer. |
| 10 |
// Implementations must handle headers, rows, footers, and separator lines. |
| 11 |
type Renderer interface { |
| 12 |
Start(w io.Writer) error |
| 13 |
Header(headers [][]string, ctx Formatting) // Renders table header |
| 14 |
Row(row []string, ctx Formatting) // Renders a single row |
| 15 |
Footer(footers [][]string, ctx Formatting) // Renders table footer |
| 16 |
Line(ctx Formatting) // Renders separator line |
| 17 |
Config() Rendition // Returns renderer config |
| 18 |
Close() error // Gets Rendition form Blueprint |
| 19 |
Logger(logger *ll.Logger) // send logger to renderers |
| 20 |
} |
| 21 |
|
| 22 |
// Rendition holds the configuration for the default renderer. |
| 23 |
type Rendition struct { |
| 24 |
Borders Border // Border visibility settings |
| 25 |
Symbols Symbols // Symbols used for table drawing |
| 26 |
Settings Settings // Rendering behavior settings |
| 27 |
Streaming bool |
| 28 |
} |
| 29 |
|
| 30 |
// Renditioning has a method to update its rendition. |
| 31 |
// Let's define an optional interface for this. |
| 32 |
type Renditioning interface { |
| 33 |
Rendition(r Rendition) |
| 34 |
} |
| 35 |
|
| 36 |
// Formatting encapsulates the complete formatting context for a table row. |
| 37 |
// It provides all necessary information to render a row correctly within the table structure. |
| 38 |
type Formatting struct { |
| 39 |
Row RowContext // Detailed configuration for the row and its cells |
| 40 |
Level Level // Hierarchical level (Header, Body, Footer) affecting line drawing |
| 41 |
HasFooter bool // Indicates if the table includes a footer section |
| 42 |
IsSubRow bool // Marks this as a continuation or padding line in multi-line rows |
| 43 |
NormalizedWidths Mapper[int, int] |
| 44 |
} |
| 45 |
|
| 46 |
// CellContext defines the properties and formatting state of an individual table cell. |
| 47 |
type CellContext struct { |
| 48 |
Data string // Content to be displayed in the cell, provided by the caller |
| 49 |
Align Align // Text alignment within the cell (Left, Right, Center, Skip) |
| 50 |
Padding Padding // Padding characters surrounding the cell content |
| 51 |
Width int // Suggested width (often overridden by Row.Widths) |
| 52 |
Merge MergeState // Details about cell spanning across rows or columns |
| 53 |
} |
| 54 |
|
| 55 |
// MergeState captures how a cell merges across different directions. |
| 56 |
type MergeState struct { |
| 57 |
Vertical MergeStateOption // Properties for vertical merging (across rows) |
| 58 |
Horizontal MergeStateOption // Properties for horizontal merging (across columns) |
| 59 |
Hierarchical MergeStateOption // Properties for nested/hierarchical merging |
| 60 |
} |
| 61 |
|
| 62 |
// MergeStateOption represents common attributes for merging in a specific direction. |
| 63 |
type MergeStateOption struct { |
| 64 |
Present bool // True if this merge direction is active |
| 65 |
Span int // Number of cells this merge spans |
| 66 |
Start bool // True if this cell is the starting point of the merge |
| 67 |
End bool // True if this cell is the ending point of the merge |
| 68 |
} |
| 69 |
|
| 70 |
// RowContext manages layout properties and relationships for a row and its columns. |
| 71 |
// It maintains state about the current row and its neighbors for proper rendering. |
| 72 |
type RowContext struct { |
| 73 |
Position Position // Section of the table (Header, Row, Footer) |
| 74 |
Location Location // Boundary position (First, Middle, End) |
| 75 |
Current map[int]CellContext // Cells in this row, indexed by column |
| 76 |
Previous map[int]CellContext // Cells from the row above; nil if none |
| 77 |
Next map[int]CellContext // Cells from the row below; nil if none |
| 78 |
Widths Mapper[int, int] // Computed widths for each column |
| 79 |
ColMaxWidths CellWidth // Maximum allowed width per column |
| 80 |
} |
| 81 |
|
| 82 |
func (r RowContext) GetCell(col int) CellContext { |
| 83 |
return r.Current[col] |
| 84 |
} |
| 85 |
|
| 86 |
// Separators controls the visibility of separators in the table. |
| 87 |
type Separators struct { |
| 88 |
ShowHeader State // Controls header separator visibility |
| 89 |
ShowFooter State // Controls footer separator visibility |
| 90 |
BetweenRows State // Determines if lines appear between rows |
| 91 |
BetweenColumns State // Determines if separators appear between columns |
| 92 |
} |
| 93 |
|
| 94 |
// Lines manages the visibility of table boundary lines. |
| 95 |
type Lines struct { |
| 96 |
ShowTop State // Top border visibility |
| 97 |
ShowBottom State // Bottom border visibility |
| 98 |
ShowHeaderLine State // Header separator line visibility |
| 99 |
ShowFooterLine State // Footer separator line visibility |
| 100 |
} |
| 101 |
|
| 102 |
// Settings holds configuration preferences for rendering behavior. |
| 103 |
type Settings struct { |
| 104 |
Separators Separators // Separator visibility settings |
| 105 |
Lines Lines // Line visibility settings |
| 106 |
CompactMode State // Reserved for future compact rendering (unused) |
| 107 |
// Cushion State |
| 108 |
} |
| 109 |
|
| 110 |
// Border defines the visibility states of table borders. |
| 111 |
type Border struct { |
| 112 |
Left State // Left border visibility |
| 113 |
Right State // Right border visibility |
| 114 |
Top State // Top border visibility |
| 115 |
Bottom State // Bottom border visibility |
| 116 |
Overwrite bool |
| 117 |
} |
| 118 |
|
| 119 |
type StreamConfig struct { |
| 120 |
Enable bool |
| 121 |
|
| 122 |
// StrictColumns, if true, causes Append() to return an error |
| 123 |
// in streaming mode if the number of cells in an appended row |
| 124 |
// does not match the established number of columns for the stream. |
| 125 |
// If false (default), rows with mismatched column counts will be |
| 126 |
// padded or truncated with a warning log. |
| 127 |
StrictColumns bool |
| 128 |
|
| 129 |
// Deprecated: Use top-level Config.Widths for streaming width control. |
| 130 |
// This field will be removed in a future version. It will be respected if |
| 131 |
// Config.Widths is not set and this field is. |
| 132 |
Widths CellWidth |
| 133 |
} |