Files
tablizer/lib/printer.go

345 lines
7.7 KiB
Go
Raw Normal View History

/*
Copyright © 2022-2025 Thomas von Dein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package lib
2022-09-28 19:30:08 +02:00
import (
"encoding/csv"
2022-09-28 19:30:08 +02:00
"fmt"
"io"
"log"
"strconv"
2022-09-28 19:30:08 +02:00
"strings"
"github.com/gookit/color"
"github.com/olekukonko/tablewriter"
2025-05-27 13:09:18 +02:00
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
"github.com/tlinden/tablizer/cfg"
"gopkg.in/yaml.v3"
2022-09-28 19:30:08 +02:00
)
2024-05-07 15:19:54 +02:00
func printData(writer io.Writer, conf cfg.Config, data *Tabdata) {
// Sort the data first, before headers+entries are being
// reduced. That way the user can specify any valid column to sort
// by, independently if it's being used for display or not.
sortTable(conf, data)
2025-01-20 19:28:19 +01:00
// put one or more columns into clipboard
yankColumns(conf, data)
// add numbers to headers and remove those we're not interested in
2024-05-07 15:19:54 +02:00
numberizeAndReduceHeaders(conf, data)
2022-10-13 18:56:34 +02:00
// remove unwanted columns, if any
2024-05-07 15:19:54 +02:00
reduceColumns(conf, data)
2022-09-28 19:30:08 +02:00
2024-05-07 15:19:54 +02:00
switch conf.OutputMode {
case cfg.Extended:
2024-05-07 15:19:54 +02:00
printExtendedData(writer, conf, data)
case cfg.ASCII:
2024-05-07 18:01:12 +02:00
printASCIIData(writer, conf, data)
case cfg.Orgtbl:
2024-05-07 15:19:54 +02:00
printOrgmodeData(writer, conf, data)
case cfg.Markdown:
2024-05-07 15:19:54 +02:00
printMarkdownData(writer, conf, data)
case cfg.Shell:
2024-05-07 15:19:54 +02:00
printShellData(writer, data)
case cfg.Yaml:
2024-05-07 15:19:54 +02:00
printYamlData(writer, data)
case cfg.CSV:
2024-05-07 15:19:54 +02:00
printCSVData(writer, data)
default:
2024-05-07 18:01:12 +02:00
printASCIIData(writer, conf, data)
}
}
2022-09-28 19:30:08 +02:00
2024-05-07 15:19:54 +02:00
func output(writer io.Writer, str string) {
fmt.Fprint(writer, str)
}
/*
Emacs org-mode compatible table (also orgtbl-mode)
*/
2024-05-07 15:19:54 +02:00
func printOrgmodeData(writer io.Writer, conf cfg.Config, data *Tabdata) {
tableString := &strings.Builder{}
2025-05-27 13:09:18 +02:00
table := tablewriter.NewTable(tableString,
tablewriter.WithRenderer(
renderer.NewBlueprint(
tw.Rendition{
Borders: tw.Border{
Left: tw.On,
Right: tw.On,
Top: tw.On,
Bottom: tw.On,
},
Settings: tw.Settings{
Separators: tw.Separators{
ShowHeader: tw.On,
ShowFooter: tw.Off,
BetweenRows: tw.Off,
BetweenColumns: 0,
},
},
Symbols: tw.NewSymbols(tw.StyleASCII),
})),
tablewriter.WithConfig(
tablewriter.Config{
Header: tw.CellConfig{
Formatting: tw.CellFormatting{
Alignment: tw.AlignLeft,
AutoFormat: tw.Off,
},
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
Alignment: tw.AlignLeft,
},
},
},
),
)
2022-09-28 19:30:08 +02:00
2024-05-07 15:19:54 +02:00
if !conf.NoHeaders {
2025-05-27 13:09:18 +02:00
table.Header(data.headers)
}
if err := table.Bulk(data.entries); err != nil {
log.Fatalf("Failed to add data to table renderer: %s", err)
}
2025-05-27 13:09:18 +02:00
if err := table.Render(); err != nil {
log.Fatalf("Failed to render table: %s", err)
}
2025-05-27 13:09:18 +02:00
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
}
/*
Markdown table
*/
2024-05-07 15:19:54 +02:00
func printMarkdownData(writer io.Writer, conf cfg.Config, data *Tabdata) {
2022-10-10 20:14:51 +02:00
tableString := &strings.Builder{}
2025-05-27 13:09:18 +02:00
table := tablewriter.NewTable(tableString,
tablewriter.WithRenderer(
renderer.NewBlueprint(
tw.Rendition{
Borders: tw.Border{
Left: tw.On,
Right: tw.On,
Top: tw.Off,
Bottom: tw.Off,
},
Settings: tw.Settings{
Separators: tw.Separators{
ShowHeader: tw.On,
ShowFooter: tw.Off,
BetweenRows: tw.Off,
BetweenColumns: 0,
},
},
Symbols: tw.NewSymbols(tw.StyleMarkdown),
})),
tablewriter.WithConfig(
tablewriter.Config{
Header: tw.CellConfig{
Formatting: tw.CellFormatting{
Alignment: tw.AlignLeft,
AutoFormat: tw.Off,
},
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
Alignment: tw.AlignLeft,
},
},
},
),
)
2024-05-07 15:19:54 +02:00
if !conf.NoHeaders {
2025-05-27 13:09:18 +02:00
table.Header(data.headers)
}
2025-05-27 13:09:18 +02:00
if err := table.Bulk(data.entries); err != nil {
log.Fatalf("Failed to add data to table renderer: %s", err)
2022-09-28 19:30:08 +02:00
}
2025-05-27 13:09:18 +02:00
if err := table.Render(); err != nil {
log.Fatalf("Failed to render table: %s", err)
}
2024-05-07 15:19:54 +02:00
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
}
/*
Simple ASCII table without any borders etc, just like the input we expect
*/
2024-05-07 18:01:12 +02:00
func printASCIIData(writer io.Writer, conf cfg.Config, data *Tabdata) {
2022-10-10 20:14:51 +02:00
tableString := &strings.Builder{}
2025-05-27 13:09:18 +02:00
styleTSV := tw.NewSymbolCustom("space").WithColumn("\t")
2025-05-27 13:09:18 +02:00
table := tablewriter.NewTable(tableString,
tablewriter.WithRenderer(
renderer.NewBlueprint(tw.Rendition{
Borders: tw.BorderNone,
Symbols: styleTSV,
2025-05-27 13:09:18 +02:00
Settings: tw.Settings{
Separators: tw.SeparatorsNone,
Lines: tw.LinesNone,
2025-05-27 13:09:18 +02:00
},
})),
tablewriter.WithConfig(tablewriter.Config{
Header: tw.CellConfig{
Formatting: tw.CellFormatting{
AutoFormat: tw.Off,
},
Padding: tw.CellPadding{Global: tw.Padding{Left: "", Right: " "}},
2025-05-27 13:09:18 +02:00
},
Row: tw.CellConfig{
Formatting: tw.CellFormatting{
AutoWrap: tw.WrapNone,
Alignment: tw.AlignLeft,
},
Padding: tw.CellPadding{Global: tw.Padding{Right: " "}},
2025-05-27 13:09:18 +02:00
},
Debug: true,
}),
)
2024-05-07 15:19:54 +02:00
if !conf.NoHeaders {
table.Header(data.headers)
2025-05-27 13:09:18 +02:00
}
if err := table.Bulk(data.entries); err != nil {
2025-05-27 13:09:18 +02:00
log.Fatalf("Failed to add data to table renderer: %s", err)
}
2024-05-07 18:01:12 +02:00
2025-05-27 13:09:18 +02:00
if err := table.Render(); err != nil {
log.Fatalf("Failed to render table: %s", err)
}
2024-05-07 15:19:54 +02:00
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
2022-09-28 19:30:08 +02:00
}
/*
We simulate the \x command of psql (the PostgreSQL client)
2022-09-28 19:30:08 +02:00
*/
2024-05-07 15:19:54 +02:00
func printExtendedData(writer io.Writer, conf cfg.Config, data *Tabdata) {
2022-09-28 19:30:08 +02:00
// needed for data output
2022-10-11 09:11:46 +02:00
format := fmt.Sprintf("%%%ds: %%s\n", data.maxwidthHeader)
out := ""
2024-05-07 18:01:12 +02:00
2022-09-28 19:30:08 +02:00
if len(data.entries) > 0 {
for _, entry := range data.entries {
for i, value := range entry {
out += color.Sprintf(format, data.headers[i], value)
2022-09-28 19:30:08 +02:00
}
2022-10-11 09:11:46 +02:00
out += "\n"
2022-09-28 19:30:08 +02:00
}
}
2024-05-07 15:19:54 +02:00
output(writer, colorizeData(conf, out))
2022-09-28 19:30:08 +02:00
}
2022-10-03 13:28:04 +02:00
/*
Shell output, ready to be eval'd. Just like FreeBSD stat(1)
2022-10-03 13:28:04 +02:00
*/
2024-05-07 15:19:54 +02:00
func printShellData(writer io.Writer, data *Tabdata) {
out := ""
2024-05-07 18:01:12 +02:00
2022-10-03 13:28:04 +02:00
if len(data.entries) > 0 {
for _, entry := range data.entries {
shentries := []string{}
2024-05-07 18:01:12 +02:00
2024-05-07 15:19:54 +02:00
for idx, value := range entry {
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"",
2024-05-07 15:19:54 +02:00
data.headers[idx], value))
2022-10-03 13:28:04 +02:00
}
2024-05-07 18:01:12 +02:00
out += strings.Join(shentries, " ") + "\n"
2022-10-03 13:28:04 +02:00
}
}
// no colorization here
2024-05-07 15:19:54 +02:00
output(writer, out)
2022-10-03 13:28:04 +02:00
}
2024-05-07 15:19:54 +02:00
func printYamlData(writer io.Writer, data *Tabdata) {
type Data struct {
Entries []map[string]interface{} `yaml:"entries"`
}
2024-05-07 15:19:54 +02:00
yamlout := Data{}
for _, entry := range data.entries {
2024-05-07 15:19:54 +02:00
yamldata := map[string]interface{}{}
2024-05-07 15:19:54 +02:00
for idx, entry := range entry {
style := yaml.TaggedStyle
2024-05-07 18:01:12 +02:00
_, err := strconv.Atoi(entry)
if err != nil {
style = yaml.DoubleQuotedStyle
}
2024-05-07 15:19:54 +02:00
yamldata[strings.ToLower(data.headers[idx])] =
&yaml.Node{
Kind: yaml.ScalarNode,
Style: style,
Value: entry}
}
2024-05-07 15:19:54 +02:00
yamlout.Entries = append(yamlout.Entries, yamldata)
}
2024-05-07 15:19:54 +02:00
yamlstr, err := yaml.Marshal(&yamlout)
if err != nil {
log.Fatal(err)
}
2024-05-07 15:19:54 +02:00
output(writer, string(yamlstr))
}
2024-05-07 15:19:54 +02:00
func printCSVData(writer io.Writer, data *Tabdata) {
csvout := csv.NewWriter(writer)
if err := csvout.Write(data.headers); err != nil {
log.Fatalln("error writing record to csv:", err)
}
for _, entry := range data.entries {
if err := csvout.Write(entry); err != nil {
log.Fatalln("error writing record to csv:", err)
}
}
csvout.Flush()
if err := csvout.Error(); err != nil {
log.Fatal(err)
}
}