Skip to content

Go module for outputting data in various table formats.

License

Notifications You must be signed in to change notification settings

cuberat-go/fmttable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fmttable

Go module for outputting data in various table formats.

go get github.com/cuberat-go/fmttable

There is also a sample command (dsv2table) to convert delimited files to table output under the cmd directory that you can install:

go install github.com/cuberat-go/fmttable/cmd/dsv2table@latest

Example

    csvr := csv.NewReader(r)
    csvr.Comma = ','
    csvr.TrimLeadingSpace = true

    var (
        headers []string
        rows    [][]string
    )

    // Read the CSV data.
    for {
        record, err := csvr.Read()
        if err == io.EOF {
            break
        }
        if err != nil {
            slog.Error("Error reading CSV data", "error", err)
            return err
        }

        if headers == nil {
            headers = record
        } else {
            rows = append(rows, record)
        }
    }

    table := fmttable.NewTableFormat(headers, rows).WithStyle(conf.TableStyle)
    if table == nil {
        return fmt.Errorf("invalid table style: %s", conf.TableStyle)
    }

    tableStr := table.String()

Formats

default

+---+---+
|foo|bar|
+---+---+
|1  |2  |
|3  |4  |
|5  |6  |
|7  |8  |
+---+---+

confluence

Atlassian Confluence style table.

|| foo || bar ||
| 1    | 2    |
| 3    | 4    |
| 5    | 6    |
| 7    | 8    |

markdown

GitHub style Markdown table.

| foo | bar |
| --- | --- |
| 1   | 2   |
| 3   | 4   |
| 5   | 6   |
| 7   | 8   |

About

Go module for outputting data in various table formats.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages