Skip to content

Commit

Permalink
Merge branch 'master' into center
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn authored Apr 9, 2019
2 parents ab81997 + f82d313 commit 52c3d1a
Show file tree
Hide file tree
Showing 11 changed files with 654 additions and 293 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ go:
- 1.6
- 1.7
- 1.8
- 1.9
- "1.10"
- tip
2 changes: 1 addition & 1 deletion LICENCE.md → LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE.
76 changes: 52 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
ASCII Table Writer
=========

[![Build Status](https://travis-ci.org/olekukonko/tablewriter.png?branch=master)](https://travis-ci.org/olekukonko/tablewriter) [![Total views](https://img.shields.io/sourcegraph/rrc/github.com/olekukonko/tablewriter.svg)](https://sourcegraph.com/github.com/olekukonko/tablewriter)
[![Build Status](https://travis-ci.org/olekukonko/tablewriter.png?branch=master)](https://travis-ci.org/olekukonko/tablewriter)
[![Total views](https://img.shields.io/sourcegraph/rrc/github.com/olekukonko/tablewriter.svg)](https://sourcegraph.com/github.com/olekukonko/tablewriter)
[![Godoc](https://godoc.org/github.com/olekukonko/tablewriter?status.svg)](https://godoc.org/github.com/olekukonko/tablewriter)

Generate ASCII table on the fly ... Installation is simple as

go get github.com/olekukonko/tablewriter
go get github.com/olekukonko/tablewriter


#### Features
Expand All @@ -23,7 +25,7 @@ Generate ASCII table on the fly ... Installation is simple as
- Set custom footer support
- Optional identical cells merging
- Set custom caption

- Optional reflowing of paragrpahs in multi-line cells.

#### Example 1 - Basic
```go
Expand Down Expand Up @@ -90,7 +92,7 @@ table.Render()

#### Example 3 - CSV
```go
table, _ := tablewriter.NewCSV(os.Stdout, "test_info.csv", true)
table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test_info.csv", true)
table.SetAlignment(tablewriter.ALIGN_LEFT) // Set Alignment
table.Render()
```
Expand All @@ -108,12 +110,12 @@ table.Render()

#### Example 4 - Custom Separator
```go
table, _ := tablewriter.NewCSV(os.Stdout, "test.csv", true)
table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test.csv", true)
table.SetRowLine(true) // Enable row line

// Change table lines
table.SetCenterSeparator("*")
table.SetColumnSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("-")

table.SetAlignment(tablewriter.ALIGN_LEFT)
Expand Down Expand Up @@ -199,30 +201,30 @@ table.Render()
#### Table with color
```go
data := [][]string{
[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
[]string{"1/1/2014", "Domain name", "2233", "$10.98"},
[]string{"1/1/2014", "January Hosting", "2233", "$54.95"},
[]string{"1/4/2014", "February Hosting", "2233", "$51.00"},
[]string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer
table.SetBorder(false) // Set Border to false

table.SetHeaderAttributes(tablewriter.Add(tablewriter.Bold, tablewriter.BgGreenColor),
tablewriter.Add(tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor),
tablewriter.Add(tablewriter.BgRedColor, tablewriter.FgWhiteColor),
tablewriter.Add(tablewriter.BgCyanColor, tablewriter.FgWhiteColor))
table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor},
tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor},
tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor},
tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor})

table.SetColumnAttributes(tablewriter.Add(tablewriter.Bold, tablewriter.FgHiBlackColor),
tablewriter.Add(tablewriter.Bold, tablewriter.FgHiRedColor),
tablewriter.Add(tablewriter.Bold, tablewriter.FgHiBlackColor),
tablewriter.Add(tablewriter.Bold, tablewriter.FgBlackColor))
table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor},
tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor})

table.SetFooterAttributes(tablewriter.Add(), tablewriter.Add(),
tablewriter.Add(tablewriter.Bold),
tablewriter.Add(tablewriter.FgHiRedColor))
table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{},
tablewriter.Colors{tablewriter.Bold},
tablewriter.Colors{tablewriter.FgHiRedColor})

table.AppendBulk(data)
table.Render()
Expand Down Expand Up @@ -265,13 +267,39 @@ Note: Caption text will wrap with total width of rendered table.
Movie ratings.
```

#### Render table into a string

Instead of rendering the table to `io.Stdout` you can also render it into a string. Go 1.10 introduced the `strings.Builder` type which implements the `io.Writer` interface and can therefore be used for this task. Example:

```go
package main

import (
"strings"
"fmt"

"github.com/olekukonko/tablewriter"
)

func main() {
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)

/*
* Code to fill the table
*/

table.Render()

fmt.Println(tableString.String())
}
```

#### TODO
- ~~Import Directly from CSV~~ - `done`
- ~~Support for `SetFooter`~~ - `done`
- ~~Support for `SetBorder`~~ - `done`
- ~~Support table with uneven rows~~ - `done`
- Support custom alignment
- ~~Support custom alignment~~
- General Improvement & Optimisation
- `NewHTML` Parse table from HTML


Loading

0 comments on commit 52c3d1a

Please sign in to comment.