Skip to content

Commit

Permalink
feat: add markdown support
Browse files Browse the repository at this point in the history
  • Loading branch information
nehemming committed Aug 10, 2021
1 parent ce4ab01 commit dedd13d
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cliflags/langpack/langpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ const (

var languagePack = lpax.TextMap{

FlagsReportingFormat: "output format (csv|json|yaml|text). Default is text.",
FlagsReportingStyle: "output style (plain|grid|aligned) Default for text is aligned",
FlagsReportingFormat: "output format (csv|json|yaml|text). Default is text",
FlagsReportingStyle: "output style (plain|grid|aligned|md) Default for text is aligned",
FlagsReportingTemplate: "template string to use for text output. Uses Go templating syntax",
FlagsReportingTemplateFile: "template file path. File must ne in GO templating syntax ",
FlagsReportingTemplateFile: "template file path. File must ne in GO templating syntax",
FlagsReportingIndent: "indenting to use with JSON formating, 0 for single line output",
FlagsReportingInclude: "columns to output",
FlagsReportingExclude: "columns to exclude from output",
Expand Down
9 changes: 6 additions & 3 deletions textformatter/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,22 @@ const (

// Grid surrounding.
Grid

// Markdown uses a markdown table format.
Markdown
)

// GetTextStyleFromString get the text style for a string.
func GetTextStyleFromString(style string) (TableStyle, error) {
switch style {
case "plain":
return Plain, nil
case "":
fallthrough
case "aligned":
case "", "aligned":
return Aligned, nil
case "grid":
return Grid, nil
case "md":
return Markdown, nil
default:
return Plain, lpax.Errorf(langpack.ErrorUnknownStyle, style)
}
Expand Down
72 changes: 72 additions & 0 deletions textformatter/tabular.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func (tablet *tabular) write(out io.Writer, style TableStyle, excludeHeader bool
case Grid:
return tablet.writeAligned(out, excludeHeader, true, 1, terminalWidth)

case Markdown:
return tablet.writeMarkdown(out)

default:
return lpax.Errorf(langpack.ErrorUnknownStyle, style)
}
Expand Down Expand Up @@ -353,6 +356,75 @@ func (tablet *tabular) writePlain(out io.Writer, excludeHeader bool, columnSepar
return nil
}

func (tablet *tabular) writeMarkdown(out io.Writer) error {
// basic raw output, using a separator between columns

if err := tablet.writeMarkdownHeader(out); err != nil {
return err
}

for _, row := range tablet.rows {
if err := tablet.writeMarkdownRow(out, row); err != nil {
return err
}
}

return nil
}

func (tablet *tabular) writeMarkdownHeader(out io.Writer) error {
if len(tablet.columns) == 0 {
return nil
}

// add fin line return
//nolint:errcheck
defer out.Write([]byte("\n"))

for _, col := range tablet.columns {
_, err := out.Write([]byte("|" + col.name))
if err != nil {
return err
}
}
_, err := out.Write([]byte("|\n"))
if err != nil {
return err
}
_, err = out.Write([]byte(strings.Repeat("|-", len(tablet.columns))))
if err != nil {
return err
}
_, err = out.Write([]byte("|"))
if err != nil {
return err
}
return nil
}

func (tablet *tabular) writeMarkdownRow(out io.Writer, row []string) error {
if len(row) == 0 {
return nil
}

// add in line return on completion / error
//nolint:errcheck
defer out.Write([]byte("\n"))

for _, field := range row {
_, err := out.Write([]byte("|" + field))
if err != nil {
return err
}
}

_, err := out.Write([]byte("|"))
if err != nil {
return err
}
return nil
}

func (tablet *tabular) writePlainHeader(out io.Writer, columnSeparator string) error {
if len(tablet.columns) == 0 {
return nil
Expand Down
37 changes: 37 additions & 0 deletions textformatter/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,43 @@ today
testsupport.CompareStrings(t, expected, got)
}

func TestNewFormatterMarkdown(t *testing.T) {
fmt, err := NewFormatter()
if err != nil {
t.Errorf("Error %v", err)
}

if fmt == nil {
t.Error("No formatter")
}

// Generate some output
var buf bytes.Buffer

options := NewOptions()

options.Style = Markdown
options.ExcludeSet["I"] = true

err = fmt.Format(&buf, options, []testData{
{S: "Hello", I: 10, F: 3.14, N: innerData{Sin: "Inside"}},
{S: "Bye", I: 32, F: 2.77, N: innerData{Sin: "Outside"}},
})

if err != nil {
t.Errorf("Formatter Error %v", err)
}

expected := `|S|F|Sun|
|-|-|-|
|Hello|3.14|Inside|
|Bye|2.77|Outside|
`
got := buf.String()

testsupport.CompareStrings(t, expected, got)
}

func TestNewFormatterBadStyle(t *testing.T) {
fmt, err := NewFormatter()
if err != nil {
Expand Down

0 comments on commit dedd13d

Please sign in to comment.