| 1 |
package tblfmt |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"reflect" |
| 8 |
"strconv" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/nathan-fiscaletti/consolesize-go" |
| 12 |
) |
| 13 |
|
| 14 |
// Builder is the shared builder interface. |
| 15 |
type Builder = func(ResultSet, ...Option) (Encoder, error) |
| 16 |
|
| 17 |
// Summary is the interface for a summary map. |
| 18 |
type Summary = map[int]func(io.Writer, int) (int, error) |
| 19 |
|
| 20 |
// Option is a Encoder option. |
| 21 |
type Option interface { |
| 22 |
apply(any) error |
| 23 |
} |
| 24 |
|
| 25 |
// option wraps setting an option on an encoder. |
| 26 |
type option struct { |
| 27 |
table func(*TableEncoder) error |
| 28 |
expanded func(*ExpandedEncoder) error |
| 29 |
json func(*JSONEncoder) error |
| 30 |
unaligned func(*UnalignedEncoder) error |
| 31 |
template func(*TemplateEncoder) error |
| 32 |
crosstab func(*CrosstabView) error |
| 33 |
err func(*errEncoder) error |
| 34 |
} |
| 35 |
|
| 36 |
// apply applies the option. |
| 37 |
func (opt option) apply(o any) error { |
| 38 |
switch v := o.(type) { |
| 39 |
case *TableEncoder: |
| 40 |
if opt.table != nil { |
| 41 |
return opt.table(v) |
| 42 |
} |
| 43 |
return nil |
| 44 |
case *ExpandedEncoder: |
| 45 |
if opt.expanded != nil { |
| 46 |
return opt.expanded(v) |
| 47 |
} |
| 48 |
return nil |
| 49 |
case *JSONEncoder: |
| 50 |
if opt.json != nil { |
| 51 |
return opt.json(v) |
| 52 |
} |
| 53 |
return nil |
| 54 |
case *UnalignedEncoder: |
| 55 |
if opt.unaligned != nil { |
| 56 |
return opt.unaligned(v) |
| 57 |
} |
| 58 |
return nil |
| 59 |
case *TemplateEncoder: |
| 60 |
if opt.template != nil { |
| 61 |
return opt.template(v) |
| 62 |
} |
| 63 |
return nil |
| 64 |
case *CrosstabView: |
| 65 |
if opt.crosstab != nil { |
| 66 |
return opt.crosstab(v) |
| 67 |
} |
| 68 |
return nil |
| 69 |
case *errEncoder: |
| 70 |
if opt.err != nil { |
| 71 |
return opt.err(v) |
| 72 |
} |
| 73 |
return nil |
| 74 |
} |
| 75 |
panic(fmt.Sprintf("option cannot be applied to %T", o)) |
| 76 |
} |
| 77 |
|
| 78 |
// FromMap creates an encoder for the provided result set, applying the named |
| 79 |
// options. |
| 80 |
// |
| 81 |
// Note: this func is primarily a helper func to accommodate psql-like format |
| 82 |
// option names. |
| 83 |
func FromMap(opts map[string]string) (Builder, []Option) { |
| 84 |
// unaligned, aligned, wrapped, html, asciidoc, latex, latex-longtable, troff-ms, json, csv |
| 85 |
switch format := opts["format"]; format { |
| 86 |
case "json": |
| 87 |
return NewJSONEncoder, []Option{ |
| 88 |
WithLowerColumnNames(opts["lower_column_names"] == "true"), |
| 89 |
WithUseColumnTypes(opts["use_column_types"] == "true"), |
| 90 |
FormatterOptionFromMap(opts), |
| 91 |
} |
| 92 |
case "csv", "unaligned": |
| 93 |
// determine separator, quote |
| 94 |
enc, sep, quote, field := NewUnalignedEncoder, '|', rune(0), "fieldsep" |
| 95 |
if format == "csv" { |
| 96 |
enc, sep, quote, field = NewCSVEncoder, ',', '"', "csv_fieldsep" |
| 97 |
} |
| 98 |
if s, ok := opts[field]; ok { |
| 99 |
r := []rune(s) |
| 100 |
if len(r) != 1 { |
| 101 |
err := ErrInvalidFieldSeparator |
| 102 |
if format == "csv" { |
| 103 |
err = ErrInvalidCSVFieldSeparator |
| 104 |
} |
| 105 |
return newErrEncoder, []Option{withError(err)} |
| 106 |
} |
| 107 |
sep = r[0] |
| 108 |
} |
| 109 |
if format != "csv" && opts["fieldsep_zero"] == "on" { |
| 110 |
sep = 0 |
| 111 |
} |
| 112 |
// determine newline |
| 113 |
recordsep := newline |
| 114 |
if rs, ok := opts["recordsep"]; ok { |
| 115 |
recordsep = []byte(rs) |
| 116 |
} |
| 117 |
if opts["recordsep_zero"] == "on" { |
| 118 |
recordsep = []byte{0} |
| 119 |
} |
| 120 |
tableOpts := []Option{ |
| 121 |
WithSeparator(sep), |
| 122 |
WithQuote(quote), |
| 123 |
WithFormatter(NewEscapeFormatter(WithIsRaw(true, sep, quote))), |
| 124 |
WithNewline(string(recordsep)), |
| 125 |
WithTitle(opts["title"]), |
| 126 |
WithEmpty(opts["null"]), |
| 127 |
WithSkipHeader(opts["tuples_only"] == "on"), |
| 128 |
WithLowerColumnNames(opts["lower_column_names"] == "true"), |
| 129 |
WithUseColumnTypes(opts["use_column_types"] == "true"), |
| 130 |
FormatterOptionFromMap(opts), |
| 131 |
} |
| 132 |
if opts["tuples_only"] == "on" { |
| 133 |
opts["footer"] = "off" |
| 134 |
} |
| 135 |
if s, ok := opts["footer"]; ok && s == "off" { |
| 136 |
// use an empty summary map to skip drawing the footer |
| 137 |
tableOpts = append(tableOpts, WithSummary(Summary{})) |
| 138 |
} |
| 139 |
return enc, tableOpts |
| 140 |
case "html", "asciidoc", "latex", "latex-longtable", "troff-ms", "vertical": |
| 141 |
return NewTemplateEncoder, []Option{ |
| 142 |
WithTemplate(format), |
| 143 |
WithTableAttributes(opts["tableattr"]), |
| 144 |
WithTitle(opts["title"]), |
| 145 |
WithEmpty(opts["null"]), |
| 146 |
WithLowerColumnNames(opts["lower_column_names"] == "true"), |
| 147 |
WithUseColumnTypes(opts["use_column_types"] == "true"), |
| 148 |
FormatterOptionFromMap(opts), |
| 149 |
} |
| 150 |
case "table": |
| 151 |
tableOpts := []Option{ |
| 152 |
WithForceUpperColumnNames(true), |
| 153 |
WithBorder(0), |
| 154 |
WithLineStyle(TableLineStyle()), |
| 155 |
WithInline(true), |
| 156 |
WithFormatterOptions( |
| 157 |
WithHeaderAlign(AlignLeft), |
| 158 |
WithAlign(AlignLeft), |
| 159 |
), |
| 160 |
FormatterOptionFromMap(opts), |
| 161 |
} |
| 162 |
if s, ok := opts["tuples_only"]; ok && s == "on" { |
| 163 |
tableOpts = append(tableOpts, WithSkipHeader(true)) |
| 164 |
opts["footer"] = "off" |
| 165 |
} |
| 166 |
if s, ok := opts["null"]; ok { |
| 167 |
tableOpts = append(tableOpts, WithEmpty(s)) |
| 168 |
} |
| 169 |
if s, ok := opts["footer"]; ok && s == "off" { |
| 170 |
// use an empty summary map to skip drawing the footer |
| 171 |
tableOpts = append(tableOpts, WithSummary(Summary{})) |
| 172 |
} |
| 173 |
tableOpts = pagerOpts(tableOpts, opts) |
| 174 |
return NewTableEncoder, tableOpts |
| 175 |
case "aligned": |
| 176 |
tableOpts := []Option{ |
| 177 |
WithLowerColumnNames(opts["lower_column_names"] == "true"), |
| 178 |
WithUseColumnTypes(opts["use_column_types"] == "true"), |
| 179 |
FormatterOptionFromMap(opts), |
| 180 |
} |
| 181 |
if s, ok := opts["border"]; ok { |
| 182 |
border, _ := strconv.Atoi(s) |
| 183 |
tableOpts = append(tableOpts, WithBorder(border)) |
| 184 |
} |
| 185 |
if s, ok := opts["tuples_only"]; ok && s == "on" { |
| 186 |
tableOpts = append(tableOpts, WithSkipHeader(true)) |
| 187 |
opts["footer"] = "off" |
| 188 |
} |
| 189 |
if s, ok := opts["title"]; ok { |
| 190 |
tableOpts = append(tableOpts, WithTitle(s)) |
| 191 |
} |
| 192 |
if s, ok := opts["null"]; ok { |
| 193 |
tableOpts = append(tableOpts, WithEmpty(s)) |
| 194 |
} |
| 195 |
if s, ok := opts["footer"]; ok && s == "off" { |
| 196 |
// use an empty summary map to skip drawing the footer |
| 197 |
tableOpts = append(tableOpts, WithSummary(Summary{})) |
| 198 |
} |
| 199 |
if s, ok := opts["linestyle"]; ok { |
| 200 |
switch s { |
| 201 |
case "ascii": |
| 202 |
tableOpts = append(tableOpts, WithLineStyle(ASCIILineStyle())) |
| 203 |
case "old-ascii": |
| 204 |
tableOpts = append(tableOpts, WithLineStyle(OldASCIILineStyle())) |
| 205 |
case "unicode": |
| 206 |
switch opts["unicode_border_linestyle"] { |
| 207 |
case "single": |
| 208 |
tableOpts = append(tableOpts, WithLineStyle(UnicodeLineStyle())) |
| 209 |
case "double": |
| 210 |
tableOpts = append(tableOpts, WithLineStyle(UnicodeDoubleLineStyle())) |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
tableOpts = pagerOpts(tableOpts, opts) |
| 215 |
builder := NewTableEncoder |
| 216 |
if e, ok := opts["expanded"]; ok { |
| 217 |
switch e { |
| 218 |
case "auto": |
| 219 |
cols, _ := consolesize.GetConsoleSize() |
| 220 |
if cstr, ok := opts["columns"]; ok && cstr != "" { |
| 221 |
if c, err := strconv.ParseUint(cstr, 10, 32); err == nil && c != 0 { |
| 222 |
cols = int(c) |
| 223 |
} |
| 224 |
} |
| 225 |
tableOpts = append(tableOpts, WithMinExpandWidth(cols+1)) |
| 226 |
case "on": |
| 227 |
builder = NewExpandedEncoder |
| 228 |
} |
| 229 |
} |
| 230 |
return builder, tableOpts |
| 231 |
} |
| 232 |
return newErrEncoder, []Option{withError(ErrInvalidFormat)} |
| 233 |
} |
| 234 |
|
| 235 |
// FormatterOptionFromMap builds formatter encoding options from the named |
| 236 |
// options. |
| 237 |
func FormatterOptionFromMap(opts map[string]string) Option { |
| 238 |
// time format |
| 239 |
timeFormat := opts["time"] |
| 240 |
if timeFormat == "" { |
| 241 |
timeFormat = time.RFC3339 |
| 242 |
} |
| 243 |
// time location |
| 244 |
var timeLocation *time.Location |
| 245 |
if tz := opts["timezone"]; tz != "" { |
| 246 |
if loc, err := time.LoadLocation(tz); err == nil { |
| 247 |
timeLocation = loc |
| 248 |
} |
| 249 |
} |
| 250 |
// numeric locale |
| 251 |
locale := opts["locale"] |
| 252 |
if locale == "" { |
| 253 |
locale = "en-US" |
| 254 |
} |
| 255 |
numericLocale := opts["numericlocale"] == "true" || opts["numericlocale"] == "on" |
| 256 |
return WithFormatterOptions( |
| 257 |
WithTimeFormat(timeFormat), |
| 258 |
WithTimeLocation(timeLocation), |
| 259 |
WithNumericLocale(numericLocale, locale), |
| 260 |
) |
| 261 |
} |
| 262 |
|
| 263 |
// WithCount is a encoder option to set the buffered line count. |
| 264 |
func WithCount(count int) Option { |
| 265 |
return option{ |
| 266 |
table: func(enc *TableEncoder) error { |
| 267 |
enc.count = count |
| 268 |
return nil |
| 269 |
}, |
| 270 |
expanded: func(enc *ExpandedEncoder) error { |
| 271 |
enc.count = count |
| 272 |
return nil |
| 273 |
}, |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// WithLineStyle is a encoder option to set the table line style. |
| 278 |
func WithLineStyle(lineStyle LineStyle) Option { |
| 279 |
return option{ |
| 280 |
table: func(enc *TableEncoder) error { |
| 281 |
enc.lineStyle = lineStyle |
| 282 |
return nil |
| 283 |
}, |
| 284 |
expanded: func(enc *ExpandedEncoder) error { |
| 285 |
enc.lineStyle = lineStyle |
| 286 |
return nil |
| 287 |
}, |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// WithFormatter is a encoder option to set a formatter for formatting values. |
| 292 |
func WithFormatter(formatter Formatter) Option { |
| 293 |
return option{ |
| 294 |
table: func(enc *TableEncoder) error { |
| 295 |
enc.formatter = formatter |
| 296 |
return nil |
| 297 |
}, |
| 298 |
expanded: func(enc *ExpandedEncoder) error { |
| 299 |
enc.formatter = formatter |
| 300 |
return nil |
| 301 |
}, |
| 302 |
json: func(enc *JSONEncoder) error { |
| 303 |
enc.formatter = formatter |
| 304 |
return nil |
| 305 |
}, |
| 306 |
unaligned: func(enc *UnalignedEncoder) error { |
| 307 |
enc.formatter = formatter |
| 308 |
return nil |
| 309 |
}, |
| 310 |
template: func(enc *TemplateEncoder) error { |
| 311 |
enc.formatter = formatter |
| 312 |
return nil |
| 313 |
}, |
| 314 |
crosstab: func(view *CrosstabView) error { |
| 315 |
view.formatter = formatter |
| 316 |
return nil |
| 317 |
}, |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// WithFormatterOptions is a encoder option to add additional formatter |
| 322 |
// options. |
| 323 |
func WithFormatterOptions(opts ...EscapeFormatterOption) Option { |
| 324 |
apply := func(formatter Formatter) { |
| 325 |
f := formatter.(*EscapeFormatter) |
| 326 |
for _, o := range opts { |
| 327 |
o(f) |
| 328 |
} |
| 329 |
} |
| 330 |
return option{ |
| 331 |
table: func(enc *TableEncoder) error { |
| 332 |
apply(enc.formatter) |
| 333 |
return nil |
| 334 |
}, |
| 335 |
expanded: func(enc *ExpandedEncoder) error { |
| 336 |
apply(enc.formatter) |
| 337 |
return nil |
| 338 |
}, |
| 339 |
json: func(enc *JSONEncoder) error { |
| 340 |
apply(enc.formatter) |
| 341 |
return nil |
| 342 |
}, |
| 343 |
unaligned: func(enc *UnalignedEncoder) error { |
| 344 |
apply(enc.formatter) |
| 345 |
return nil |
| 346 |
}, |
| 347 |
template: func(enc *TemplateEncoder) error { |
| 348 |
apply(enc.formatter) |
| 349 |
return nil |
| 350 |
}, |
| 351 |
crosstab: func(view *CrosstabView) error { |
| 352 |
apply(view.formatter) |
| 353 |
return nil |
| 354 |
}, |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// WithSummary is a encoder option to set a table summary. |
| 359 |
func WithSummary(summary Summary) Option { |
| 360 |
return option{ |
| 361 |
table: func(enc *TableEncoder) error { |
| 362 |
enc.summary = summary |
| 363 |
enc.isCustomSummary = true |
| 364 |
return nil |
| 365 |
}, |
| 366 |
expanded: func(enc *ExpandedEncoder) error { |
| 367 |
enc.summary = summary |
| 368 |
enc.isCustomSummary = true |
| 369 |
return nil |
| 370 |
}, |
| 371 |
// FIXME: all of these should have a summary option as well ... |
| 372 |
json: func(*JSONEncoder) error { |
| 373 |
return nil |
| 374 |
}, |
| 375 |
unaligned: func(enc *UnalignedEncoder) error { |
| 376 |
enc.summary = summary |
| 377 |
return nil |
| 378 |
}, |
| 379 |
template: func(*TemplateEncoder) error { |
| 380 |
return nil |
| 381 |
}, |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
// WithSkipHeader is a encoder option to disable writing a header. |
| 386 |
func WithSkipHeader(s bool) Option { |
| 387 |
return option{ |
| 388 |
table: func(enc *TableEncoder) error { |
| 389 |
enc.skipHeader = s |
| 390 |
return nil |
| 391 |
}, |
| 392 |
expanded: func(enc *ExpandedEncoder) error { |
| 393 |
enc.skipHeader = s |
| 394 |
return nil |
| 395 |
}, |
| 396 |
unaligned: func(enc *UnalignedEncoder) error { |
| 397 |
enc.skipHeader = s |
| 398 |
return nil |
| 399 |
}, |
| 400 |
template: func(enc *TemplateEncoder) error { |
| 401 |
enc.skipHeader = s |
| 402 |
return nil |
| 403 |
}, |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
// WithInline is a encoder option to set the column headers as inline to the |
| 408 |
// top line. |
| 409 |
func WithInline(inline bool) Option { |
| 410 |
return option{ |
| 411 |
table: func(enc *TableEncoder) error { |
| 412 |
enc.inline = inline |
| 413 |
return nil |
| 414 |
}, |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
// WithTitle is a encoder option to set the table title. |
| 419 |
func WithTitle(title string) Option { |
| 420 |
encode := func(formatter Formatter, empty *Value) *Value { |
| 421 |
if title == "" { |
| 422 |
return nil |
| 423 |
} |
| 424 |
if v, err := formatter.Header([]string{title}); err == nil { |
| 425 |
return v[0] |
| 426 |
} |
| 427 |
return empty |
| 428 |
} |
| 429 |
return option{ |
| 430 |
table: func(enc *TableEncoder) error { |
| 431 |
enc.title = encode(enc.formatter, enc.empty) |
| 432 |
return nil |
| 433 |
}, |
| 434 |
expanded: func(enc *ExpandedEncoder) error { |
| 435 |
enc.title = encode(enc.formatter, enc.empty) |
| 436 |
return nil |
| 437 |
}, |
| 438 |
template: func(enc *TemplateEncoder) error { |
| 439 |
enc.title = encode(enc.formatter, enc.empty) |
| 440 |
return nil |
| 441 |
}, |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
// WithEmpty is a encoder option to set the value used in empty (nil) |
| 446 |
// cells. |
| 447 |
func WithEmpty(empty string) Option { |
| 448 |
encode := func(formatter Formatter) *Value { |
| 449 |
z := new(any) |
| 450 |
*z = empty |
| 451 |
if v, err := formatter.Format([]any{z}); err == nil { |
| 452 |
return v[0] |
| 453 |
} |
| 454 |
panic(fmt.Sprintf("invalid empty value %q", empty)) |
| 455 |
} |
| 456 |
return option{ |
| 457 |
table: func(enc *TableEncoder) error { |
| 458 |
enc.empty = encode(enc.formatter) |
| 459 |
return nil |
| 460 |
}, |
| 461 |
expanded: func(enc *ExpandedEncoder) error { |
| 462 |
enc.empty = encode(enc.formatter) |
| 463 |
return nil |
| 464 |
}, |
| 465 |
json: func(enc *JSONEncoder) error { |
| 466 |
enc.empty = encode(enc.formatter) |
| 467 |
return nil |
| 468 |
}, |
| 469 |
unaligned: func(enc *UnalignedEncoder) error { |
| 470 |
enc.empty = encode(enc.formatter) |
| 471 |
return nil |
| 472 |
}, |
| 473 |
template: func(enc *TemplateEncoder) error { |
| 474 |
enc.empty = encode(enc.formatter) |
| 475 |
return nil |
| 476 |
}, |
| 477 |
crosstab: func(enc *CrosstabView) error { |
| 478 |
enc.empty = encode(enc.formatter) |
| 479 |
return nil |
| 480 |
}, |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
// WithWidths is a encoder option to set (minimum) widths for a column. |
| 485 |
func WithWidths(widths ...int) Option { |
| 486 |
return option{ |
| 487 |
table: func(enc *TableEncoder) error { |
| 488 |
enc.widths = widths |
| 489 |
return nil |
| 490 |
}, |
| 491 |
expanded: func(enc *ExpandedEncoder) error { |
| 492 |
enc.widths = widths |
| 493 |
return nil |
| 494 |
}, |
| 495 |
unaligned: func(*UnalignedEncoder) error { |
| 496 |
// FIXME: unaligned encoder should be able to support minimum |
| 497 |
// column widths |
| 498 |
// enc.widths = widths |
| 499 |
return nil |
| 500 |
}, |
| 501 |
template: func(*TemplateEncoder) error { |
| 502 |
// FIXME: template encoder should be able to support minimum column |
| 503 |
// widths |
| 504 |
// enc.widths = widths |
| 505 |
return nil |
| 506 |
}, |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
// WithSeparator is a encoder option to set the field separator. |
| 511 |
func WithSeparator(sep rune) Option { |
| 512 |
return option{ |
| 513 |
unaligned: func(enc *UnalignedEncoder) error { |
| 514 |
enc.sep = sep |
| 515 |
return nil |
| 516 |
}, |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
// WithQuote is a encoder option to set the field quote. |
| 521 |
func WithQuote(quote rune) Option { |
| 522 |
return option{ |
| 523 |
unaligned: func(enc *UnalignedEncoder) error { |
| 524 |
enc.quote = quote |
| 525 |
return nil |
| 526 |
}, |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
// WithNewline is a encoder option to set the newline. |
| 531 |
func WithNewline(newline string) Option { |
| 532 |
return option{ |
| 533 |
table: func(enc *TableEncoder) error { |
| 534 |
enc.newline = []byte(newline) |
| 535 |
return nil |
| 536 |
}, |
| 537 |
expanded: func(enc *ExpandedEncoder) error { |
| 538 |
enc.newline = []byte(newline) |
| 539 |
return nil |
| 540 |
}, |
| 541 |
json: func(enc *JSONEncoder) error { |
| 542 |
enc.newline = []byte(newline) |
| 543 |
return nil |
| 544 |
}, |
| 545 |
unaligned: func(enc *UnalignedEncoder) error { |
| 546 |
enc.newline = []byte(newline) |
| 547 |
return nil |
| 548 |
}, |
| 549 |
template: func(enc *TemplateEncoder) error { |
| 550 |
enc.newline = []byte(newline) |
| 551 |
return nil |
| 552 |
}, |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
// WithBorder is a encoder option to set the border size. |
| 557 |
func WithBorder(border int) Option { |
| 558 |
return option{ |
| 559 |
table: func(enc *TableEncoder) error { |
| 560 |
enc.border = border |
| 561 |
return nil |
| 562 |
}, |
| 563 |
expanded: func(enc *ExpandedEncoder) error { |
| 564 |
enc.border = border |
| 565 |
return nil |
| 566 |
}, |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
// WithTableAttributes is a encoder option to set the table attributes. |
| 571 |
func WithTableAttributes(a string) Option { |
| 572 |
return option{ |
| 573 |
template: func(enc *TemplateEncoder) error { |
| 574 |
enc.attributes = a |
| 575 |
return nil |
| 576 |
}, |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
// WithExecutor is a encoder option to set the executor. |
| 581 |
func WithExecutor(executor func(io.Writer, *Template) error) Option { |
| 582 |
return option{ |
| 583 |
template: func(enc *TemplateEncoder) error { |
| 584 |
enc.executor = executor |
| 585 |
return nil |
| 586 |
}, |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
// WithTemplate is a encoder option to set a named template. |
| 591 |
func WithTemplate(name string) Option { |
| 592 |
return option{ |
| 593 |
template: func(enc *TemplateEncoder) error { |
| 594 |
switch name { |
| 595 |
case "html": |
| 596 |
enc.executor = WriteHTMLTo |
| 597 |
case "asciidoc": |
| 598 |
enc.executor = WriteAsciidocTo |
| 599 |
case "vertical": |
| 600 |
enc.executor = WriteVerticalTo |
| 601 |
default: |
| 602 |
return ErrInvalidTemplate |
| 603 |
} |
| 604 |
return nil |
| 605 |
}, |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
// WithHeaderTransformer is a encoder option to set the column header transform |
| 610 |
// style. |
| 611 |
func WithHeaderTransformer(headerTransformer Transformer) Option { |
| 612 |
return option{ |
| 613 |
table: func(enc *TableEncoder) error { |
| 614 |
enc.headerTransformer = headerTransformer |
| 615 |
return nil |
| 616 |
}, |
| 617 |
expanded: func(enc *ExpandedEncoder) error { |
| 618 |
enc.headerTransformer = headerTransformer |
| 619 |
return nil |
| 620 |
}, |
| 621 |
json: func(enc *JSONEncoder) error { |
| 622 |
enc.headerTransformer = headerTransformer |
| 623 |
return nil |
| 624 |
}, |
| 625 |
unaligned: func(enc *UnalignedEncoder) error { |
| 626 |
enc.headerTransformer = headerTransformer |
| 627 |
return nil |
| 628 |
}, |
| 629 |
template: func(enc *TemplateEncoder) error { |
| 630 |
enc.headerTransformer = headerTransformer |
| 631 |
return nil |
| 632 |
}, |
| 633 |
crosstab: func(view *CrosstabView) error { |
| 634 |
view.headerTransformer = headerTransformer |
| 635 |
return nil |
| 636 |
}, |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
// WithLowerColumnNames is a encoder option to lower case column names when |
| 641 |
// column names are all caps. See [TransformUpperToLower]. |
| 642 |
func WithLowerColumnNames(lowerColumnNames bool) Option { |
| 643 |
transformStyle := TransformNone |
| 644 |
if lowerColumnNames { |
| 645 |
transformStyle = TransformUpperToLower |
| 646 |
} |
| 647 |
return WithHeaderTransformer(transformStyle) |
| 648 |
} |
| 649 |
|
| 650 |
// WithForceUpperColumnNames is a encoder option to force upper case column |
| 651 |
// names. See [TransformForceUpper]. |
| 652 |
func WithForceUpperColumnNames(forceUpper bool) Option { |
| 653 |
transformStyle := TransformNone |
| 654 |
if forceUpper { |
| 655 |
transformStyle = TransformForceUpper |
| 656 |
} |
| 657 |
return WithHeaderTransformer(transformStyle) |
| 658 |
} |
| 659 |
|
| 660 |
// WithColumnTypes is a encoder option to set a func to use for building column |
| 661 |
// types. |
| 662 |
func WithColumnTypes(columnTypes func(ResultSet, []any, int) error) Option { |
| 663 |
return option{ |
| 664 |
table: func(enc *TableEncoder) error { |
| 665 |
enc.columnTypes = columnTypes |
| 666 |
return nil |
| 667 |
}, |
| 668 |
expanded: func(enc *ExpandedEncoder) error { |
| 669 |
enc.columnTypes = columnTypes |
| 670 |
return nil |
| 671 |
}, |
| 672 |
json: func(enc *JSONEncoder) error { |
| 673 |
enc.columnTypes = columnTypes |
| 674 |
return nil |
| 675 |
}, |
| 676 |
unaligned: func(enc *UnalignedEncoder) error { |
| 677 |
enc.columnTypes = columnTypes |
| 678 |
return nil |
| 679 |
}, |
| 680 |
template: func(enc *TemplateEncoder) error { |
| 681 |
enc.columnTypes = columnTypes |
| 682 |
return nil |
| 683 |
}, |
| 684 |
crosstab: func(view *CrosstabView) error { |
| 685 |
view.columnTypes = columnTypes |
| 686 |
return nil |
| 687 |
}, |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
// WithUseColumnTypes is a encoder option to use the result set's column types. |
| 692 |
func WithUseColumnTypes(useColumnTypes bool) Option { |
| 693 |
if !useColumnTypes { |
| 694 |
return WithColumnTypes(nil) |
| 695 |
} |
| 696 |
return WithColumnTypes(func(resultSet ResultSet, r []any, n int) error { |
| 697 |
cols, err := resultSetColumns(resultSet, n) |
| 698 |
if err != nil { |
| 699 |
return err |
| 700 |
} |
| 701 |
for i := range n { |
| 702 |
r[i] = reflect.New(cols[i].ScanType()).Interface() |
| 703 |
} |
| 704 |
return nil |
| 705 |
}) |
| 706 |
} |
| 707 |
|
| 708 |
// WithColumnTypesFunc is a encoder option to set a func to build each column's |
| 709 |
// type. |
| 710 |
func WithColumnTypesFunc(f func(*sql.ColumnType) (any, error)) Option { |
| 711 |
return WithColumnTypes(func(resultSet ResultSet, r []any, n int) error { |
| 712 |
cols, err := resultSetColumns(resultSet, n) |
| 713 |
if err != nil { |
| 714 |
return err |
| 715 |
} |
| 716 |
for i := range n { |
| 717 |
if r[i], err = f(cols[i]); err != nil { |
| 718 |
return err |
| 719 |
} |
| 720 |
} |
| 721 |
return nil |
| 722 |
}) |
| 723 |
} |
| 724 |
|
| 725 |
// WithParams is a view option to set the column parameters. |
| 726 |
func WithParams(params ...string) Option { |
| 727 |
return option{ |
| 728 |
crosstab: func(view *CrosstabView) error { |
| 729 |
if len(params) > 4 { |
| 730 |
return ErrInvalidColumnParams |
| 731 |
} |
| 732 |
if len(params) > 0 { |
| 733 |
view.v = params[0] |
| 734 |
} |
| 735 |
if len(params) > 1 { |
| 736 |
view.h = params[1] |
| 737 |
} |
| 738 |
if len(params) > 2 { |
| 739 |
view.d = params[2] |
| 740 |
} |
| 741 |
if len(params) > 3 { |
| 742 |
view.s = params[3] |
| 743 |
} |
| 744 |
return nil |
| 745 |
}, |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
// WithMinExpandWidth is a encoder option to set maximum width before switching |
| 750 |
// to expanded format. |
| 751 |
func WithMinExpandWidth(w int) Option { |
| 752 |
return option{ |
| 753 |
table: func(enc *TableEncoder) error { |
| 754 |
enc.minExpandWidth = w |
| 755 |
return nil |
| 756 |
}, |
| 757 |
expanded: func(enc *ExpandedEncoder) error { |
| 758 |
enc.minExpandWidth = w |
| 759 |
return nil |
| 760 |
}, |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
// WithMinPagerWidth is a encoder option to set maximum width before |
| 765 |
// redirecting output to pager. |
| 766 |
func WithMinPagerWidth(w int) Option { |
| 767 |
return option{ |
| 768 |
table: func(enc *TableEncoder) error { |
| 769 |
enc.minPagerWidth = w |
| 770 |
return nil |
| 771 |
}, |
| 772 |
expanded: func(enc *ExpandedEncoder) error { |
| 773 |
enc.minPagerWidth = w |
| 774 |
return nil |
| 775 |
}, |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
// WithMinPagerHeight is a encoder option to set maximum height before |
| 780 |
// redirecting output to pager. |
| 781 |
func WithMinPagerHeight(h int) Option { |
| 782 |
return option{ |
| 783 |
table: func(enc *TableEncoder) error { |
| 784 |
enc.minPagerHeight = h |
| 785 |
return nil |
| 786 |
}, |
| 787 |
expanded: func(enc *ExpandedEncoder) error { |
| 788 |
enc.minPagerHeight = h |
| 789 |
return nil |
| 790 |
}, |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
// WithPager is a encoder option to set the pager command. |
| 795 |
func WithPager(p string) Option { |
| 796 |
return option{ |
| 797 |
table: func(enc *TableEncoder) error { |
| 798 |
enc.pagerCmd = p |
| 799 |
return nil |
| 800 |
}, |
| 801 |
expanded: func(enc *ExpandedEncoder) error { |
| 802 |
enc.pagerCmd = p |
| 803 |
return nil |
| 804 |
}, |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
// withError is a encoder option to force an error. |
| 809 |
func withError(err error) Option { |
| 810 |
return option{ |
| 811 |
err: func(enc *errEncoder) error { |
| 812 |
enc.err = err |
| 813 |
return err |
| 814 |
}, |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
// resultSetColumns retrieves the columns from a result set and checks the |
| 819 |
// length. |
| 820 |
func resultSetColumns(resultSet ResultSet, n int) ([]*sql.ColumnType, error) { |
| 821 |
rs, ok := resultSet.(interface { |
| 822 |
ColumnTypes() ([]*sql.ColumnType, error) |
| 823 |
}) |
| 824 |
if !ok { |
| 825 |
return nil, ErrResultSetHasNoColumnTypes |
| 826 |
} |
| 827 |
cols, err := rs.ColumnTypes() |
| 828 |
switch { |
| 829 |
case err != nil: |
| 830 |
return nil, err |
| 831 |
case len(cols) != n: |
| 832 |
return nil, ErrResultSetReturnedInvalidColumnTypes |
| 833 |
} |
| 834 |
return cols, nil |
| 835 |
} |
| 836 |
|
| 837 |
// pagerOpts adds pager options to the table options. |
| 838 |
func pagerOpts(tableOpts []Option, opts map[string]string) []Option { |
| 839 |
pager, pagerCmd := opts["pager"], opts["pager_cmd"] |
| 840 |
if pager == "" || pagerCmd == "" { |
| 841 |
return tableOpts |
| 842 |
} |
| 843 |
tableOpts = append(tableOpts, WithPager(pagerCmd)) |
| 844 |
switch pager { |
| 845 |
case "on": |
| 846 |
cols, rows := consolesize.GetConsoleSize() |
| 847 |
if cstr, ok := opts["columns"]; ok && cstr != "" { |
| 848 |
if c, err := strconv.ParseUint(cstr, 10, 32); err == nil && c != 0 { |
| 849 |
cols = int(c) |
| 850 |
} |
| 851 |
} |
| 852 |
if rstr, ok := opts["pager_min_lines"]; ok && rstr != "" { |
| 853 |
if r, err := strconv.ParseUint(rstr, 10, 32); err == nil && r != 0 { |
| 854 |
rows = int(r) |
| 855 |
} |
| 856 |
} |
| 857 |
tableOpts = append(tableOpts, WithMinPagerWidth(cols+1), WithMinPagerHeight(rows+1)) |
| 858 |
case "always": |
| 859 |
tableOpts = append(tableOpts, WithMinPagerWidth(-1), WithMinPagerHeight(-1)) |
| 860 |
} |
| 861 |
return tableOpts |
| 862 |
} |