Skip to content

Commit 652bc0c

Browse files
jbowensannrpom
authored andcommitted
ascii/table: new package
Define a new package with facilities for rendering an ASCII table to a ascii.Board.
1 parent 2430d94 commit 652bc0c

File tree

6 files changed

+567
-3
lines changed

6 files changed

+567
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require (
55
github.com/HdrHistogram/hdrhistogram-go v1.1.2
66
github.com/RaduBerinde/axisds v0.0.0-20250419182453-5135a0650657
77
github.com/cespare/xxhash/v2 v2.2.0
8-
github.com/cockroachdb/crlib v0.0.0-20241112164430-1264a2edc35b
8+
github.com/cockroachdb/crlib v0.0.0-20250617202621-0794c595bbe6
99
github.com/cockroachdb/datadriven v1.0.3-0.20250407164829-2945557346d5
1010
github.com/cockroachdb/errors v1.11.3
1111
github.com/cockroachdb/metamorphic v0.0.0-20231108215700-4ba948b56895

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
3434
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
3535
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
3636
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
37-
github.com/cockroachdb/crlib v0.0.0-20241112164430-1264a2edc35b h1:SHlYZ/bMx7frnmeqCu+xm0TCxXLzX3jQIVuFbnFGtFU=
38-
github.com/cockroachdb/crlib v0.0.0-20241112164430-1264a2edc35b/go.mod h1:Gq51ZeKaFCXk6QwuGM0w1dnaOqc/F5zKT2zA9D6Xeac=
37+
github.com/cockroachdb/crlib v0.0.0-20250617202621-0794c595bbe6 h1:PZVolkXzVqPQQZ7Jm8/lGpI9ZM086mB55oOWqy0wG6E=
38+
github.com/cockroachdb/crlib v0.0.0-20250617202621-0794c595bbe6/go.mod h1:Gq51ZeKaFCXk6QwuGM0w1dnaOqc/F5zKT2zA9D6Xeac=
3939
github.com/cockroachdb/datadriven v1.0.3-0.20250407164829-2945557346d5 h1:UycK/E0TkisVrQbSoxvU827FwgBBcZ95nRRmpj/12QI=
4040
github.com/cockroachdb/datadriven v1.0.3-0.20250407164829-2945557346d5/go.mod h1:jsaKMvD3RBCATk1/jbUZM8C9idWBJME9+VRZ5+Liq1g=
4141
github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I=
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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

Comments
 (0)