Skip to content

Commit

Permalink
Add -pretty as a command flag, output pretty printed tables using g…
Browse files Browse the repository at this point in the history
…ithub.com/olekukonko/tablewriter
  • Loading branch information
dinedal committed Dec 18, 2015
1 parent 4d542fc commit 1539f62
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 8 deletions.
4 changes: 4 additions & 0 deletions man/textql.1
Expand Up @@ -60,6 +60,10 @@ Display column names in output
Surpress logging
.
.TP
\fB\-pretty\fR
Pretty print output
.
.TP
\fB\-save\-to file\fR
SQLite3 db is left on disk at this file
.
Expand Down
1 change: 1 addition & 0 deletions man/textql.1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/textql.1.ronn
Expand Up @@ -50,6 +50,8 @@ common structured text format is CSV (RFC4180).
Display column names in output
* `-quiet`:
Surpress logging
* `-pretty`:
Pretty print output
* `-save-to file`:
SQLite3 db is left on disk at this file
* `-sql string`:
Expand Down
2 changes: 1 addition & 1 deletion outputs/csv.go
Expand Up @@ -73,7 +73,7 @@ func (csvOutput *CSVOutput) Show(rows *sql.Rows) {
writeErr := csvOutput.writer.Write(result)

if writeErr != nil {
log.Fatalln(colsErr)
log.Fatalln(writeErr)
}
}

Expand Down
76 changes: 76 additions & 0 deletions outputs/pretty_csv.go
@@ -0,0 +1,76 @@
package outputs

import (
"database/sql"
"io"
"log"

"github.com/olekukonko/tablewriter"
)

// PrettyCSVOutput represents a TextQL output that transforms sql.Rows into pretty tables
type PrettyCSVOutput struct {
options *PrettyCSVOutputOptions
writer *tablewriter.Table
firstRow []string
header []string
minOutputLength int
}

// PrettyCSVOutputOptions define options that are passed to tablewriter for formatting
// the output in specific ways.
type PrettyCSVOutputOptions struct {
// WriteHeader determines if a header row based on the column names should be written.
WriteHeader bool
// WriteTo is where the formatted data will be written to.
WriteTo io.Writer
}

// NewPrettyCSVOutput returns a new PrettyCSVOutput configured per the options provided.
func NewPrettyCSVOutput(opts *PrettyCSVOutputOptions) *PrettyCSVOutput {
prettyCsvOutput := &PrettyCSVOutput{
options: opts,
writer: tablewriter.NewWriter(opts.WriteTo),
}

return prettyCsvOutput
}

// Show writes the sql.Rows given to the destination in tablewriter basic format.
func (prettyCsvOutput *PrettyCSVOutput) Show(rows *sql.Rows) {
cols, colsErr := rows.Columns()

if colsErr != nil {
log.Fatalln(colsErr)
}

if prettyCsvOutput.options.WriteHeader {
prettyCsvOutput.writer.SetHeader(cols)
}

rawResult := make([][]byte, len(cols))
result := make([]string, len(cols))

dest := make([]interface{}, len(cols))

for i := range cols {
dest[i] = &rawResult[i]
}

for rows.Next() {
rows.Scan(dest...)

for i, raw := range rawResult {
result[i] = string(raw)
}

writeErr := prettyCsvOutput.writer.Append(result)

if writeErr != nil {
log.Fatalln(writeErr)
}
}

prettyCsvOutput.writer.Render()
rows.Close()
}
29 changes: 22 additions & 7 deletions textql/main.go
Expand Up @@ -27,6 +27,7 @@ type commandLineOptions struct {
Console *bool
Version *bool
Quiet *bool
Pretty *bool
}

// Must be set at build via -ldflags "-X main.VERSION=`cat VERSION`"
Expand All @@ -44,6 +45,7 @@ func newCommandLineOptions() *commandLineOptions {
cmdLineOpts.Console = flag.Bool("console", false, "After all statements are run, open SQLite3 REPL with this data")
cmdLineOpts.Version = flag.Bool("version", false, "Print version and exit")
cmdLineOpts.Quiet = flag.Bool("quiet", false, "Surpress logging")
cmdLineOpts.Pretty = flag.Bool("pretty", false, "Output pretty formatting")
flag.Usage = cmdLineOpts.Usage
flag.Parse()

Expand Down Expand Up @@ -94,11 +96,15 @@ func (clo *commandLineOptions) GetQuiet() bool {
return *clo.Quiet
}

func (clo *commandLineOptions) GetPretty() bool {
return *clo.Pretty
}

func (clo *commandLineOptions) Usage() {
if !clo.GetQuiet() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, " %s [-console] [-save-to path path] [-output-file path] [-output-dlm delimter] [-output-header] [-header] [-dlm delimter] [-sql sql_statements] [-quiet] [path ...] \n", os.Args[0])
fmt.Fprintf(os.Stderr, " %s [-console] [-save-to path path] [-output-file path] [-output-dlm delimter] [-output-header] [-pretty] [-quiet] [-header] [-dlm delimter] [-sql sql_statements] [path ...] \n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
flag.PrintDefaults()
}
Expand Down Expand Up @@ -170,13 +176,22 @@ func main() {
sqlStrings := strings.Split(cmdLineOpts.GetStatements(), ";")

if cmdLineOpts.GetOutputFile() != "" {
displayOpts := &outputs.CSVOutputOptions{
WriteHeader: cmdLineOpts.GetOutputHeader(),
Seperator: util.DetermineSeparator(cmdLineOpts.GetOutputDelimiter()),
WriteTo: util.OpenFileOrStdDev(cmdLineOpts.GetOutputFile(), true),
}
if cmdLineOpts.GetPretty() {
displayOpts := &outputs.PrettyCSVOutputOptions{
WriteHeader: cmdLineOpts.GetOutputHeader(),
WriteTo: util.OpenFileOrStdDev(cmdLineOpts.GetOutputFile(), true),
}

outputer = outputs.NewCSVOutput(displayOpts)
outputer = outputs.NewPrettyCSVOutput(displayOpts)
} else {
displayOpts := &outputs.CSVOutputOptions{
WriteHeader: cmdLineOpts.GetOutputHeader(),
Seperator: util.DetermineSeparator(cmdLineOpts.GetOutputDelimiter()),
WriteTo: util.OpenFileOrStdDev(cmdLineOpts.GetOutputFile(), true),
}

outputer = outputs.NewCSVOutput(displayOpts)
}
}

for _, sqlQuery := range sqlStrings {
Expand Down

0 comments on commit 1539f62

Please sign in to comment.