Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for markdown column alignment #181

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Table struct {
columnsParams []string
footerParams []string
columnsAlign []int
mdAlign bool
}

// NewWriter Start New Table
Expand Down Expand Up @@ -140,7 +141,9 @@ func NewWriter(writer io.Writer) *Table {
headerParams: []string{},
columnsParams: []string{},
footerParams: []string{},
columnsAlign: []int{}}
columnsAlign: []int{},
mdAlign: false,
}
return t
}

Expand Down Expand Up @@ -211,6 +214,11 @@ func (t *Table) SetReflowDuringAutoWrap(auto bool) {
t.reflowText = auto
}

// Use markdown alignment characters (":") make markdown rendering match text alignment
func (t *Table) SetMarkdownColumnAlignment(val bool) {
t.mdAlign = val
}

// SetColWidth Set the Default column width
func (t *Table) SetColWidth(width int) {
t.mW = width
Expand Down Expand Up @@ -534,16 +542,41 @@ func (t *Table) center(i int, isFirstRow, isLastRow bool) string {
return t.syms[symNESW]
}

func (t *Table) lineEdges(col int) (left, right string) {
if !t.mdAlign {
return t.syms[symEW], t.syms[symEW]
}
t.fillAlignment(len(t.cs))
const mdAlignChar = ":"
switch t.columnsAlign[col] {
case ALIGN_CENTER:
return mdAlignChar, mdAlignChar
case ALIGN_LEFT:
return mdAlignChar, t.syms[symEW]
case ALIGN_RIGHT:
return t.syms[symEW], mdAlignChar
default:
return t.syms[symEW], t.syms[symEW]
}
}

// Print line based on row width
func (t *Table) printLine(isFirst, isLast bool) {
fmt.Fprint(t.out, t.center(-1, isFirst, isLast))
for i := 0; i < len(t.cs); i++ {
lEdge, rEdge := t.lineEdges(i)
v := t.cs[i]
fmt.Fprintf(t.out, "%s%s%s%s",
t.syms[symEW],
lEdge,
strings.Repeat(t.syms[symEW], v),
t.syms[symEW],
rEdge,
t.center(i, isFirst, isLast))

//fmt.Fprintf(t.out, "%s%s%s%s",
// lEdge,
// strings.Repeat(string(t.pRow), v),
// rEdge,
// t.center(i))
}
fmt.Fprint(t.out, t.newLine)
}
Expand Down
28 changes: 28 additions & 0 deletions table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,34 @@ func TestPrintingInMarkdown(t *testing.T) {
checkEqual(t, buf.String(), want, "border table rendering failed")
}

func TestPrintingInMarkdownWithAlignment(t *testing.T) {
data := [][]string{
{"1/1/2014", "Domain name", "2233", "$10.98"},
{"1/1/2014", "January Hosting", "2233", "$54.95"},
{"1/4/2014", "February Hosting", "2233", "$51.00"},
{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"},
}

var buf bytes.Buffer
table := NewWriter(&buf)
table.SetHeader([]string{"Date", "Description", "CV2", "Amount"})
table.AppendBulk(data) // Add Bulk Data
table.SetBorders(Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.SetColumnAlignment([]int{ALIGN_DEFAULT, ALIGN_LEFT, ALIGN_RIGHT, ALIGN_CENTER})
table.SetMarkdownColumnAlignment(true)
table.Render()

want := `| DATE | DESCRIPTION | CV2 | AMOUNT |
|----------|:-------------------------|-----:|:------:|
| 1/1/2014 | Domain name | 2233 | $10.98 |
| 1/1/2014 | January Hosting | 2233 | $54.95 |
| 1/4/2014 | February Hosting | 2233 | $51.00 |
| 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |
`
checkEqual(t, buf.String(), want, "border table rendering failed")
}

func TestPrintHeading(t *testing.T) {
var buf bytes.Buffer
table := NewWriter(&buf)
Expand Down