Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
luo6 authored and luo6 committed Aug 5, 2022
1 parent 5f31c45 commit 5fd6d50
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,51 @@
```shell
go get github.com/ICU-Coders/table
```
```go
import "github.com/ICU-Coders/table"

table.Show([]string{"Module", "Type", "Path", "Author"}, [][]string{
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
})
```

![example](./example.png)

### Config
```go
var MaxCellWidth = 40
var LineEndTag = "*"
var LineBody = "-"
var LineDivider = "|"
```
Example
```go
func TestShow(t *testing.T) {
LineEndTag = "*"
LineBody = "="
LineDivider = "/"
Show([]string{"Module", "Type", "Path", "Author"}, [][]string{
{"1111", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
})
}
```
Display
```
*========*======*======*========*
/ Module / Type / Path / Author /
*========*======*======*========*
/ 1111 / 2 / 3 / 4 /
/ 1 / 2 / 3 / 4 /
/ 1 / 2 / 3 / 4 /
/ 1 / 2 / 3 / 4 /
*========*======*======*========*
```
## MIT License

Copyright (c) 2022 ICU-Coders
Expand Down
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 16 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import (
)

var MaxCellWidth = 40
var LineEndTag = "*"
var LineBody = "-"
var LineDivider = "|"

func trimTableCell(headers []string, content [][]string) {
for i, header := range headers {
Expand Down Expand Up @@ -75,32 +78,37 @@ func tableBorder(maxWidths []int) string {
func row(row []string, maxWidth []int) string {
maxLine := 0
for i, cell := range row {
if i >= len(maxWidth) {
break
}
lines := howManyLine(cell, maxWidth[i])
if lines > maxLine {
maxLine = lines
}
}
bufferString := bytes.NewBufferString("")
for i := 0; i < maxLine; i++ {
bufferString.WriteString("|")
bufferString.WriteString(LineDivider)
for j, s := range row {
// fixme 检查row和maxWidth长度
bufferString.WriteString(" ")
bufferString.WriteString(fixStr(s, i, maxWidth[j]))
bufferString.WriteString(" |")
if j < len(maxWidth) {
bufferString.WriteString(" ")
bufferString.WriteString(fixStr(s, i, maxWidth[j]))
bufferString.WriteString(" ")
bufferString.WriteString(LineDivider)
}
}
bufferString.WriteString("\n")
}
return bufferString.String()
}

func borderWithEnd(length int, end bool) string {
bufferString := bytes.NewBufferString("+")
bufferString := bytes.NewBufferString(LineEndTag)
for i := 0; i < length; i++ {
bufferString.WriteString("-")
bufferString.WriteString(LineBody)
}
if end {
bufferString.WriteString("+")
bufferString.WriteString(LineEndTag)
}
return bufferString.String()
}
Expand Down
15 changes: 15 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package table

import "testing"

func TestShow(t *testing.T) {
LineEndTag = "*"
LineBody = "="
LineDivider = "/"
Show([]string{"Module", "Type", "Path", "Author"}, [][]string{
{"1111", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
{"1", "2", "3", "4"},
})
}

0 comments on commit 5fd6d50

Please sign in to comment.