Skip to content

Commit b2eda90

Browse files
authored
table: split style.go into individual files (#392)
1 parent 0b7174f commit b2eda90

File tree

7 files changed

+398
-383
lines changed

7 files changed

+398
-383
lines changed

table/style.go

Lines changed: 0 additions & 383 deletions
Large diffs are not rendered by default.

table/style_color.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
package table
2+
3+
import "github.com/jedib0t/go-pretty/v6/text"
4+
5+
// ColorOptions defines the ANSI colors to use for parts of the Table.
6+
type ColorOptions struct {
7+
Border text.Colors // borders (if nil, uses one of the below)
8+
Footer text.Colors // footer row(s) colors
9+
Header text.Colors // header row(s) colors
10+
IndexColumn text.Colors // index-column colors (row #, etc.)
11+
Row text.Colors // regular row(s) colors
12+
RowAlternate text.Colors // regular row(s) colors for the even-numbered rows
13+
Separator text.Colors // separators (if nil, uses one of the above)
14+
}
15+
16+
var (
17+
// ColorOptionsDefault defines sensible ANSI color options - basically NONE.
18+
ColorOptionsDefault = ColorOptions{}
19+
20+
// ColorOptionsBright renders dark text on bright background.
21+
ColorOptionsBright = ColorOptionsBlackOnCyanWhite
22+
23+
// ColorOptionsDark renders bright text on dark background.
24+
ColorOptionsDark = ColorOptionsCyanWhiteOnBlack
25+
26+
// ColorOptionsBlackOnBlueWhite renders Black text on Blue/White background.
27+
ColorOptionsBlackOnBlueWhite = ColorOptions{
28+
Footer: text.Colors{text.BgBlue, text.FgBlack},
29+
Header: text.Colors{text.BgHiBlue, text.FgBlack},
30+
IndexColumn: text.Colors{text.BgHiBlue, text.FgBlack},
31+
Row: text.Colors{text.BgHiWhite, text.FgBlack},
32+
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
33+
}
34+
35+
// ColorOptionsBlackOnCyanWhite renders Black text on Cyan/White background.
36+
ColorOptionsBlackOnCyanWhite = ColorOptions{
37+
Footer: text.Colors{text.BgCyan, text.FgBlack},
38+
Header: text.Colors{text.BgHiCyan, text.FgBlack},
39+
IndexColumn: text.Colors{text.BgHiCyan, text.FgBlack},
40+
Row: text.Colors{text.BgHiWhite, text.FgBlack},
41+
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
42+
}
43+
44+
// ColorOptionsBlackOnGreenWhite renders Black text on Green/White
45+
// background.
46+
ColorOptionsBlackOnGreenWhite = ColorOptions{
47+
Footer: text.Colors{text.BgGreen, text.FgBlack},
48+
Header: text.Colors{text.BgHiGreen, text.FgBlack},
49+
IndexColumn: text.Colors{text.BgHiGreen, text.FgBlack},
50+
Row: text.Colors{text.BgHiWhite, text.FgBlack},
51+
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
52+
}
53+
54+
// ColorOptionsBlackOnMagentaWhite renders Black text on Magenta/White
55+
// background.
56+
ColorOptionsBlackOnMagentaWhite = ColorOptions{
57+
Footer: text.Colors{text.BgMagenta, text.FgBlack},
58+
Header: text.Colors{text.BgHiMagenta, text.FgBlack},
59+
IndexColumn: text.Colors{text.BgHiMagenta, text.FgBlack},
60+
Row: text.Colors{text.BgHiWhite, text.FgBlack},
61+
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
62+
}
63+
64+
// ColorOptionsBlackOnRedWhite renders Black text on Red/White background.
65+
ColorOptionsBlackOnRedWhite = ColorOptions{
66+
Footer: text.Colors{text.BgRed, text.FgBlack},
67+
Header: text.Colors{text.BgHiRed, text.FgBlack},
68+
IndexColumn: text.Colors{text.BgHiRed, text.FgBlack},
69+
Row: text.Colors{text.BgHiWhite, text.FgBlack},
70+
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
71+
}
72+
73+
// ColorOptionsBlackOnYellowWhite renders Black text on Yellow/White
74+
// background.
75+
ColorOptionsBlackOnYellowWhite = ColorOptions{
76+
Footer: text.Colors{text.BgYellow, text.FgBlack},
77+
Header: text.Colors{text.BgHiYellow, text.FgBlack},
78+
IndexColumn: text.Colors{text.BgHiYellow, text.FgBlack},
79+
Row: text.Colors{text.BgHiWhite, text.FgBlack},
80+
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
81+
}
82+
83+
// ColorOptionsBlueWhiteOnBlack renders Blue/White text on Black background.
84+
ColorOptionsBlueWhiteOnBlack = ColorOptions{
85+
Footer: text.Colors{text.FgBlue, text.BgHiBlack},
86+
Header: text.Colors{text.FgHiBlue, text.BgHiBlack},
87+
IndexColumn: text.Colors{text.FgHiBlue, text.BgHiBlack},
88+
Row: text.Colors{text.FgHiWhite, text.BgBlack},
89+
RowAlternate: text.Colors{text.FgWhite, text.BgBlack},
90+
}
91+
92+
// ColorOptionsCyanWhiteOnBlack renders Cyan/White text on Black background.
93+
ColorOptionsCyanWhiteOnBlack = ColorOptions{
94+
Footer: text.Colors{text.FgCyan, text.BgHiBlack},
95+
Header: text.Colors{text.FgHiCyan, text.BgHiBlack},
96+
IndexColumn: text.Colors{text.FgHiCyan, text.BgHiBlack},
97+
Row: text.Colors{text.FgHiWhite, text.BgBlack},
98+
RowAlternate: text.Colors{text.FgWhite, text.BgBlack},
99+
}
100+
101+
// ColorOptionsGreenWhiteOnBlack renders Green/White text on Black
102+
// background.
103+
ColorOptionsGreenWhiteOnBlack = ColorOptions{
104+
Footer: text.Colors{text.FgGreen, text.BgHiBlack},
105+
Header: text.Colors{text.FgHiGreen, text.BgHiBlack},
106+
IndexColumn: text.Colors{text.FgHiGreen, text.BgHiBlack},
107+
Row: text.Colors{text.FgHiWhite, text.BgBlack},
108+
RowAlternate: text.Colors{text.FgWhite, text.BgBlack},
109+
}
110+
111+
// ColorOptionsMagentaWhiteOnBlack renders Magenta/White text on Black
112+
// background.
113+
ColorOptionsMagentaWhiteOnBlack = ColorOptions{
114+
Footer: text.Colors{text.FgMagenta, text.BgHiBlack},
115+
Header: text.Colors{text.FgHiMagenta, text.BgHiBlack},
116+
IndexColumn: text.Colors{text.FgHiMagenta, text.BgHiBlack},
117+
Row: text.Colors{text.FgHiWhite, text.BgBlack},
118+
RowAlternate: text.Colors{text.FgWhite, text.BgBlack},
119+
}
120+
121+
// ColorOptionsRedWhiteOnBlack renders Red/White text on Black background.
122+
ColorOptionsRedWhiteOnBlack = ColorOptions{
123+
Footer: text.Colors{text.FgRed, text.BgHiBlack},
124+
Header: text.Colors{text.FgHiRed, text.BgHiBlack},
125+
IndexColumn: text.Colors{text.FgHiRed, text.BgHiBlack},
126+
Row: text.Colors{text.FgHiWhite, text.BgBlack},
127+
RowAlternate: text.Colors{text.FgWhite, text.BgBlack},
128+
}
129+
130+
// ColorOptionsYellowWhiteOnBlack renders Yellow/White text on Black
131+
// background.
132+
ColorOptionsYellowWhiteOnBlack = ColorOptions{
133+
Footer: text.Colors{text.FgYellow, text.BgHiBlack},
134+
Header: text.Colors{text.FgHiYellow, text.BgHiBlack},
135+
IndexColumn: text.Colors{text.FgHiYellow, text.BgHiBlack},
136+
Row: text.Colors{text.FgHiWhite, text.BgBlack},
137+
RowAlternate: text.Colors{text.FgWhite, text.BgBlack},
138+
}
139+
)

table/style_format.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package table
2+
3+
import "github.com/jedib0t/go-pretty/v6/text"
4+
5+
// FormatOptions defines the text-formatting to perform on parts of the Table.
6+
type FormatOptions struct {
7+
Direction text.Direction // (forced) BiDi direction for each Column
8+
Footer text.Format // default text format
9+
FooterAlign text.Align // default horizontal align
10+
FooterVAlign text.VAlign // default vertical align
11+
Header text.Format // default text format
12+
HeaderAlign text.Align // default horizontal align
13+
HeaderVAlign text.VAlign // default vertical align
14+
Row text.Format // default text format
15+
RowAlign text.Align // default horizontal align
16+
RowVAlign text.VAlign // default vertical align
17+
}
18+
19+
var (
20+
// FormatOptionsDefault defines sensible formatting options.
21+
FormatOptionsDefault = FormatOptions{
22+
Footer: text.FormatUpper,
23+
FooterAlign: text.AlignDefault,
24+
FooterVAlign: text.VAlignDefault,
25+
Header: text.FormatUpper,
26+
HeaderAlign: text.AlignDefault,
27+
HeaderVAlign: text.VAlignDefault,
28+
Row: text.FormatDefault,
29+
RowAlign: text.AlignDefault,
30+
RowVAlign: text.VAlignDefault,
31+
}
32+
)

table/style_html.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package table
2+
3+
// HTMLOptions defines the global options to control HTML rendering.
4+
type HTMLOptions struct {
5+
ConvertColorsToSpans bool // convert ANSI escape sequences to HTML <span> tags with CSS classes? EscapeText will be true if this is true.
6+
CSSClass string // CSS class to set on the overall <table> tag
7+
EmptyColumn string // string to replace "" columns with (entire content being "")
8+
EscapeText bool // escape text into HTML-safe content?
9+
Newline string // string to replace "\n" characters with
10+
}
11+
12+
var (
13+
// DefaultHTMLOptions defines sensible HTML rendering defaults.
14+
DefaultHTMLOptions = HTMLOptions{
15+
ConvertColorsToSpans: true,
16+
CSSClass: DefaultHTMLCSSClass,
17+
EmptyColumn: "&nbsp;",
18+
EscapeText: true,
19+
Newline: "<br/>",
20+
}
21+
)

table/style_options.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package table
2+
3+
// Options defines the global options that determine how the Table is
4+
// rendered.
5+
type Options struct {
6+
// DoNotColorBordersAndSeparators disables coloring all the borders and row
7+
// or column separators.
8+
DoNotColorBordersAndSeparators bool
9+
10+
// DrawBorder enables or disables drawing the border around the Table.
11+
// Example of a table where it is disabled:
12+
// # │ FIRST NAME │ LAST NAME │ SALARY │
13+
// ─────┼────────────┼───────────┼────────┼─────────────────────────────
14+
// 1 │ Arya │ Stark │ 3000 │
15+
// 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow!
16+
// 300 │ Tyrion │ Lannister │ 5000 │
17+
// ─────┼────────────┼───────────┼────────┼─────────────────────────────
18+
// │ │ TOTAL │ 10000 │
19+
DrawBorder bool
20+
21+
// SeparateColumns enables or disable drawing border between columns.
22+
// Example of a table where it is disabled:
23+
// ┌─────────────────────────────────────────────────────────────────┐
24+
// │ # FIRST NAME LAST NAME SALARY │
25+
// ├─────────────────────────────────────────────────────────────────┤
26+
// │ 1 Arya Stark 3000 │
27+
// │ 20 Jon Snow 2000 You know nothing, Jon Snow! │
28+
// │ 300 Tyrion Lannister 5000 │
29+
// │ TOTAL 10000 │
30+
// └─────────────────────────────────────────────────────────────────┘
31+
SeparateColumns bool
32+
33+
// SeparateFooter enables or disable drawing border between the footer and
34+
// the rows. Example of a table where it is disabled:
35+
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
36+
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
37+
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
38+
// │ 1 │ Arya │ Stark │ 3000 │ │
39+
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
40+
// │ 300 │ Tyrion │ Lannister │ 5000 │ │
41+
// │ │ │ TOTAL │ 10000 │ │
42+
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
43+
SeparateFooter bool
44+
45+
// SeparateHeader enables or disable drawing border between the header and
46+
// the rows. Example of a table where it is disabled:
47+
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
48+
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
49+
// │ 1 │ Arya │ Stark │ 3000 │ │
50+
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
51+
// │ 300 │ Tyrion │ Lannister │ 5000 │ │
52+
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
53+
// │ │ │ TOTAL │ 10000 │ │
54+
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
55+
SeparateHeader bool
56+
57+
// SeparateRows enables or disables drawing separators between each row.
58+
// Example of a table where it is enabled:
59+
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
60+
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
61+
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
62+
// │ 1 │ Arya │ Stark │ 3000 │ │
63+
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
64+
// │ 20 │ Jon │ Snow │ 2000 │ You know nothing, Jon Snow! │
65+
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
66+
// │ 300 │ Tyrion │ Lannister │ 5000 │ │
67+
// ├─────┼────────────┼───────────┼────────┼─────────────────────────────┤
68+
// │ │ │ TOTAL │ 10000 │ │
69+
// └─────┴────────────┴───────────┴────────┴─────────────────────────────┘
70+
SeparateRows bool
71+
}
72+
73+
var (
74+
// OptionsDefault defines sensible global options.
75+
OptionsDefault = Options{
76+
DoNotColorBordersAndSeparators: false,
77+
DrawBorder: true,
78+
SeparateColumns: true,
79+
SeparateFooter: true,
80+
SeparateHeader: true,
81+
SeparateRows: false,
82+
}
83+
84+
// OptionsNoBorders sets up a table without any borders.
85+
OptionsNoBorders = Options{
86+
DoNotColorBordersAndSeparators: false,
87+
DrawBorder: false,
88+
SeparateColumns: true,
89+
SeparateFooter: true,
90+
SeparateHeader: true,
91+
SeparateRows: false,
92+
}
93+
94+
// OptionsNoBordersAndSeparators sets up a table without any borders or
95+
// separators.
96+
OptionsNoBordersAndSeparators = Options{
97+
DoNotColorBordersAndSeparators: false,
98+
DrawBorder: false,
99+
SeparateColumns: false,
100+
SeparateFooter: false,
101+
SeparateHeader: false,
102+
SeparateRows: false,
103+
}
104+
)

table/style_size.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package table
2+
3+
// SizeOptions defines the way to control the width of the table output.
4+
type SizeOptions struct {
5+
// WidthMax is the maximum allotted width for the full row;
6+
// any content beyond this will be truncated using the text
7+
// in Style.Box.UnfinishedRow
8+
WidthMax int
9+
// WidthMin is the minimum allotted width for the full row;
10+
// columns will be auto-expanded until the overall width
11+
// is met
12+
WidthMin int
13+
}
14+
15+
var (
16+
// SizeOptionsDefault defines sensible size options - basically NONE.
17+
SizeOptionsDefault = SizeOptions{
18+
WidthMax: 0,
19+
WidthMin: 0,
20+
}
21+
)

0 commit comments

Comments
 (0)