Skip to content

Commit

Permalink
table: a lot more ready-to-use Color Styles (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed May 15, 2018
1 parent e026101 commit 4c495c9
Show file tree
Hide file tree
Showing 7 changed files with 311 additions and 24 deletions.
5 changes: 5 additions & 0 deletions cmd/demo-table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,8 @@ Table without Any Borders or Separators!
[Markdown] | | | Total | 10000 | |
```


Output of `go run cmd/demo-table/demo.go colors`:

<img src="demo-colors.png" width="600px"/>
Binary file added cmd/demo-table/demo-colors.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 55 additions & 1 deletion cmd/demo-table/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,53 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text"
)

func main() {
func demoTableColors() {
tw := table.NewWriter()
tw.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
tw.AppendRows([]table.Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
tw.AppendFooter(table.Row{"", "", "Total", 10000})
tw.SetIndexColumn(1)

stylePairs := [][]table.Style{
{table.StyleColoredBright, table.StyleColoredDark},
{table.StyleColoredBlackOnBlueWhite, table.StyleColoredBlueWhiteOnBlack},
{table.StyleColoredBlackOnCyanWhite, table.StyleColoredCyanWhiteOnBlack},
{table.StyleColoredBlackOnGreenWhite, table.StyleColoredGreenWhiteOnBlack},
{table.StyleColoredBlackOnMagentaWhite, table.StyleColoredMagentaWhiteOnBlack},
{table.StyleColoredBlackOnRedWhite, table.StyleColoredRedWhiteOnBlack},
{table.StyleColoredBlackOnYellowWhite, table.StyleColoredYellowWhiteOnBlack},
}

twOuter := table.NewWriter()
twOuter.AppendHeader(table.Row{"Bright", "Dark"})
twOuter.SetAlignHeader([]text.Align{text.AlignCenter, text.AlignCenter})
for _, stylePair := range stylePairs {
row := make(table.Row, 2)
for idx, style := range stylePair {
tw.SetStyle(style)
tw.SetCaption("%s", style.Name)
row[idx] = tw.Render()
}
twOuter.AppendRow(row)
}
twOuter.SetAlign([]text.Align{text.AlignCenter, text.AlignCenter})
twOuter.SetStyle(table.StyleLight)
twOuter.Style().Options.SeparateRows = true
fmt.Println(twOuter.Render())
}

func demoTableFeatures() {
//==========================================================================
// Initialization
//==========================================================================
Expand Down Expand Up @@ -552,3 +592,17 @@ func main() {
// That's it for today! New features will always find a place in this demo!
//==========================================================================
}

func main() {
demoWhat := "features"
if len(os.Args) > 1 {
demoWhat = os.Args[1]
}

switch strings.ToLower(demoWhat) {
case "colors":
demoTableColors()
default:
demoTableFeatures()
}
}
4 changes: 2 additions & 2 deletions table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Pretty-print tables into ASCII/Unicode strings.
- Mirror output to an io.Writer object (like os.StdOut)
- Completely customizable styles
- Many ready-to-use styles: [style.go](style.go)
- Colorize Headers/Body/Footers using [../text/color](../text/color)
- Colorize Headers/Body/Footers using [../text/color.go](../text/color.go)
- Custom text-case for Headers/Body/Footers
- Enable separators between each row
- Render table without a Border
Expand Down Expand Up @@ -116,7 +116,7 @@ Or if you want to use a full-color mode, and don't care for boxes, use:
```
to get:

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

### Roll your own Style

Expand Down
2 changes: 1 addition & 1 deletion table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func TestTable_Render_ColoredStyleAutoIndex(t *testing.T) {
expectedOut := strings.Join([]string{
"\x1b[96;100m \x1b[0m\x1b[96;100m # \x1b[0m\x1b[96;100m FIRST NAME \x1b[0m\x1b[96;100m LAST NAME \x1b[0m\x1b[96;100m SALARY \x1b[0m\x1b[96;100m \x1b[0m",
"\x1b[96;100m 1 \x1b[0m\x1b[97;40m 1 \x1b[0m\x1b[97;40m Arya \x1b[0m\x1b[97;40m Stark \x1b[0m\x1b[97;40m 3000 \x1b[0m\x1b[97;40m \x1b[0m",
"\x1b[96;100m 2 \x1b[0m\x1b[90;40m 20 \x1b[0m\x1b[90;40m Jon \x1b[0m\x1b[90;40m Snow \x1b[0m\x1b[90;40m 2000 \x1b[0m\x1b[90;40m You know nothing, Jon Snow! \x1b[0m",
"\x1b[96;100m 2 \x1b[0m\x1b[37;40m 20 \x1b[0m\x1b[37;40m Jon \x1b[0m\x1b[37;40m Snow \x1b[0m\x1b[37;40m 2000 \x1b[0m\x1b[37;40m You know nothing, Jon Snow! \x1b[0m",
"\x1b[96;100m 3 \x1b[0m\x1b[97;40m 300 \x1b[0m\x1b[97;40m Tyrion \x1b[0m\x1b[97;40m Lannister \x1b[0m\x1b[97;40m 5000 \x1b[0m\x1b[97;40m \x1b[0m",
"\x1b[96;100m \x1b[0m\x1b[36;100m \x1b[0m\x1b[36;100m \x1b[0m\x1b[36;100m TOTAL \x1b[0m\x1b[36;100m 10000 \x1b[0m\x1b[36;100m \x1b[0m",
}, "\n")
Expand Down

0 comments on commit 4c495c9

Please sign in to comment.