|
| 1 | +// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use |
| 2 | +// of this source code is governed by a BSD-style license that can be found in |
| 3 | +// the LICENSE file. |
| 4 | + |
| 5 | +package table_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "slices" |
| 10 | + |
| 11 | + "github.com/cockroachdb/pebble/internal/ascii" |
| 12 | + "github.com/cockroachdb/pebble/internal/ascii/table" |
| 13 | +) |
| 14 | + |
| 15 | +func ExampleDefine() { |
| 16 | + type Cat struct { |
| 17 | + Name string |
| 18 | + Age int |
| 19 | + Cuteness int |
| 20 | + } |
| 21 | + |
| 22 | + tbl := table.Define[Cat]( |
| 23 | + table.String("name", 7, table.AlignLeft, func(c Cat) string { return c.Name }), |
| 24 | + table.Div(), |
| 25 | + table.Int("age", 4, table.AlignRight, func(c Cat) int { return c.Age }), |
| 26 | + table.Div(), |
| 27 | + table.Int("cuteness", 8, table.AlignRight, func(c Cat) int { return c.Cuteness }), |
| 28 | + ) |
| 29 | + |
| 30 | + board := ascii.Make(8, tbl.CumulativeFieldWidth) |
| 31 | + fmt.Println("Cool cats:") |
| 32 | + tbl.Render(board.At(0, 0), table.RenderOptions{}, slices.Values([]Cat{ |
| 33 | + {Name: "Chicken", Age: 5, Cuteness: 10}, |
| 34 | + {Name: "Heart", Age: 4, Cuteness: 10}, |
| 35 | + {Name: "Mai", Age: 2, Cuteness: 10}, |
| 36 | + {Name: "Poi", Age: 15, Cuteness: 10}, |
| 37 | + {Name: "Pigeon", Age: 2, Cuteness: 10}, |
| 38 | + {Name: "Sugar", Age: 8, Cuteness: 10}, |
| 39 | + {Name: "Yaya", Age: 5, Cuteness: 10}, |
| 40 | + {Name: "Yuumi", Age: 5, Cuteness: 10}, |
| 41 | + })) |
| 42 | + fmt.Println(board.String()) |
| 43 | + // Output: |
| 44 | + // Cool cats: |
| 45 | + // name | age | cuteness |
| 46 | + // --------+------+--------- |
| 47 | + // Chicken | 5 | 10 |
| 48 | + // Heart | 4 | 10 |
| 49 | + // Mai | 2 | 10 |
| 50 | + // Poi | 15 | 10 |
| 51 | + // Pigeon | 2 | 10 |
| 52 | + // Sugar | 8 | 10 |
| 53 | + // Yaya | 5 | 10 |
| 54 | + // Yuumi | 5 | 10 |
| 55 | +} |
| 56 | + |
| 57 | +func ExampleHorizontally() { |
| 58 | + type Cat struct { |
| 59 | + Name string |
| 60 | + Age int |
| 61 | + Cuteness int |
| 62 | + } |
| 63 | + |
| 64 | + tbl := table.Define[Cat]( |
| 65 | + table.String("name", 7, table.AlignRight, func(c Cat) string { return c.Name }), |
| 66 | + table.Div(), |
| 67 | + table.Int("age", 4, table.AlignRight, func(c Cat) int { return c.Age }), |
| 68 | + table.Int("cuteness", 8, table.AlignRight, func(c Cat) int { return c.Cuteness }), |
| 69 | + ) |
| 70 | + |
| 71 | + board := ascii.Make(8, tbl.CumulativeFieldWidth) |
| 72 | + fmt.Println("Cool cats:") |
| 73 | + opts := table.RenderOptions{Orientation: table.Horizontally} |
| 74 | + tbl.Render(board.At(0, 0), opts, slices.Values([]Cat{ |
| 75 | + {Name: "Chicken", Age: 5, Cuteness: 10}, |
| 76 | + {Name: "Heart", Age: 4, Cuteness: 10}, |
| 77 | + {Name: "Mai", Age: 2, Cuteness: 10}, |
| 78 | + {Name: "Poi", Age: 15, Cuteness: 10}, |
| 79 | + {Name: "Pigeon", Age: 2, Cuteness: 10}, |
| 80 | + {Name: "Sugar", Age: 8, Cuteness: 10}, |
| 81 | + {Name: "Yaya", Age: 5, Cuteness: 10}, |
| 82 | + {Name: "Yuumi", Age: 5, Cuteness: 10}, |
| 83 | + {Name: "Yuumibestcatever", Age: 5, Cuteness: 100000000}, |
| 84 | + })) |
| 85 | + fmt.Println(board.String()) |
| 86 | + // Output: |
| 87 | + // Cool cats: |
| 88 | + // name | Chicken Heart Mai Poi Pigeon Sugar Yaya Yuumi Yuumibestcatever |
| 89 | + // ---------+------------------------------------------------------------------------- |
| 90 | + // age | 5 4 2 15 2 8 5 5 5 |
| 91 | + // cuteness | 10 10 10 10 10 10 10 10100000000 |
| 92 | +} |
0 commit comments