Skip to content

Commit 606168a

Browse files
committed
let Append respect t.config.Behavior.Struct.AutoHeader .. update #283
1 parent bb111e7 commit 606168a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

tablewriter.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,13 @@ func (t *Table) Append(rows ...interface{}) error {
184184
t.ensureInitialized()
185185

186186
if t.config.Stream.Enable && t.hasPrinted {
187+
// Streaming logic remains unchanged, as AutoHeader is a batch-mode concept.
187188
t.logger.Debugf("Append() called in streaming mode with %d items for a single row", len(rows))
188189
var rowItemForStream interface{}
189190
if len(rows) == 1 {
190191
rowItemForStream = rows[0]
191192
} else {
192-
rowItemForStream = rows // Pass the slice of items if multiple args
193+
rowItemForStream = rows
193194
}
194195
if err := t.streamAppendRow(rowItemForStream); err != nil {
195196
t.logger.Errorf("Error rendering streaming row: %v", err)
@@ -204,13 +205,22 @@ func (t *Table) Append(rows ...interface{}) error {
204205
var cellsSource interface{}
205206
if len(rows) == 1 {
206207
cellsSource = rows[0]
207-
t.logger.Debug("Append (Batch): Single argument provided. Treating it as the source for row cells.")
208208
} else {
209-
cellsSource = rows // 'rows' is []interface{} containing all arguments
210-
t.logger.Debug("Append (Batch): Multiple arguments provided. Treating them directly as cells for one row.")
209+
cellsSource = rows
210+
}
211+
// Check if we should attempt to auto-generate headers from this append operation.
212+
// Conditions: AutoHeader is on, no headers are set yet, and this is the first data row.
213+
isFirstRow := len(t.rows) == 0
214+
if t.config.Behavior.Struct.AutoHeader.Enabled() && len(t.headers) == 0 && isFirstRow {
215+
t.logger.Debug("Append: Triggering AutoHeader for the first row.")
216+
headers := t.extractHeadersFromStruct(cellsSource)
217+
if len(headers) > 0 {
218+
// Set the extracted headers. The Header() method handles the rest.
219+
t.Header(headers)
220+
}
211221
}
212222

213-
// Delegate to the robust toStringLines, which now correctly handles structs via the new parser.
223+
// The rest of the function proceeds as before, converting the data to string lines.
214224
lines, err := t.toStringLines(cellsSource, t.config.Row)
215225
if err != nil {
216226
t.logger.Errorf("Append (Batch) failed for cellsSource %v: %v", cellsSource, err)

0 commit comments

Comments
 (0)