-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatstrings.go
More file actions
106 lines (100 loc) · 3.6 KB
/
formatstrings.go
File metadata and controls
106 lines (100 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package retable
import (
"context"
)
// FormatTableAsStrings converts any table data into a 2D string slice.
//
// This function automatically selects an appropriate Viewer for the table type,
// creates a View, and then formats it as strings using the provided formatter and options.
//
// Parameters:
// - ctx: Context for cancellation and timeout control during formatting
// - table: The table data to format (e.g., [][]string, []Person, etc.)
// - formatter: Optional CellFormatter to customize cell rendering (can be nil for default formatting)
// - options: Optional formatting options (e.g., OptionAddHeaderRow)
//
// Returns a 2D string slice where each inner slice represents a row,
// and an error if viewer selection, view creation, or formatting fails.
//
// Example:
//
// type Person struct {
// Name string
// Age int
// }
// people := []Person{{Name: "John", Age: 30}, {Name: "Alice", Age: 25}}
// rows, err := FormatTableAsStrings(ctx, people, nil, OptionAddHeaderRow)
// if err != nil {
// log.Fatal(err)
// }
// // rows[0] = ["Name", "Age"] // header row
// // rows[1] = ["John", "30"] // data row
// // rows[2] = ["Alice", "25"] // data row
func FormatTableAsStrings(ctx context.Context, table any, formatter CellFormatter, options ...Option) (rows [][]string, err error) {
viewer, err := SelectViewer(table)
if err != nil {
return nil, err
}
view, err := viewer.NewView("", table)
if err != nil {
return nil, err
}
return FormatViewAsStrings(ctx, view, formatter, options...)
}
// FormatViewAsStrings converts a View into a 2D string slice.
//
// This function iterates through all rows and columns in the view,
// formatting each cell using the provided CellFormatter. If no formatter is provided,
// TryFormattersOrSprint is used as the default.
//
// Parameters:
// - ctx: Context for cancellation and timeout control during formatting
// - view: The View to format, providing access to rows, columns, and cell data
// - formatter: Optional CellFormatter to customize cell rendering (can be nil for default formatting)
// - options: Optional formatting options (e.g., OptionAddHeaderRow to include column titles)
//
// When OptionAddHeaderRow is set, the column titles from view.Columns() are added
// as the first row, also passed through the formatter for consistent formatting.
//
// Returns a 2D string slice where each inner slice represents a row,
// and an error if any cell formatting fails.
//
// Example:
//
// rows, err := FormatViewAsStrings(ctx, myView, nil, OptionAddHeaderRow)
// if err != nil {
// log.Fatal(err)
// }
// // Process the formatted rows
// for _, row := range rows {
// fmt.Println(strings.Join(row, " | "))
// }
func FormatViewAsStrings(ctx context.Context, view View, formatter CellFormatter, options ...Option) (rows [][]string, err error) {
formatter = TryFormattersOrSprint(formatter)
numRows := view.NumRows()
numCols := len(view.Columns())
if HasOption(options, OptionAddHeaderRow) {
// view.Columns() would already returns a string slice,
// but use formatter for any additional formatting of strings
headerView := NewHeaderViewFrom(view)
rowStrings := make([]string, numCols)
for col := range numCols {
rowStrings[col], _, err = formatter.FormatCell(ctx, headerView, 0, col)
if err != nil {
return nil, err
}
}
rows = append(rows, rowStrings)
}
for row := range numRows {
rowStrings := make([]string, numCols)
for col := range numCols {
rowStrings[col], _, err = formatter.FormatCell(ctx, view, row, col)
if err != nil {
return nil, err
}
}
rows = append(rows, rowStrings)
}
return rows, nil
}