Skip to content

Commit

Permalink
remove unnecessary dependency on fatih/color (#37)
Browse files Browse the repository at this point in the history
* remove dependency on fatih/color (used only the Attribute any way)
* table: collapse Style.Color.AutoIndexColumn/FirstColumn into IndexColumn
  • Loading branch information
jedib0t authored May 15, 2018
1 parent 2e8d249 commit e026101
Show file tree
Hide file tree
Showing 267 changed files with 207 additions and 153,791 deletions.
26 changes: 1 addition & 25 deletions Gopkg.lock

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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: [table/style.go](table/style.go)
- Colorize Headers/Body/Footers using [github.com/fatih/color](https://github.com/fatih/color)
- Colorize Headers/Body/Footers using [text/color](text/color.go)
- Custom text-case for Headers/Body/Footers
- Enable separators between each row
- Render table without a Border
Expand Down Expand Up @@ -86,7 +86,7 @@ Specifically, `table` and `list` use these extensively:
- [text/align.go](text/align.go)
- Align text vertically
- [text/valign.go](text/valign.go)
- Colorize text using a simpler interface to [github.com/fatih/color](https://github.com/fatih/color)
- Colorize text
- [text/color.go](text/color.go)
- Format text (convert case for now)
- [text/format.go](text/format.go)
Expand Down
5 changes: 2 additions & 3 deletions cmd/demo-table/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/table"
"github.com/jedib0t/go-pretty/text"
)
Expand Down Expand Up @@ -352,9 +351,9 @@ func main() {
// I need some color in my life!
//==========================================================================
t.SetStyle(table.StyleBold)
colorBOnW := text.Colors{color.BgWhite, color.FgBlack}
colorBOnW := text.Colors{text.BgWhite, text.FgBlack}
t.SetColorsHeader([]text.Colors{colorBOnW, colorBOnW, colorBOnW, colorBOnW, colorBOnW})
t.SetColors([]text.Colors{{color.FgYellow}, {color.FgHiRed}, {color.FgHiRed}, {color.FgGreen}, {color.FgCyan}})
t.SetColors([]text.Colors{{text.FgYellow}, {text.FgHiRed}, {text.FgHiRed}, {text.FgGreen}, {text.FgCyan}})
t.SetColorsFooter([]text.Colors{{}, {}, colorBOnW, colorBOnW})
t.SetCaption("Table with Colors.\n")
fmt.Println(t.Render())
Expand Down
5 changes: 3 additions & 2 deletions cmd/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func demoList() string {
func demoTable() string {
styles := []table.Style{
table.StyleDefault,
table.StyleLight,
table.StyleBold,
table.StyleRounded,
table.StyleBold,
table.StyleColoredBright,
table.StyleColoredDark,
}
header := table.Row{"#", "First Name", "Last Name", "Salary"}
rows1And2 := []table.Row{
Expand All @@ -74,6 +74,7 @@ func demoTable() string {
tw.AppendRow(row3)
tw.AppendFooter(footer)
tw.SetAlign(align)
tw.SetIndexColumn(1)
tw.SetStyle(style)

if len(content) == 0 {
Expand Down
Binary file modified cmd/demo/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion 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 [github.com/fatih/color](https://github.com/fatih/color)
- Colorize Headers/Body/Footers using [../text/color](../text/color)
- Custom text-case for Headers/Body/Footers
- Enable separators between each row
- Render table without a Border
Expand Down
14 changes: 5 additions & 9 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import (
"github.com/jedib0t/go-pretty/util"
)

func (h *renderHint) isRegularRow() bool {
return !h.isHeaderRow && !h.isFooterRow
}

// Render renders the Table in a human-readable "pretty" format. Example:
// ┌─────┬────────────┬───────────┬────────┬─────────────────────────────┐
// │ # │ FIRST NAME │ LAST NAME │ SALARY │ │
Expand Down Expand Up @@ -106,8 +102,8 @@ func (t *Table) renderColumnAutoIndex(out *strings.Builder, rowNum int, hint ren
outAutoIndex.WriteString(t.style.Box.PaddingRight)
}

if t.style.Color.AutoIndexColumn != nil {
out.WriteString(t.style.Color.AutoIndexColumn.Sprintf(outAutoIndex.String()))
if t.style.Color.IndexColumn != nil {
out.WriteString(t.style.Color.IndexColumn.Sprintf(outAutoIndex.String()))
} else {
out.WriteString(outAutoIndex.String())
}
Expand All @@ -122,9 +118,9 @@ func (t *Table) renderColumnColorized(out *strings.Builder, rowNum int, colIdx i
} else if hint.isFooterRow && t.style.Color.Footer != nil {
out.WriteString(t.style.Color.Footer.Sprint(colStr))
} else if hint.isRegularRow() {
if colIdx == 0 && t.style.Color.FirstColumn != nil {
out.WriteString(t.style.Color.FirstColumn.Sprint(colStr))
} else if rowNum%2 == 1 && t.style.Color.RowAlternate != nil {
if colIdx == t.indexColumn-1 && t.style.Color.IndexColumn != nil {
out.WriteString(t.style.Color.IndexColumn.Sprint(colStr))
} else if rowNum%2 == 0 && t.style.Color.RowAlternate != nil {
out.WriteString(t.style.Color.RowAlternate.Sprint(colStr))
} else if t.style.Color.Row != nil {
out.WriteString(t.style.Color.Row.Sprint(colStr))
Expand Down
34 changes: 17 additions & 17 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestTable_Render_ColoredTableWithinATable(t *testing.T) {
table.AppendRows(testRows)
table.AppendFooter(testFooter)
table.SetStyle(StyleColoredBright)
table.Style().Color.FirstColumn = StyleColoredBright.Color.AutoIndexColumn
table.SetIndexColumn(1)

// colored is simple; render the colored table into another table
tableOuter := Table{}
Expand All @@ -230,9 +230,9 @@ func TestTable_Render_ColoredTableWithinATable(t *testing.T) {
expectedOut := strings.Join([]string{
"╭───────────────────────────────────────────────────────────────────╮",
"│ \x1b[106;30m # \x1b[0m\x1b[106;30m FIRST NAME \x1b[0m\x1b[106;30m LAST NAME \x1b[0m\x1b[106;30m SALARY \x1b[0m\x1b[106;30m \x1b[0m │",
"│ \x1b[106;30m 1 \x1b[0m\x1b[47;30m Arya \x1b[0m\x1b[47;30m Stark \x1b[0m\x1b[47;30m 3000 \x1b[0m\x1b[47;30m \x1b[0m │",
"│ \x1b[106;30m 20 \x1b[0m\x1b[107;30m Jon \x1b[0m\x1b[107;30m Snow \x1b[0m\x1b[107;30m 2000 \x1b[0m\x1b[107;30m You know nothing, Jon Snow! \x1b[0m │",
"│ \x1b[106;30m 300 \x1b[0m\x1b[47;30m Tyrion \x1b[0m\x1b[47;30m Lannister \x1b[0m\x1b[47;30m 5000 \x1b[0m\x1b[47;30m \x1b[0m │",
"│ \x1b[106;30m 1 \x1b[0m\x1b[107;30m Arya \x1b[0m\x1b[107;30m Stark \x1b[0m\x1b[107;30m 3000 \x1b[0m\x1b[107;30m \x1b[0m │",
"│ \x1b[106;30m 20 \x1b[0m\x1b[47;30m Jon \x1b[0m\x1b[47;30m Snow \x1b[0m\x1b[47;30m 2000 \x1b[0m\x1b[47;30m You know nothing, Jon Snow! \x1b[0m │",
"│ \x1b[106;30m 300 \x1b[0m\x1b[107;30m Tyrion \x1b[0m\x1b[107;30m Lannister \x1b[0m\x1b[107;30m 5000 \x1b[0m\x1b[107;30m \x1b[0m │",
"│ \x1b[46;30m \x1b[0m\x1b[46;30m \x1b[0m\x1b[46;30m TOTAL \x1b[0m\x1b[46;30m 10000 \x1b[0m\x1b[46;30m \x1b[0m │",
"╰───────────────────────────────────────────────────────────────────╯",
}, "\n")
Expand All @@ -255,7 +255,7 @@ func TestTable_Render_ColoredTableWithinAColoredTable(t *testing.T) {
table.AppendRows(testRows)
table.AppendFooter(testFooter)
table.SetStyle(StyleColoredBright)
table.Style().Color.FirstColumn = StyleColoredBright.Color.AutoIndexColumn
table.SetIndexColumn(1)

// colored is simple; render the colored table into another colored table
tableOuter := Table{}
Expand All @@ -266,13 +266,13 @@ func TestTable_Render_ColoredTableWithinAColoredTable(t *testing.T) {

expectedOut := strings.Join([]string{
"\x1b[106;30m COLORED TABLE WITHIN A COLORED TABLE \x1b[0m",
"\x1b[47;30m \x1b[0m",
"\x1b[47;30m \x1b[106;30m # \x1b[0m\x1b[47;30m\x1b[106;30m FIRST NAME \x1b[0m\x1b[47;30m\x1b[106;30m LAST NAME \x1b[0m\x1b[47;30m\x1b[106;30m SALARY \x1b[0m\x1b[47;30m\x1b[106;30m \x1b[0m\x1b[47;30m \x1b[0m",
"\x1b[47;30m \x1b[106;30m 1 \x1b[0m\x1b[47;30m\x1b[47;30m Arya \x1b[0m\x1b[47;30m\x1b[47;30m Stark \x1b[0m\x1b[47;30m\x1b[47;30m 3000 \x1b[0m\x1b[47;30m\x1b[47;30m \x1b[0m\x1b[47;30m \x1b[0m",
"\x1b[47;30m \x1b[106;30m 20 \x1b[0m\x1b[47;30m\x1b[107;30m Jon \x1b[0m\x1b[47;30m\x1b[107;30m Snow \x1b[0m\x1b[47;30m\x1b[107;30m 2000 \x1b[0m\x1b[47;30m\x1b[107;30m You know nothing, Jon Snow! \x1b[0m\x1b[47;30m \x1b[0m",
"\x1b[47;30m \x1b[106;30m 300 \x1b[0m\x1b[47;30m\x1b[47;30m Tyrion \x1b[0m\x1b[47;30m\x1b[47;30m Lannister \x1b[0m\x1b[47;30m\x1b[47;30m 5000 \x1b[0m\x1b[47;30m\x1b[47;30m \x1b[0m\x1b[47;30m \x1b[0m",
"\x1b[47;30m \x1b[46;30m \x1b[0m\x1b[47;30m\x1b[46;30m \x1b[0m\x1b[47;30m\x1b[46;30m TOTAL \x1b[0m\x1b[47;30m\x1b[46;30m 10000 \x1b[0m\x1b[47;30m\x1b[46;30m \x1b[0m\x1b[47;30m \x1b[0m",
"\x1b[47;30m \x1b[0m",
"\x1b[107;30m \x1b[0m",
"\x1b[107;30m \x1b[106;30m # \x1b[0m\x1b[107;30m\x1b[106;30m FIRST NAME \x1b[0m\x1b[107;30m\x1b[106;30m LAST NAME \x1b[0m\x1b[107;30m\x1b[106;30m SALARY \x1b[0m\x1b[107;30m\x1b[106;30m \x1b[0m\x1b[107;30m \x1b[0m",
"\x1b[107;30m \x1b[106;30m 1 \x1b[0m\x1b[107;30m\x1b[107;30m Arya \x1b[0m\x1b[107;30m\x1b[107;30m Stark \x1b[0m\x1b[107;30m\x1b[107;30m 3000 \x1b[0m\x1b[107;30m\x1b[107;30m \x1b[0m\x1b[107;30m \x1b[0m",
"\x1b[107;30m \x1b[106;30m 20 \x1b[0m\x1b[107;30m\x1b[47;30m Jon \x1b[0m\x1b[107;30m\x1b[47;30m Snow \x1b[0m\x1b[107;30m\x1b[47;30m 2000 \x1b[0m\x1b[107;30m\x1b[47;30m You know nothing, Jon Snow! \x1b[0m\x1b[107;30m \x1b[0m",
"\x1b[107;30m \x1b[106;30m 300 \x1b[0m\x1b[107;30m\x1b[107;30m Tyrion \x1b[0m\x1b[107;30m\x1b[107;30m Lannister \x1b[0m\x1b[107;30m\x1b[107;30m 5000 \x1b[0m\x1b[107;30m\x1b[107;30m \x1b[0m\x1b[107;30m \x1b[0m",
"\x1b[107;30m \x1b[46;30m \x1b[0m\x1b[107;30m\x1b[46;30m \x1b[0m\x1b[107;30m\x1b[46;30m TOTAL \x1b[0m\x1b[107;30m\x1b[46;30m 10000 \x1b[0m\x1b[107;30m\x1b[46;30m \x1b[0m\x1b[107;30m \x1b[0m",
"\x1b[107;30m \x1b[0m",
}, "\n")
out := tableOuter.Render()
assert.Equal(t, expectedOut, out)
Expand All @@ -296,11 +296,11 @@ func TestTable_Render_ColoredStyleAutoIndex(t *testing.T) {
table.SetStyle(StyleColoredDark)

expectedOut := strings.Join([]string{
"\x1b[96;40m \x1b[0m\x1b[96;40m # \x1b[0m\x1b[96;40m FIRST NAME \x1b[0m\x1b[96;40m LAST NAME \x1b[0m\x1b[96;40m SALARY \x1b[0m\x1b[96;40m \x1b[0m",
"\x1b[96;40m 1 \x1b[0m\x1b[37;40m 1 \x1b[0m\x1b[37;40m Arya \x1b[0m\x1b[37;40m Stark \x1b[0m\x1b[37;40m 3000 \x1b[0m\x1b[37;40m \x1b[0m",
"\x1b[96;40m 2 \x1b[0m\x1b[97;40m 20 \x1b[0m\x1b[97;40m Jon \x1b[0m\x1b[97;40m Snow \x1b[0m\x1b[97;40m 2000 \x1b[0m\x1b[97;40m You know nothing, Jon Snow! \x1b[0m",
"\x1b[96;40m 3 \x1b[0m\x1b[37;40m 300 \x1b[0m\x1b[37;40m Tyrion \x1b[0m\x1b[37;40m Lannister \x1b[0m\x1b[37;40m 5000 \x1b[0m\x1b[37;40m \x1b[0m",
"\x1b[96;40m \x1b[0m\x1b[36;40m \x1b[0m\x1b[36;40m \x1b[0m\x1b[36;40m TOTAL \x1b[0m\x1b[36;40m 10000 \x1b[0m\x1b[36;40m \x1b[0m",
"\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 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")
out := table.Render()
assert.Equal(t, expectedOut, out)
Expand Down
45 changes: 20 additions & 25 deletions table/style.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package table

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

Expand Down Expand Up @@ -344,45 +343,41 @@ var (

// ColorOptions defines the ANSI colors to use for parts of the Table.
type ColorOptions struct {
AutoIndexColumn text.Colors
FirstColumn text.Colors
Footer text.Colors
Header text.Colors
Row text.Colors
RowAlternate text.Colors
IndexColumn text.Colors
Footer text.Colors
Header text.Colors
Row text.Colors
RowAlternate text.Colors
}

var (
// ColorOptionsDefault defines sensible ANSI color options - basically NONE.
ColorOptionsDefault = ColorOptions{
AutoIndexColumn: nil,
FirstColumn: nil,
Footer: nil,
Header: nil,
Row: nil,
RowAlternate: nil,
IndexColumn: nil,
Footer: nil,
Header: nil,
Row: nil,
RowAlternate: nil,
}

// ColorOptionsBright defines ANSI color options to render dark text on
// bright background.
ColorOptionsBright = ColorOptions{
AutoIndexColumn: text.Colors{color.BgHiCyan, color.FgBlack},
FirstColumn: nil,
Footer: text.Colors{color.BgCyan, color.FgBlack},
Header: text.Colors{color.BgHiCyan, color.FgBlack},
Row: text.Colors{color.BgHiWhite, color.FgBlack},
RowAlternate: text.Colors{color.BgWhite, color.FgBlack},
IndexColumn: text.Colors{text.BgHiCyan, text.FgBlack},
Footer: text.Colors{text.BgCyan, text.FgBlack},
Header: text.Colors{text.BgHiCyan, text.FgBlack},
Row: text.Colors{text.BgHiWhite, text.FgBlack},
RowAlternate: text.Colors{text.BgWhite, text.FgBlack},
}

// ColorOptionsDark defines ANSI color options to render bright text on dark
// background.
ColorOptionsDark = ColorOptions{
AutoIndexColumn: text.Colors{color.FgHiCyan, color.BgBlack},
FirstColumn: nil,
Footer: text.Colors{color.FgCyan, color.BgBlack},
Header: text.Colors{color.FgHiCyan, color.BgBlack},
Row: text.Colors{color.FgHiWhite, color.BgBlack},
RowAlternate: text.Colors{color.FgWhite, color.BgBlack},
IndexColumn: text.Colors{text.FgHiCyan, text.BgHiBlack},
Footer: text.Colors{text.FgCyan, text.BgHiBlack},
Header: text.Colors{text.FgHiCyan, text.BgHiBlack},
Row: text.Colors{text.FgHiWhite, text.BgBlack},
RowAlternate: text.Colors{text.FgHiBlack, text.BgBlack},
}
)

Expand Down
12 changes: 12 additions & 0 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Table struct {
columnIsNonNumeric []bool
// htmlCSSClass stores the HTML CSS Class to use on the <table> node
htmlCSSClass string
// indexColumn stores the number of the column considered as the "index"
indexColumn int
// maxColumnLengths stores the length of the longest line in each column
maxColumnLengths []int
// maxRowLength stores the length of the longest row
Expand Down Expand Up @@ -163,6 +165,12 @@ func (t *Table) SetHTMLCSSClass(cssClass string) {
t.htmlCSSClass = cssClass
}

// SetIndexColumn sets the given Column # as the column that has the row
// "Index". Valid values range from 1 to N. Note that this is not 0-indexed.
func (t *Table) SetIndexColumn(colNum int) {
t.indexColumn = colNum
}

// SetOutputMirror sets an io.Writer for all the Render functions to "Write" to
// in addition to returning a string.
func (t *Table) SetOutputMirror(mirror io.Writer) {
Expand Down Expand Up @@ -208,6 +216,10 @@ type renderHint struct {
isSeparatorRow bool
}

func (h *renderHint) isRegularRow() bool {
return !h.isHeaderRow && !h.isFooterRow
}

func (t *Table) analyzeAndStringify(row Row, isHeader bool, isFooter bool) rowStr {
// update t.numColumns if this row is the longest seen till now
if len(row) > t.numColumns {
Expand Down
13 changes: 6 additions & 7 deletions table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import (
"testing"
"unicode/utf8"

"github.com/fatih/color"
"github.com/jedib0t/go-pretty/text"
"github.com/stretchr/testify/assert"
)

var (
testAlign = []text.Align{text.AlignDefault, text.AlignLeft, text.AlignLeft, text.AlignRight}
testCaption = "test-caption"
testColor = text.Colors{color.FgGreen}
testColorBoW = text.Colors{color.FgBlack, color.BgWhite}
testColorHiRedBold = text.Colors{color.FgHiRed, color.Bold}
testColorHiBlueBold = text.Colors{color.FgHiBlue, color.Bold}
testColorWoB = text.Colors{color.FgWhite, color.BgBlack}
testColors = []text.Colors{testColor, testColor, testColor, testColor, {color.FgCyan}}
testColor = text.Colors{text.FgGreen}
testColorBoW = text.Colors{text.FgBlack, text.BgWhite}
testColorHiRedBold = text.Colors{text.FgHiRed, text.Bold}
testColorHiBlueBold = text.Colors{text.FgHiBlue, text.Bold}
testColorWoB = text.Colors{text.FgWhite, text.BgBlack}
testColors = []text.Colors{testColor, testColor, testColor, testColor, {text.FgCyan}}
testColorsFooter = []text.Colors{{}, {}, testColorHiBlueBold, testColorHiBlueBold}
testColorsHeader = []text.Colors{testColorHiRedBold, testColorHiRedBold, testColorHiRedBold, testColorHiRedBold}
testCSSClass = "test-css-class"
Expand Down
1 change: 1 addition & 0 deletions table/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Writer interface {
SetColorsFooter(colors []text.Colors)
SetColorsHeader(colors []text.Colors)
SetHTMLCSSClass(cssClass string)
SetIndexColumn(colNum int)
SetOutputMirror(mirror io.Writer)
SetStyle(style Style)
SetVAlign(vAlign []text.VAlign)
Expand Down
Loading

0 comments on commit e026101

Please sign in to comment.