Files
tablizer/lib/printer.go

292 lines
6.5 KiB
Go
Raw Normal View History

/*
Copyright © 2022 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"
"regexp"
"strconv"
2022-09-28 19:30:08 +02:00
"strings"
2025-01-20 19:28:19 +01:00
"github.com/atotto/clipboard"
"github.com/gookit/color"
"github.com/olekukonko/tablewriter"
"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{}
table := tablewriter.NewWriter(tableString)
2022-09-28 19:30:08 +02:00
2024-05-07 15:19:54 +02:00
if !conf.NoHeaders {
table.SetHeader(data.headers)
}
for _, row := range data.entries {
table.Append(trimRow(row))
}
table.Render()
/* fix output for org-mode (orgtbl)
tableWriter output:
+------+------+
| cell | cell |
+------+------+
Needed for org-mode compatibility:
|------+------|
| cell | cell |
|------+------|
*/
2022-10-19 12:57:50 +02:00
leftR := regexp.MustCompile(`(?m)^\\+`)
rightR := regexp.MustCompile(`\\+(?m)$`)
2024-05-07 15:19:54 +02:00
output(writer, color.Sprint(
colorizeData(conf,
2022-10-10 20:14:51 +02:00
rightR.ReplaceAllString(
leftR.ReplaceAllString(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{}
table := tablewriter.NewWriter(tableString)
2024-05-07 15:19:54 +02:00
if !conf.NoHeaders {
table.SetHeader(data.headers)
}
for _, row := range data.entries {
table.Append(trimRow(row))
2022-09-28 19:30:08 +02:00
}
table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false})
table.SetCenterSeparator("|")
table.Render()
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{}
table := tablewriter.NewWriter(tableString)
2024-05-07 15:19:54 +02:00
if !conf.NoHeaders {
table.SetHeader(data.headers)
}
2024-05-07 18:01:12 +02:00
table.AppendBulk(data.entries)
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetNoWhiteSpace(true)
2024-05-07 15:19:54 +02:00
if !conf.UseHighlight {
// the tabs destroy the highlighting
table.SetTablePadding("\t") // pad with tabs
} else {
table.SetTablePadding(" ")
}
table.Render()
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)
}
}
2025-01-20 19:28:19 +01:00
func yankColumns(conf cfg.Config, data *Tabdata) {
var yank string
if len(data.entries) == 0 || len(conf.UseYankColumns) == 0 {
return
}
for _, row := range data.entries {
for i, field := range row {
for _, idx := range conf.UseYankColumns {
if i == idx-1 {
yank += field + " "
}
}
}
}
if yank != "" {
2025-01-20 21:27:26 +01:00
clipboard.Primary = true // unix
2025-01-20 19:28:19 +01:00
clipboard.WriteAll(yank)
}
}