Last active
December 1, 2023 03:20
-
-
Save danopia/4b62f8190458ab1f63b85b6ccc0162fd to your computer and use it in GitHub Desktop.
examples of adding a title to olekukonko/tablewriter in post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"github.com/google/uuid" | |
"github.com/olekukonko/tablewriter" | |
) | |
func main() { | |
tableString := &strings.Builder{} | |
table := tablewriter.NewWriter(tableString) | |
tableTitle := "" | |
tableTitle = "Some of your data" | |
table.SetHeader([]string{"ID", "Name", "Status"}) | |
table.Append([]string{uuid.New().String(), "qwerty", "Pending"}) | |
table.Append([]string{uuid.New().String(), "asdf", "Done"}) | |
table.Append([]string{uuid.New().String(), "zxcvzxcvzxcv", "Done"}) | |
table.Append([]string{uuid.New().String(), "111! 222!! 333!!!", "Done"}) | |
table.Render() | |
fmt.Println() | |
if tableTitle != "" { | |
lineLen := strings.Index(tableString.String(), "\n") | |
padding := (lineLen - len(tableTitle) - 8) | |
if padding < 0 { | |
padding = 0 | |
} | |
leftPadWidth := (padding / 2) | |
leftPad := strings.Repeat(" ", leftPadWidth) | |
rightPad := strings.Repeat(" ", padding-leftPadWidth) | |
fmt.Printf("%s*** %s ***%s\n", leftPad, tableTitle, rightPad) | |
} | |
fmt.Println(tableString.String()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"strings" | |
"github.com/google/uuid" | |
"github.com/olekukonko/tablewriter" | |
) | |
func main() { | |
var noHeadDivider bool | |
flag.BoolVar(&noHeadDivider, "no-head-divider", false, "") | |
flag.Parse() | |
tableString := &strings.Builder{} | |
table := tablewriter.NewWriter(tableString) | |
table.SetBorders(tablewriter.Border{true, true, !noHeadDivider, true}) | |
tableTitle := "Some of your data" | |
table.SetHeader([]string{"ID", "Name", "Status"}) | |
table.Append([]string{uuid.New().String(), "qwerty", "Pending"}) | |
table.Append([]string{uuid.New().String(), "asdf", "Done"}) | |
table.Append([]string{uuid.New().String(), "zxcvzxcvzxcv", "Done"}) | |
table.Append([]string{uuid.New().String(), "111! 222!! 333!!!", "Done"}) | |
table.Render() | |
if tableTitle != "" { | |
lineLen := strings.Index(tableString.String(), "\n") | |
padding := (lineLen - len(tableTitle) - 4) | |
if padding < 0 { | |
padding = 0 | |
} | |
firstPad := (padding / 2) | |
fmt.Println("+" + strings.Repeat("-", lineLen-2) + "+") | |
fmt.Println("|"+strings.Repeat(" ", firstPad), tableTitle, strings.Repeat(" ", padding-firstPad)+"|") | |
if noHeadDivider { | |
fmt.Println("|" + strings.Repeat(" ", lineLen-2) + "|") | |
} | |
} | |
fmt.Println(tableString.String()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/p/v/f/k/z/T/tmp.T5i1kAZa $ go run 1_floating.go | |
*** Some of your data *** | |
+--------------------------------------+-------------------+---------+ | |
| ID | NAME | STATUS | | |
+--------------------------------------+-------------------+---------+ | |
| 60f1401f-e75f-4e8a-a776-a2e48a2d5c4b | qwerty | Pending | | |
| 6efeded4-966f-4b38-87a5-7f14cee1ba92 | asdf | Done | | |
| ceda1bb4-a91d-4f36-b71e-51f5c420e7a3 | zxcvzxcvzxcv | Done | | |
| 18e9cf3a-c7b9-4449-9464-e9e8312f8270 | 111! 222!! 333!!! | Done | | |
+--------------------------------------+-------------------+---------+ | |
/p/v/f/k/z/T/tmp.T5i1kAZa $ go run 2_enclosed.go | |
+--------------------------------------------------------------------+ | |
| Some of your data | | |
+--------------------------------------+-------------------+---------+ | |
| ID | NAME | STATUS | | |
+--------------------------------------+-------------------+---------+ | |
| 4735ebe9-d5aa-44ba-8ff7-a1a33988858a | qwerty | Pending | | |
| 4c480d42-fb72-486e-bd39-368f81edbd67 | asdf | Done | | |
| ef14c3b0-502e-4b86-9e84-38ffb8f589e6 | zxcvzxcvzxcv | Done | | |
| 6170748e-4819-4f58-b0cc-0e6c09aa215a | 111! 222!! 333!!! | Done | | |
+--------------------------------------+-------------------+---------+ | |
/p/v/f/k/z/T/tmp.T5i1kAZa $ go run 2_enclosed.go --no-head-divider | |
+--------------------------------------------------------------------+ | |
| Some of your data | | |
| | | |
| ID | NAME | STATUS | | |
+--------------------------------------+-------------------+---------+ | |
| 757a024f-0668-4c6f-a4ec-c28f616b0ada | qwerty | Pending | | |
| 34a860a9-7397-4a29-871d-b020ba3c7bc7 | asdf | Done | | |
| 6fb826ad-4d86-4c00-ad0d-a80dada76ec1 | zxcvzxcvzxcv | Done | | |
| 46719d88-6333-4fb8-8b94-ac65448f1798 | 111! 222!! 333!!! | Done | | |
+--------------------------------------+-------------------+---------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment