mirror of
https://codeberg.org/scip/tablizer.git
synced 2025-12-17 04:30:56 +01:00
added yaml output mode support (-o yaml or -Y)
This commit is contained in:
@@ -100,11 +100,13 @@ func init() {
|
||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagMarkdown, "markdown", "M", false, "Enable markdown table output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagOrgtable, "orgtbl", "O", false, "Enable org-mode table output")
|
||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagShell, "shell", "S", false, "Enable shell mode output")
|
||||
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell")
|
||||
rootCmd.PersistentFlags().BoolVarP(&lib.OutflagYaml, "yaml", "Y", false, "Enable yaml output")
|
||||
rootCmd.MarkFlagsMutuallyExclusive("extended", "markdown", "orgtbl", "shell", "yaml")
|
||||
rootCmd.Flags().MarkHidden("extended")
|
||||
rootCmd.Flags().MarkHidden("orgtbl")
|
||||
rootCmd.Flags().MarkHidden("markdown")
|
||||
rootCmd.Flags().MarkHidden("shell")
|
||||
rootCmd.Flags().MarkHidden("yaml")
|
||||
|
||||
// same thing but more common, takes precedence over above group
|
||||
rootCmd.PersistentFlags().StringVarP(&lib.OutputMode, "output", "o", "", "Output mode - one of: orgtbl, markdown, extended, shell, ascii(default)")
|
||||
|
||||
@@ -16,7 +16,7 @@ SYNOPSIS
|
||||
-m, --man Display manual page
|
||||
-n, --no-numbering Disable header numbering
|
||||
-N, --no-color Disable pattern highlighting
|
||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, yaml, ascii(default)
|
||||
-X, --extended Enable extended output
|
||||
-M, --markdown Enable markdown table output
|
||||
-O, --orgtbl Enable org-mode table output
|
||||
@@ -178,7 +178,8 @@ DESCRIPTION
|
||||
|
||||
Beside normal ascii mode (the default) and extended mode there are more
|
||||
output modes available: orgtbl which prints an Emacs org-mode table and
|
||||
markdown which prints a Markdown table.
|
||||
markdown which prints a Markdown table and yaml, which prints yaml
|
||||
encoding.
|
||||
|
||||
BUGS
|
||||
In order to report a bug, unexpected behavior, feature requests or to
|
||||
|
||||
1
go.mod
1
go.mod
@@ -8,6 +8,7 @@ require (
|
||||
github.com/gookit/color v1.5.2
|
||||
github.com/olekukonko/tablewriter v0.0.5
|
||||
github.com/spf13/cobra v1.5.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
1
go.sum
1
go.sum
@@ -35,6 +35,7 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHg
|
||||
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -133,7 +133,7 @@
|
||||
.\" ========================================================================
|
||||
.\"
|
||||
.IX Title "TABLIZER 1"
|
||||
.TH TABLIZER 1 "2022-10-15" "1" "User Commands"
|
||||
.TH TABLIZER 1 "2022-10-16" "1" "User Commands"
|
||||
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
|
||||
.\" way too many mistakes in technical documents.
|
||||
.if n .ad l
|
||||
@@ -154,7 +154,7 @@ tablizer \- Manipulate tabular output of other programs
|
||||
\& \-m, \-\-man Display manual page
|
||||
\& \-n, \-\-no\-numbering Disable header numbering
|
||||
\& \-N, \-\-no\-color Disable pattern highlighting
|
||||
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, ascii(default)
|
||||
\& \-o, \-\-output string Output mode \- one of: orgtbl, markdown, extended, yaml, ascii(default)
|
||||
\& \-X, \-\-extended Enable extended output
|
||||
\& \-M, \-\-markdown Enable markdown table output
|
||||
\& \-O, \-\-orgtbl Enable org\-mode table output
|
||||
@@ -345,7 +345,8 @@ You can use this in an eval loop.
|
||||
.PP
|
||||
Beside normal ascii mode (the default) and extended mode there are
|
||||
more output modes available: \fBorgtbl\fR which prints an Emacs org-mode
|
||||
table and \fBmarkdown\fR which prints a Markdown table.
|
||||
table and \fBmarkdown\fR which prints a Markdown table and \fByaml\fR, which
|
||||
prints yaml encoding.
|
||||
.SH "BUGS"
|
||||
.IX Header "BUGS"
|
||||
In order to report a bug, unexpected behavior, feature requests
|
||||
|
||||
@@ -15,7 +15,7 @@ tablizer - Manipulate tabular output of other programs
|
||||
-m, --man Display manual page
|
||||
-n, --no-numbering Disable header numbering
|
||||
-N, --no-color Disable pattern highlighting
|
||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, ascii(default)
|
||||
-o, --output string Output mode - one of: orgtbl, markdown, extended, yaml, ascii(default)
|
||||
-X, --extended Enable extended output
|
||||
-M, --markdown Enable markdown table output
|
||||
-O, --orgtbl Enable org-mode table output
|
||||
@@ -198,7 +198,8 @@ You can use this in an eval loop.
|
||||
|
||||
Beside normal ascii mode (the default) and extended mode there are
|
||||
more output modes available: B<orgtbl> which prints an Emacs org-mode
|
||||
table and B<markdown> which prints a Markdown table.
|
||||
table and B<markdown> which prints a Markdown table and B<yaml>, which
|
||||
prints yaml encoding.
|
||||
|
||||
=head1 BUGS
|
||||
|
||||
|
||||
Reference in New Issue
Block a user