@@ -184,12 +184,13 @@ func (t *Table) Append(rows ...interface{}) error {
184
184
t .ensureInitialized ()
185
185
186
186
if t .config .Stream .Enable && t .hasPrinted {
187
+ // Streaming logic remains unchanged, as AutoHeader is a batch-mode concept.
187
188
t .logger .Debugf ("Append() called in streaming mode with %d items for a single row" , len (rows ))
188
189
var rowItemForStream interface {}
189
190
if len (rows ) == 1 {
190
191
rowItemForStream = rows [0 ]
191
192
} else {
192
- rowItemForStream = rows // Pass the slice of items if multiple args
193
+ rowItemForStream = rows
193
194
}
194
195
if err := t .streamAppendRow (rowItemForStream ); err != nil {
195
196
t .logger .Errorf ("Error rendering streaming row: %v" , err )
@@ -204,13 +205,22 @@ func (t *Table) Append(rows ...interface{}) error {
204
205
var cellsSource interface {}
205
206
if len (rows ) == 1 {
206
207
cellsSource = rows [0 ]
207
- t .logger .Debug ("Append (Batch): Single argument provided. Treating it as the source for row cells." )
208
208
} 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
+ }
211
221
}
212
222
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 .
214
224
lines , err := t .toStringLines (cellsSource , t .config .Row )
215
225
if err != nil {
216
226
t .logger .Errorf ("Append (Batch) failed for cellsSource %v: %v" , cellsSource , err )
0 commit comments