package execute import ( "io" "sort" "strconv" "strings" "github.com/influxdata/platform/query" ) const fixedWidthTimeFmt = "2006-01-02T15:04:05.000000000Z" // Formatter writes a table to a Writer. type Formatter struct { tbl query.Table widths []int maxWidth int newWidths []int pad []byte dash []byte // fmtBuf is used to format values fmtBuf [64]byte opts FormatOptions cols orderedCols } type FormatOptions struct { // RepeatHeaderCount is the number of rows to print before printing the header again. // If zero then the headers are not repeated. RepeatHeaderCount int } func DefaultFormatOptions() *FormatOptions { return &FormatOptions{} } var eol = []byte{'\n'} // NewFormatter creates a Formatter for a given table. // If opts is nil, the DefaultFormatOptions are used. func NewFormatter(tbl query.Table, opts *FormatOptions) *Formatter { if opts == nil { opts = DefaultFormatOptions() } return &Formatter{ tbl: tbl, opts: *opts, } } type writeToHelper struct { w io.Writer n int64 err error } func (w *writeToHelper) write(data []byte) { if w.err != nil { return } n, err := w.w.Write(data) w.n += int64(n) w.err = err } var minWidthsByType = map[query.DataType]int{ query.TBool: 12, query.TInt: 26, query.TUInt: 27, query.TFloat: 28, query.TString: 22, query.TTime: len(fixedWidthTimeFmt), query.TInvalid: 10, } // WriteTo writes the formatted table data to w. func (f *Formatter) WriteTo(out io.Writer) (int64, error) { w := &writeToHelper{w: out} // Sort cols cols := f.tbl.Cols() f.cols = newOrderedCols(cols, f.tbl.Key()) sort.Sort(f.cols) // Compute header widths f.widths = make([]int, len(cols)) for j, c := range cols { // Column header is "