Skip to content

Commit

Permalink
table: SetColumnConfigs() to control column rendering (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t authored May 30, 2019
1 parent 50be2aa commit 918cee6
Show file tree
Hide file tree
Showing 99 changed files with 21,102 additions and 164 deletions.
56 changes: 56 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Pretty-print tables into ASCII/Unicode strings.

<img src="table/images/table-StyleColoredBright.png" width="640px"/>

Detailed documentation can be found here: [table/](table/)
Detailed documentation can be found here: [table/](table)

## List

Expand All @@ -44,7 +44,7 @@ Pretty-print lists with multiple levels/indents into ASCII/Unicode strings.
■ The Gunslinger
```

Detailed documentation can be found here: [list/](list/)
Detailed documentation can be found here: [list/](list)

# Progress

Expand All @@ -65,7 +65,7 @@ Downloading File # 9 ... 32.1% (●●●●●●●○◌◌◌◌◌◌
Transferring Amount # 10 ... 13.0% (●●○◌◌◌◌◌◌◌◌◌◌◌◌◌◌◌◌◌◌◌◌) [£32.50K in 198.84ms]
```

Detailed documentation can be found here: [progress/](progress/)
Detailed documentation can be found here: [progress/](progress)

## Text

Expand All @@ -78,10 +78,12 @@ of the functions available are used in one or more of the other packages here.
- [text/color.go](text/color.go)
- Cursor Movement
- [text/cursor.go](text/cursor.go)
- Format text (convert case for now)
- Format text (convert case)
- [text/format.go](text/format.go)
- String Manipulation (Pad, RepeatAndTrim, RuneCount, Trim, etc.)
- [text/string.go](text/string.go)
- Transform text (UnixTime to human-readable-time, pretty-JSON, etc.)
- [text/transformer.go](text/transformer.go)
- Wrap text
- [text/wrap.go](text/wrap.go)

Expand Down
70 changes: 31 additions & 39 deletions table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Pretty-print tables into ASCII/Unicode strings.
- Custom (vertical) VAlign per column (and multi-line column support)
- Mirror output to an io.Writer object (like os.StdOut)
- Sort by any of the Columns (by Column Name or Number)
- Transformers to customize individual cell rendering
- Completely customizable styles
- Many ready-to-use styles: [style.go](style.go)
- Colorize Headers/Body/Footers using [../text/color.go](../text/color.go)
Expand Down Expand Up @@ -230,40 +231,39 @@ to get:
+-----+------------+-----------+--------+------- ~
```

Or restrict the maximum (text) width for a Column:
```go
t.SetAllowedColumnLengths([]int{0, 6, 9, 6, 10})
t.SetStyle(table.StyleRounded)
t.Render()
```
to get:
```
╭─────┬────────┬───────────┬────────┬────────────╮
│ # │ FIRST │ LAST NAME │ SALARY │ │
│ │ NAME │ │ │ │
├─────┼────────┼───────────┼────────┼────────────┤
│ 1 │ Arya │ Stark │ 3000 │ │
│ 20 │ Jon │ Snow │ 2000 │ You know n │
│ │ │ │ │ othing, Jo │
│ │ │ │ │ n Snow! │
│ 300 │ Tyrion │ Lannister │ 5000 │ │
├─────┼────────┼───────────┼────────┼────────────┤
│ │ │ TOTAL │ 10000 │ │
╰─────┴────────┴───────────┴────────┴────────────╯
```
## Column Control - Alignment, Colors, Width and more

## Column Control - Alignment & Colors

You can align text in columns horizontally and/or vertically. You can set up
per-column color styles that will override the directives from the global Style
set through `SetStyle()`.
You can control a lot of things about individual cells/columns which overrides
global properties/styles using the `SetColumnConfig()` interface:
- Alignment (horizontal & vertical)
- Colorization
- Transform individual cells based on the content
- Width (minimum & maximum)

```go
t.SetAlign([]text.Align{text.AlignDefault, text.AlignRight, text.AlignDefault, text.AlignDefault, text.AlignCenter})
t.SetVAlign([]text.VAlign{text.VAlignDefault, text.VAlignMiddle, text.VAlignBottom, text.VAlignMiddle})
t.SetColors([]text.Colors{{text.FgWhite, text.BgBlack}, {text.FgWhite, text.BgBlack}})
t.SetColorsFooter([]text.Colors{{text.FgRed}, {text.FgGreen}, {text.FgBlue}})
t.SetColorsHeader([]text.Colors{{text.FgCyan}, {text.FgMagenta}, {text.FgYellow}, {text.FgBlack, text.BgWhite}})
nameTransformer := text.Transformer(func(val interface{}) string {
return text.Bold.Sprint(val)
})

t.SetColumnConfigs([]ColumnConfig{
{
Name: "First Name",
Align: text.AlignLeft,
AlignFooter: text.AlignLeft,
AlignHeader: text.AlignLeft,
Colors: text.Colors{text.BgBlack, text.FgRed},
ColorsHeader: text.Colors{text.BgRed, text.FgBlack, text.Bold},
ColorsFooter: text.Colors{text.BgRed, text.FgBlack},
Transformer: nameTransformer,
TransformerFooter: nameTransformer,
TransformerHeader: nameTransformer,
VAlign: text.VAlignMiddle,
VAlignFooter: text.VAlignTop,
VAlignHeader: text.VAlignBottom,
WidthMin: 6,
WidthMax: 64,
}
})
```

## Render As ...
Expand Down Expand Up @@ -350,11 +350,3 @@ to get:
| 300 | Tyrion | Lannister | 5000 | |
| | | Total | 10000 | |
```

# TODO

- Generic Cell Content Transformers (with some ready-made ones)
- Base64 Decoder
- Currency Formatter
- UnixTime to Date & Time
- Status Formatter (color "FAILED" in RED, etc.)
52 changes: 52 additions & 0 deletions table/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package table

import (
"github.com/jedib0t/go-pretty/text"
)

// ColumnConfig contains configurations that determine and modify the way the
// contents of the column get rendered.
type ColumnConfig struct {
// Name is the name of the Column as it appears in the first Header row.
// If a Header is not provided, or the name is not found in the header, this
// will not work.
Name string
// Number is the Column # from left. When specified, it overrides the Name
// property. If you know the exact Column number, use this instead of Name.
Number int

// Align defines the horizontal alignment
Align text.Align
// AlignFooter defines the horizontal alignment of Footer rows
AlignFooter text.Align
// AlignHeader defines the horizontal alignment of Header rows
AlignHeader text.Align

// Colors defines the colors to be used on the column
Colors text.Colors
// ColorsFooter defines the colors to be used on the column in Footer rows
ColorsFooter text.Colors
// ColorsHeader defines the colors to be used on the column in Header rows
ColorsHeader text.Colors

// Transformer is a custom-function that changes the way the value gets
// rendered to the console. Refer to text/transformer.go for ready-to-use
// Transformer functions.
Transformer text.Transformer
// TransformerFooter is like Transformer but for Footer rows
TransformerFooter text.Transformer
// TransformerHeader is like Transformer but for Header rows
TransformerHeader text.Transformer

// VAlign defines the vertical alignment
VAlign text.VAlign
// VAlignFooter defines the vertical alignment in Footer rows
VAlignFooter text.VAlign
// VAlignHeader defines the vertical alignment in Header rows
VAlignHeader text.VAlign

// WidthMin defines the minimum character length of the column
WidthMin int
// WidthMax defines the maximum character length of the column
WidthMax int
}
8 changes: 4 additions & 4 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (t *Table) Render() string {
t.renderRowsHeader(&out)

// (data) rows
t.renderRows(&out, t.getRowsSorted(), renderHint{})
t.renderRows(&out, t.rows, renderHint{})

// footer rows
t.renderRowsFooter(&out)
Expand Down Expand Up @@ -106,9 +106,9 @@ func (t *Table) renderColumnAutoIndex(out *strings.Builder, hint renderHint) {
}

func (t *Table) renderColumnColorized(out *strings.Builder, colIdx int, colStr string, hint renderHint) {
colors := t.getRowColors(hint)
if colIdx < len(colors) && colors[colIdx] != nil {
out.WriteString(colors[colIdx].Sprint(colStr))
colors := t.getColumnColors(colIdx, hint)
if colors != nil {
out.WriteString(colors.Sprint(colStr))
} else if hint.isHeaderRow && t.style.Color.Header != nil {
out.WriteString(t.style.Color.Header.Sprint(colStr))
} else if hint.isFooterRow && t.style.Color.Footer != nil {
Expand Down
2 changes: 1 addition & 1 deletion table/render_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (t *Table) RenderCSV() string {
var out strings.Builder
if t.numColumns > 0 {
t.csvRenderRows(&out, t.rowsHeader)
t.csvRenderRows(&out, t.getRowsSorted())
t.csvRenderRows(&out, t.rows)
t.csvRenderRows(&out, t.rowsFooter)
}
return t.render(&out)
Expand Down
11 changes: 9 additions & 2 deletions table/render_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (t *Table) RenderHTML() string {
}
out.WriteString("\">\n")
t.htmlRenderRows(&out, t.rowsHeader, renderHint{isHeaderRow: true})
t.htmlRenderRows(&out, t.getRowsSorted(), renderHint{})
t.htmlRenderRows(&out, t.rows, renderHint{})
t.htmlRenderRows(&out, t.rowsFooter, renderHint{isFooterRow: true})
out.WriteString("</table>")
}
Expand All @@ -92,6 +92,8 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
// determine the HTML "align"/"valign" property values
align := t.getAlign(colIdx, hint).HTMLProperty()
vAlign := t.getVAlign(colIdx, hint).HTMLProperty()
// determine the HTML "class" property values for the colors
class := t.getColumnColors(colIdx, hint).HTMLProperty()

// write the row
out.WriteString(" <")
Expand All @@ -100,6 +102,10 @@ func (t *Table) htmlRenderRow(out *strings.Builder, row rowStr, hint renderHint)
out.WriteRune(' ')
out.WriteString(align)
}
if class != "" {
out.WriteRune(' ')
out.WriteString(class)
}
if vAlign != "" {
out.WriteRune(' ')
out.WriteString(vAlign)
Expand Down Expand Up @@ -128,7 +134,8 @@ func (t *Table) htmlRenderRows(out *strings.Builder, rows []rowStr, hint renderH
}

var renderedTagOpen, shouldRenderTagClose bool
for _, row := range rows {
for idx, row := range rows {
hint.rowNumber = idx + 1
if len(row) > 0 {
if !renderedTagOpen {
out.WriteString(" <")
Expand Down
Loading

0 comments on commit 918cee6

Please sign in to comment.