Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fbiville/markdown-table-formatter
Markdown table formatter in Go
https://github.com/fbiville/markdown-table-formatter
go markdown
Last synced: 3 months ago
JSON representation
Markdown table formatter in Go
- Host: GitHub
- URL: https://github.com/fbiville/markdown-table-formatter
- Owner: fbiville
- License: apache-2.0
- Created: 2019-08-19T16:26:53.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-12-16T09:36:11.000Z (about 3 years ago)
- Last Synced: 2024-11-02T08:41:46.870Z (4 months ago)
- Topics: go, markdown
- Language: Go
- Homepage:
- Size: 21.5 KB
- Stars: 13
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Markdown table formatter
## Import
```sh
$ go get github.com/fbiville/markdown-table-formatter/pkg/markdown
```## Usage
### Basic
```go
package some_packageimport (
"fmt"
"github.com/fbiville/markdown-table-formatter/pkg/markdown"
)func someFunction() {
basicTable, err := markdown.NewTableFormatterBuilder().
Build("column 1", "column 2", "column 3").
Format([][]string{
{"so", "much", "fun"},
{"don't", "you", "agree"},
})
if err != nil {
// ... do your thing
}
fmt.Print(basicTable)
}
```The output will be:
```markdown
| column 1 | column 2 | column 3 |
| -------- | -------- | -------- |
| so | much | fun |
| don't | you | agree |
```### Pretty-print
```go
package some_packageimport (
"fmt"
"github.com/fbiville/markdown-table-formatter/pkg/markdown"
)func someFunction() {
prettyPrintedTable, err := markdown.NewTableFormatterBuilder().
WithPrettyPrint().
Build("column 1", "column 2", "column 3").
Format([][]string{
{"so", "much", "fun"},
{"don't", "you", "agree"},
})
if err != nil {
// ... do your thing
}
fmt.Print(prettyPrintedTable)
}
```The output will be:
```markdown
| column 1 | column 2 | column 3 |
| -------- | -------- | -------- |
| so | much | fun |
| don't | you | agree |
```### Sort
Sorting by columns is disabled by default.
It is available to all formatters.#### Alphabetical order
When using `markdown.ASCENDING_ORDER` or `markdown.DESCENDING_ORDER`, the associated string comparison algorithm
is applied to **every** column, in column order.##### Ascending order
```go
table, err := markdown.NewTableFormatterBuilder().
WithAlphabeticalSortIn(markdown.ASCENDING_ORDER).
Build("column 1", "column 2", "column 3").
Format([][]string{
{"don't", "you", "know"},
{"so", "much", "fun"},
{"don't", "you", "agree"},
})if err != nil {
// ... do your thing
}
fmt.Print(table)
```The output will be:
```markdown
| column 1 | column 2 | column 3 |
| -------- | -------- | -------- |
| don't | you | agree |
| don't | you | know |
| so | much | fun |
```##### Descending order
```go
table, err := markdown.NewTableFormatterBuilder().
WithAlphabeticalSortIn(markdown.DESCENDING_ORDER).
// ... same as above ...
fmt.Print(basicTable)
```The output will be:
```markdown
| column 1 | column 2 | column 3 |
| -------- | -------- | -------- |
| so | much | fun |
| don't | you | know |
| don't | you | agree |
```#### Custom sort functions
There can be at most `N` custom sort functions (where `N` is the number of columns).
If the array is made of 3 columns e.g.,
```go
WithAlphabeticalSortIn(markdown.ASCENDING_ORDER)
```is effectively equivalent to:
```go
WithCustomSort(markdown.ASCENDING_ORDER.StringCompare(0),
markdown.ASCENDING_ORDER.StringCompare(1),
markdown.ASCENDING_ORDER.StringCompare(2))
```This is **not** the same as:
```go
WithCustomSort(markdown.ASCENDING_ORDER.StringCompare(0))
```Indeed, providing a single function triggers a comparison between values of the specified column, the other columns' values
are not compared.In other words, the following program:
```go
table, err := markdown.NewTableFormatterBuilder().
WithCustomSort(markdown.ASCENDING_ORDER.StringCompare(0)).
// equivalent to:
// WithCustomSort(
// SortFunction{
// Fn: func(a, b string) int {
// // you can plug *any* logic here
// return strings.Compare(a, b)
// },
// Column: 0,
// })
WithPrettyPrint().
Build("column 1", "column 2", "column 3").
Format([][]string{
{"don't", "you", "know"},
{"so", "much", "fun"},
{"don't", "you", "agree"},
})if err != nil {
// ... do your thing
}fmt.Print(table)
```... will yield:
```markdown
| column 1 | column 2 | column 3 |
| -------- | -------- | -------- |
| don't | you | know |
| don't | you | agree |
| so | much | fun |
```The order of custom sort function is the order of **execution**.
You are free to sort columns by any order:```go
table, err := markdown.NewTableFormatterBuilder().
// sort third column first, then first column
WithCustomSort(markdown.ASCENDING_ORDER.StringCompare(2), markdown.DESCENDING_ORDER.StringCompare(0)).
WithPrettyPrint().
Build("column 1", "column 2", "column 3").
Format([][]string{
{"don't", "you", "know"},
{"so", "much", "agree"},
{"don't", "you", "agree"},
})if err != nil {
// ... do your thing
}fmt.Print(table)
```... will yield:
```markdown
| column 1 | column 2 | column 3 |
| -------- | -------- | -------- |
| so | much | agree |
| don't | you | agree |
| don't | you | know |
```