MCPcopy Index your code
hub / github.com/cortexlabs/cortex / Format

Method Format

pkg/lib/table/table.go:125–213  ·  view source on GitHub ↗
(opts ...*Opts)

Source from the content-addressed store, hash-verified

123}
124
125func (t *Table) Format(opts ...*Opts) (string, error) {
126 mergedOpts := mergeTableOptions(opts...)
127 if err := validate(*t); err != nil {
128 return "", err
129 }
130
131 if t.Spacing <= 0 {
132 t.Spacing = 3
133 }
134
135 colWidths := make([]int, len(t.Headers))
136 for colNum, header := range t.Headers {
137 colWidths[colNum] = len(header.Title)
138 }
139
140 rows := make([][]string, len(t.Rows))
141 for rowNum, row := range t.Rows {
142 rows[rowNum] = make([]string, len(row))
143 for colNum, val := range row {
144 strVal := s.ObjFlatNoQuotes(val)
145 rows[rowNum][colNum] = strVal
146 if len(strVal) > colWidths[colNum] {
147 colWidths[colNum] = len(strVal)
148 }
149 }
150 }
151
152 maxColWidths := make([]int, len(t.Headers))
153 for colNum, colWidth := range colWidths {
154 if t.Headers[colNum].MaxWidth <= 0 {
155 maxColWidths[colNum] = colWidth
156 } else {
157 maxColWidths[colNum] = libmath.MinInt(colWidth, t.Headers[colNum].MaxWidth)
158 }
159
160 if maxColWidths[colNum] < t.Headers[colNum].MinWidth {
161 maxColWidths[colNum] = t.Headers[colNum].MinWidth
162 }
163 }
164
165 lastColIndex := len(t.Headers) - 1
166
167 var headerStr string
168 for colNum, header := range t.Headers {
169 if header.Hidden {
170 continue
171 }
172
173 if *mergedOpts.BoldHeader {
174 headerStr += console.Bold(header.Title)
175 } else {
176 headerStr += header.Title
177 }
178 if colNum != lastColIndex {
179 headerStr += strings.Repeat(" ", maxColWidths[colNum]+t.Spacing-len(header.Title))
180 }
181 }
182 headerStr = s.TrimTrailingWhitespace(headerStr)

Callers 11

MustFormatMethod · 0.95
LocalTimestampFunction · 0.45
LocalTimestampHumanFunction · 0.45
LocalHourNowFunction · 0.45
timeStringFunction · 0.45
taskAPITableFunction · 0.45
getTaskJobFunction · 0.45
cmdDebugFunction · 0.45
batchAPITableFunction · 0.45
getBatchJobFunction · 0.45

Calls 3

BoldFunction · 0.92
mergeTableOptionsFunction · 0.85
validateFunction · 0.85

Tested by

no test coverage detected