2022-09-30 14:08:59 +02:00
|
|
|
/*
|
2025-01-23 13:59:02 +01:00
|
|
|
Copyright © 2022-2025 Thomas von Dein
|
2022-09-30 14:08:59 +02:00
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-30 19:14:58 +02:00
|
|
|
package lib
|
2022-09-28 19:30:08 +02:00
|
|
|
|
|
|
|
|
import (
|
2022-10-23 16:57:30 +02:00
|
|
|
"encoding/csv"
|
2022-09-28 19:30:08 +02:00
|
|
|
"fmt"
|
2022-10-17 20:04:05 +02:00
|
|
|
"io"
|
2022-10-16 19:44:26 +02:00
|
|
|
"log"
|
|
|
|
|
"strconv"
|
2022-09-28 19:30:08 +02:00
|
|
|
"strings"
|
2023-11-22 10:30:40 +01:00
|
|
|
|
|
|
|
|
"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"
|
2023-11-22 10:30:40 +01:00
|
|
|
"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) {
|
2025-01-15 18:51:15 +01:00
|
|
|
// 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)
|
|
|
|
|
|
2025-01-15 18:51:15 +01:00
|
|
|
// 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 {
|
2022-10-21 10:21:07 +02:00
|
|
|
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)
|
2022-10-21 10:21:07 +02:00
|
|
|
case cfg.Orgtbl:
|
2024-05-07 15:19:54 +02:00
|
|
|
printOrgmodeData(writer, conf, data)
|
2022-10-21 10:21:07 +02:00
|
|
|
case cfg.Markdown:
|
2024-05-07 15:19:54 +02:00
|
|
|
printMarkdownData(writer, conf, data)
|
2022-10-21 10:21:07 +02:00
|
|
|
case cfg.Shell:
|
2024-05-07 15:19:54 +02:00
|
|
|
printShellData(writer, data)
|
2022-10-21 10:21:07 +02:00
|
|
|
case cfg.Yaml:
|
2024-05-07 15:19:54 +02:00
|
|
|
printYamlData(writer, data)
|
2022-10-23 16:57:30 +02:00
|
|
|
case cfg.CSV:
|
2024-05-07 15:19:54 +02:00
|
|
|
printCSVData(writer, data)
|
2022-10-01 14:14:53 +02:00
|
|
|
default:
|
2024-05-07 18:01:12 +02:00
|
|
|
printASCIIData(writer, conf, data)
|
2022-10-01 14:14:53 +02:00
|
|
|
}
|
|
|
|
|
}
|
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)
|
2022-10-17 20:04:05 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-01 14:14:53 +02:00
|
|
|
/*
|
2023-04-21 09:52:05 +02:00
|
|
|
Emacs org-mode compatible table (also orgtbl-mode)
|
2022-10-01 14:14:53 +02:00
|
|
|
*/
|
2024-05-07 15:19:54 +02:00
|
|
|
func printOrgmodeData(writer io.Writer, conf cfg.Config, data *Tabdata) {
|
2022-10-01 14:14:53 +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.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)
|
2023-04-21 09:52:05 +02:00
|
|
|
}
|
2022-10-01 14:14:53 +02:00
|
|
|
|
2025-05-27 13:09:18 +02:00
|
|
|
if err := table.Render(); err != nil {
|
|
|
|
|
log.Fatalf("Failed to render table: %s", err)
|
2022-10-01 14:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-27 13:09:18 +02:00
|
|
|
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
|
2022-10-01 14:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2023-04-21 09:52:05 +02:00
|
|
|
Markdown table
|
2022-10-01 14:14:53 +02:00
|
|
|
*/
|
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,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
2022-10-01 14:14:53 +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)
|
2023-04-21 09:52:05 +02:00
|
|
|
}
|
2022-10-01 14:14:53 +02:00
|
|
|
|
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
|
|
|
}
|
2022-10-01 14:14:53 +02:00
|
|
|
|
2025-05-27 13:09:18 +02:00
|
|
|
if err := table.Render(); err != nil {
|
|
|
|
|
log.Fatalf("Failed to render table: %s", err)
|
|
|
|
|
}
|
2022-10-01 14:14:53 +02:00
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
|
2022-10-01 14:14:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2023-04-21 09:52:05 +02:00
|
|
|
Simple ASCII table without any borders etc, just like the input we expect
|
2022-10-01 14:14:53 +02:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
|
table := tablewriter.NewTable(tableString,
|
|
|
|
|
tablewriter.WithRenderer(
|
|
|
|
|
renderer.NewBlueprint(tw.Rendition{
|
|
|
|
|
Borders: tw.BorderNone,
|
|
|
|
|
Symbols: tw.NewSymbols(tw.StyleASCII),
|
|
|
|
|
Settings: tw.Settings{
|
|
|
|
|
Separators: tw.Separators{BetweenRows: tw.Off, BetweenColumns: tw.Off},
|
|
|
|
|
Lines: tw.Lines{ShowFooterLine: tw.Off, ShowHeaderLine: tw.Off},
|
|
|
|
|
},
|
|
|
|
|
})),
|
|
|
|
|
tablewriter.WithConfig(tablewriter.Config{
|
|
|
|
|
Header: tw.CellConfig{
|
|
|
|
|
Formatting: tw.CellFormatting{
|
|
|
|
|
AutoFormat: tw.Off,
|
|
|
|
|
},
|
|
|
|
|
Padding: tw.CellPadding{
|
|
|
|
|
Global: tw.Padding{Left: "", Right: ""},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Row: tw.CellConfig{
|
|
|
|
|
Formatting: tw.CellFormatting{
|
|
|
|
|
AutoWrap: tw.WrapNone,
|
|
|
|
|
Alignment: tw.AlignLeft,
|
|
|
|
|
},
|
|
|
|
|
Padding: tw.CellPadding{
|
|
|
|
|
Global: tw.Padding{Left: "", Right: ""},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Debug: true,
|
|
|
|
|
}),
|
|
|
|
|
tablewriter.WithPadding(tw.PaddingNone),
|
|
|
|
|
)
|
2022-10-01 14:14:53 +02:00
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
if !conf.NoHeaders {
|
2025-05-27 13:09:18 +02:00
|
|
|
table.Header(data.TabHeaders())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := table.Bulk(data.TabEntries()); err != nil {
|
|
|
|
|
log.Fatalf("Failed to add data to table renderer: %s", err)
|
2023-04-21 09:52:05 +02:00
|
|
|
}
|
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)
|
2023-11-22 10:30:40 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
output(writer, color.Sprint(colorizeData(conf, tableString.String())))
|
2022-09-28 19:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2023-04-21 09:52:05 +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)
|
2022-10-17 20:04:05 +02:00
|
|
|
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 {
|
2022-10-17 20:04:05 +02:00
|
|
|
out += color.Sprintf(format, data.headers[i], value)
|
2022-09-28 19:30:08 +02:00
|
|
|
}
|
2022-10-11 09:11:46 +02:00
|
|
|
|
2022-10-17 20:04:05 +02:00
|
|
|
out += "\n"
|
2022-09-28 19:30:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-10-17 20:04:05 +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
|
|
|
|
|
|
|
|
/*
|
2023-04-21 09:52:05 +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) {
|
2022-10-17 20:04:05 +02:00
|
|
|
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 {
|
2022-10-05 14:31:01 +02:00
|
|
|
shentries := []string{}
|
2024-05-07 18:01:12 +02:00
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
for idx, value := range entry {
|
2022-10-16 19:44:26 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2022-10-19 12:44:19 +02:00
|
|
|
|
2022-10-23 16:57:30 +02:00
|
|
|
// no colorization here
|
2024-05-07 15:19:54 +02:00
|
|
|
output(writer, out)
|
2022-10-03 13:28:04 +02:00
|
|
|
}
|
2022-10-16 19:44:26 +02:00
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
func printYamlData(writer io.Writer, data *Tabdata) {
|
|
|
|
|
type Data struct {
|
2022-10-16 19:44:26 +02:00
|
|
|
Entries []map[string]interface{} `yaml:"entries"`
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
yamlout := Data{}
|
2022-10-16 19:44:26 +02:00
|
|
|
|
|
|
|
|
for _, entry := range data.entries {
|
2024-05-07 15:19:54 +02:00
|
|
|
yamldata := map[string]interface{}{}
|
2022-10-16 19:44:26 +02:00
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
for idx, entry := range entry {
|
2022-10-16 19:44:26 +02:00
|
|
|
style := yaml.TaggedStyle
|
2024-05-07 18:01:12 +02:00
|
|
|
|
2022-10-16 19:44:26 +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])] =
|
2022-10-16 19:44:26 +02:00
|
|
|
&yaml.Node{
|
|
|
|
|
Kind: yaml.ScalarNode,
|
|
|
|
|
Style: style,
|
|
|
|
|
Value: entry}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
yamlout.Entries = append(yamlout.Entries, yamldata)
|
2022-10-16 19:44:26 +02:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
yamlstr, err := yaml.Marshal(&yamlout)
|
2022-10-16 19:44:26 +02:00
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
output(writer, string(yamlstr))
|
2022-10-16 19:44:26 +02:00
|
|
|
}
|
2022-10-23 16:57:30 +02:00
|
|
|
|
2024-05-07 15:19:54 +02:00
|
|
|
func printCSVData(writer io.Writer, data *Tabdata) {
|
|
|
|
|
csvout := csv.NewWriter(writer)
|
2022-10-23 16:57:30 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|