mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 12:31:06 +01:00
no need to return string when using io.Writer anyway
This commit is contained in:
@@ -29,7 +29,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func printData(w io.Writer, data *Tabdata) string {
|
func printData(w io.Writer, data *Tabdata) {
|
||||||
// some output preparations:
|
// some output preparations:
|
||||||
|
|
||||||
// add numbers to headers and remove this we're not interested in
|
// add numbers to headers and remove this we're not interested in
|
||||||
@@ -43,31 +43,30 @@ func printData(w io.Writer, data *Tabdata) string {
|
|||||||
|
|
||||||
switch OutputMode {
|
switch OutputMode {
|
||||||
case "extended":
|
case "extended":
|
||||||
return printExtendedData(w, data)
|
printExtendedData(w, data)
|
||||||
case "ascii":
|
case "ascii":
|
||||||
return printAsciiData(w, data)
|
printAsciiData(w, data)
|
||||||
case "orgtbl":
|
case "orgtbl":
|
||||||
return printOrgmodeData(w, data)
|
printOrgmodeData(w, data)
|
||||||
case "markdown":
|
case "markdown":
|
||||||
return printMarkdownData(w, data)
|
printMarkdownData(w, data)
|
||||||
case "shell":
|
case "shell":
|
||||||
return printShellData(w, data)
|
printShellData(w, data)
|
||||||
case "yaml":
|
case "yaml":
|
||||||
return printYamlData(w, data)
|
printYamlData(w, data)
|
||||||
default:
|
default:
|
||||||
return printAsciiData(w, data)
|
printAsciiData(w, data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func output(w io.Writer, str string) string {
|
func output(w io.Writer, str string) {
|
||||||
fmt.Fprint(w, str)
|
fmt.Fprint(w, str)
|
||||||
return str
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Emacs org-mode compatible table (also orgtbl-mode)
|
Emacs org-mode compatible table (also orgtbl-mode)
|
||||||
*/
|
*/
|
||||||
func printOrgmodeData(w io.Writer, data *Tabdata) string {
|
func printOrgmodeData(w io.Writer, data *Tabdata) {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
@@ -93,7 +92,7 @@ func printOrgmodeData(w io.Writer, data *Tabdata) string {
|
|||||||
leftR := regexp.MustCompile("(?m)^\\+")
|
leftR := regexp.MustCompile("(?m)^\\+")
|
||||||
rightR := regexp.MustCompile("\\+(?m)$")
|
rightR := regexp.MustCompile("\\+(?m)$")
|
||||||
|
|
||||||
return output(w, color.Sprint(
|
output(w, color.Sprint(
|
||||||
colorizeData(
|
colorizeData(
|
||||||
rightR.ReplaceAllString(
|
rightR.ReplaceAllString(
|
||||||
leftR.ReplaceAllString(tableString.String(), "|"), "|"))))
|
leftR.ReplaceAllString(tableString.String(), "|"), "|"))))
|
||||||
@@ -102,7 +101,7 @@ func printOrgmodeData(w io.Writer, data *Tabdata) string {
|
|||||||
/*
|
/*
|
||||||
Markdown table
|
Markdown table
|
||||||
*/
|
*/
|
||||||
func printMarkdownData(w io.Writer, data *Tabdata) string {
|
func printMarkdownData(w io.Writer, data *Tabdata) {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
@@ -116,13 +115,13 @@ func printMarkdownData(w io.Writer, data *Tabdata) string {
|
|||||||
table.SetCenterSeparator("|")
|
table.SetCenterSeparator("|")
|
||||||
|
|
||||||
table.Render()
|
table.Render()
|
||||||
return output(w, color.Sprint(colorizeData(tableString.String())))
|
output(w, color.Sprint(colorizeData(tableString.String())))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Simple ASCII table without any borders etc, just like the input we expect
|
Simple ASCII table without any borders etc, just like the input we expect
|
||||||
*/
|
*/
|
||||||
func printAsciiData(w io.Writer, data *Tabdata) string {
|
func printAsciiData(w io.Writer, data *Tabdata) {
|
||||||
tableString := &strings.Builder{}
|
tableString := &strings.Builder{}
|
||||||
table := tablewriter.NewWriter(tableString)
|
table := tablewriter.NewWriter(tableString)
|
||||||
|
|
||||||
@@ -146,13 +145,13 @@ func printAsciiData(w io.Writer, data *Tabdata) string {
|
|||||||
table.SetNoWhiteSpace(true)
|
table.SetNoWhiteSpace(true)
|
||||||
|
|
||||||
table.Render()
|
table.Render()
|
||||||
return output(w, color.Sprint(colorizeData(tableString.String())))
|
output(w, color.Sprint(colorizeData(tableString.String())))
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
We simulate the \x command of psql (the PostgreSQL client)
|
We simulate the \x command of psql (the PostgreSQL client)
|
||||||
*/
|
*/
|
||||||
func printExtendedData(w io.Writer, data *Tabdata) string {
|
func printExtendedData(w io.Writer, data *Tabdata) {
|
||||||
// needed for data output
|
// needed for data output
|
||||||
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader)
|
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader)
|
||||||
out := ""
|
out := ""
|
||||||
@@ -166,13 +165,13 @@ func printExtendedData(w io.Writer, data *Tabdata) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return output(w, out)
|
output(w, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
|
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
|
||||||
*/
|
*/
|
||||||
func printShellData(w io.Writer, data *Tabdata) string {
|
func printShellData(w io.Writer, data *Tabdata) {
|
||||||
out := ""
|
out := ""
|
||||||
if len(data.entries) > 0 {
|
if len(data.entries) > 0 {
|
||||||
for _, entry := range data.entries {
|
for _, entry := range data.entries {
|
||||||
@@ -184,10 +183,10 @@ func printShellData(w io.Writer, data *Tabdata) string {
|
|||||||
out += fmt.Sprint(strings.Join(shentries, " ")) + "\n"
|
out += fmt.Sprint(strings.Join(shentries, " ")) + "\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output(w, out)
|
output(w, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printYamlData(w io.Writer, data *Tabdata) string {
|
func printYamlData(w io.Writer, data *Tabdata) {
|
||||||
type D struct {
|
type D struct {
|
||||||
Entries []map[string]interface{} `yaml:"entries"`
|
Entries []map[string]interface{} `yaml:"entries"`
|
||||||
}
|
}
|
||||||
@@ -220,5 +219,5 @@ func printYamlData(w io.Writer, data *Tabdata) string {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return output(w, string(yamlstr))
|
output(w, string(yamlstr))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -279,11 +279,15 @@ func TestPrinter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
testdata := mockdata
|
testdata := mockdata
|
||||||
output := strings.TrimSpace(printData(&w, &testdata))
|
|
||||||
exp := strings.TrimSpace(tt.expect)
|
exp := strings.TrimSpace(tt.expect)
|
||||||
if output != exp {
|
|
||||||
|
printData(&w, &testdata)
|
||||||
|
|
||||||
|
got := strings.TrimSpace(w.String())
|
||||||
|
|
||||||
|
if got != exp {
|
||||||
t.Errorf("not rendered correctly:\n+++ got:\n%s\n+++ want:\n%s",
|
t.Errorf("not rendered correctly:\n+++ got:\n%s\n+++ want:\n%s",
|
||||||
output, exp)
|
got, exp)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user