mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-18 04:51:05 +01:00
added yaml output mode support (-o yaml or -Y)
This commit is contained in:
@@ -36,6 +36,7 @@ var (
|
||||
OutflagMarkdown bool
|
||||
OutflagOrgtable bool
|
||||
OutflagShell bool
|
||||
OutflagYaml bool
|
||||
OutputMode string
|
||||
InvertMatch bool
|
||||
Pattern string
|
||||
@@ -65,7 +66,7 @@ var (
|
||||
}
|
||||
|
||||
// used for validation
|
||||
validOutputmodes = "(orgtbl|markdown|extended|ascii)"
|
||||
validOutputmodes = "(orgtbl|markdown|extended|ascii|yaml)"
|
||||
|
||||
// main program version
|
||||
Version = "v1.0.11"
|
||||
|
||||
@@ -154,6 +154,9 @@ func PrepareModeFlags() error {
|
||||
case OutflagShell:
|
||||
OutputMode = "shell"
|
||||
NoNumbering = true
|
||||
case OutflagYaml:
|
||||
OutputMode = "yaml"
|
||||
NoNumbering = true
|
||||
default:
|
||||
OutputMode = "ascii"
|
||||
}
|
||||
|
||||
@@ -21,7 +21,11 @@ import (
|
||||
"fmt"
|
||||
"github.com/gookit/color"
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"gopkg.in/yaml.v3"
|
||||
"log"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -50,6 +54,8 @@ func printData(data *Tabdata) {
|
||||
printMarkdownData(data)
|
||||
case "shell":
|
||||
printShellData(data)
|
||||
case "yaml":
|
||||
printYamlData(data)
|
||||
default:
|
||||
printAsciiData(data)
|
||||
}
|
||||
@@ -174,10 +180,47 @@ func printShellData(data *Tabdata) {
|
||||
}
|
||||
}
|
||||
|
||||
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"", data.headers[idx], value))
|
||||
shentries = append(shentries, fmt.Sprintf("%s=\"%s\"",
|
||||
data.headers[idx], value))
|
||||
idx++
|
||||
}
|
||||
fmt.Println(strings.Join(shentries, " "))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func printYamlData(data *Tabdata) {
|
||||
type D struct {
|
||||
Entries []map[string]interface{} `yaml:"entries"`
|
||||
}
|
||||
|
||||
d := D{}
|
||||
|
||||
for _, entry := range data.entries {
|
||||
ml := map[string]interface{}{}
|
||||
|
||||
for i, entry := range entry {
|
||||
style := yaml.TaggedStyle
|
||||
_, err := strconv.Atoi(entry)
|
||||
if err != nil {
|
||||
style = yaml.DoubleQuotedStyle
|
||||
}
|
||||
|
||||
ml[strings.ToLower(data.headers[i])] =
|
||||
&yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Style: style,
|
||||
Value: entry}
|
||||
}
|
||||
|
||||
d.Entries = append(d.Entries, ml)
|
||||
}
|
||||
|
||||
yamlstr, err := yaml.Marshal(&d)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
os.Stdout.Write(yamlstr)
|
||||
}
|
||||
|
||||
@@ -88,6 +88,13 @@ THREE(3): cxxxncnc
|
||||
ONE(1): 19191
|
||||
TWO(2): EDD 1
|
||||
THREE(3): X`,
|
||||
"yaml": `entries:
|
||||
- one: "asd"
|
||||
three: "cxxxncnc"
|
||||
two: "igig"
|
||||
- one: 19191
|
||||
three: "X"
|
||||
two: "EDD 1"`,
|
||||
}
|
||||
|
||||
NoColor = true
|
||||
@@ -100,6 +107,13 @@ THREE(3): X`,
|
||||
t.Run(testname, func(t *testing.T) {
|
||||
|
||||
OutputMode = mode
|
||||
|
||||
if mode == "yaml" {
|
||||
NoNumbering = true
|
||||
} else {
|
||||
NoNumbering = false
|
||||
}
|
||||
|
||||
// we need to reset our mock data, since it's being
|
||||
// modified in printData()
|
||||
data := startdata
|
||||
@@ -122,7 +136,7 @@ THREE(3): X`,
|
||||
|
||||
// Restore
|
||||
os.Stdout = origStdout
|
||||
|
||||
NoNumbering = false
|
||||
}
|
||||
|
||||
func TestSortPrinter(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user