https://github.com/daniel-zimmer/pterm
Go package for printing structs as tables in the terminal.
https://github.com/daniel-zimmer/pterm
go golang printing teminal
Last synced: 5 months ago
JSON representation
Go package for printing structs as tables in the terminal.
- Host: GitHub
- URL: https://github.com/daniel-zimmer/pterm
- Owner: daniel-zimmer
- License: mit
- Created: 2020-12-04T04:23:28.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-04T05:00:39.000Z (over 5 years ago)
- Last Synced: 2024-06-21T09:58:23.176Z (almost 2 years ago)
- Topics: go, golang, printing, teminal
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# pterm
Go package for printing structs as tables in the terminal.
---
pterm uses reflection to get struct field names and tags to define formatting behaviour.
## Example
```go
package main
import (
"fmt"
"github.com/Daniel-Zimmer/pterm"
"math/rand"
"time"
)
type Example struct {
ID string `json:"id" pterm:"2, right"`
Name string `json:"name" pterm:"2, elastic"`
Size string `json:"size" pterm:"left"`
}
func main() {
sizes := []string{"BIG", "MEDIUM", "SMALL", "TINY"}
names := []string{"Albert", "John", "Richard", "Mary", "Cleo", "Bernard", "Zimmer"}
examples := make([]Example, 0)
rand.Seed(time.Now().Unix())
for i := 0; i < 15; i++ {
examples = append(examples, Example{
ID: fmt.Sprintf("%x", rand.Int()),
Name: names[rand.Intn(len(names))],
Size: sizes[rand.Intn(len(sizes))],
},
)
}
pterm.PrintTable(examples)
}
```
## Tags
### "left"
Aligns to the **left**.
### "right"
Aligns to the **right**.
### any number
How much **padding** to leave before the next column.
Example: "2"
### "ignore"
**Ignores** the field.
### "elastic"
Trims the strings in this column that would not fit in the terminal.
Only one field can be elastic.
(99% sure this does not work on Windows)
Example: if one of the entries in the table is: **"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."** and the terminal is not wide enough to fit the entire string, the displayed value will be something like: **"Lorem ipsum dolor sit..."**.
### any name:
**Name** to use as **column header** instead of the name of the struct field.
If you want to use any of the above keywords as struct names just add a "$" in front of the name.
Example: "$left"
If you want the column header to start with a "$" just use "$$" instead.