Print the tables with options
(writer io.Writer, opt ...PrintOption)
| 45 | |
| 46 | // Print the tables with options |
| 47 | func (t *DataTable) Print(writer io.Writer, opt ...PrintOption) { |
| 48 | options := PrintOptions{ |
| 49 | ColumnName: true, |
| 50 | ColumnType: true, |
| 51 | RowNumber: true, |
| 52 | MaxRows: 100, |
| 53 | } |
| 54 | |
| 55 | for _, o := range opt { |
| 56 | o(&options) |
| 57 | } |
| 58 | |
| 59 | if writer == nil { |
| 60 | writer = os.Stdout |
| 61 | } |
| 62 | |
| 63 | tw := tablewriter.NewWriter(writer) |
| 64 | tw.SetAutoWrapText(false) |
| 65 | tw.SetHeaderAlignment(tablewriter.ALIGN_LEFT) |
| 66 | tw.SetAlignment(tablewriter.ALIGN_LEFT) |
| 67 | tw.SetCenterSeparator("") |
| 68 | tw.SetColumnSeparator("") |
| 69 | tw.SetRowSeparator("") |
| 70 | tw.SetHeaderLine(false) |
| 71 | tw.SetBorder(false) |
| 72 | tw.SetTablePadding("\t") |
| 73 | tw.SetNoWhiteSpace(true) |
| 74 | |
| 75 | if options.ColumnName || options.ColumnType { |
| 76 | headers := make([]string, 0, len(t.cols)) |
| 77 | |
| 78 | for _, col := range t.cols { |
| 79 | if !col.IsVisible() { |
| 80 | continue |
| 81 | } |
| 82 | var h []string |
| 83 | if options.ColumnName { |
| 84 | h = append(h, col.Name()) |
| 85 | } |
| 86 | if options.ColumnType { |
| 87 | h = append(h, fmt.Sprintf("<%s>", col.serie.Type().Name())) |
| 88 | } |
| 89 | headers = append(headers, strings.Join(h, " ")) |
| 90 | } |
| 91 | tw.SetHeader(headers) |
| 92 | } |
| 93 | |
| 94 | if options.MaxRows > 1 && options.MaxRows <= t.NumRows() { |
| 95 | mr := options.MaxRows / 2 |
| 96 | tw.AppendBulk(t.Head(mr).Records()) |
| 97 | seps := make([]string, 0, len(t.cols)) |
| 98 | for _, col := range t.cols { |
| 99 | if !col.IsVisible() { |
| 100 | continue |
| 101 | } |
| 102 | seps = append(seps, "...") |
| 103 | } |
| 104 | tw.Append(seps) |