Skip to content

Commit

Permalink
Merge pull request #314 from SimonRichardson/tab-writer
Browse files Browse the repository at this point in the history
feat: tab writer
  • Loading branch information
cole-miller committed Aug 23, 2024
2 parents 4b60075 + 8f81d29 commit 982e8f0
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strconv"
"strings"
"text/tabwriter"

"github.com/canonical/go-dqlite"
"github.com/canonical/go-dqlite/client"
Expand Down Expand Up @@ -340,6 +341,12 @@ func (s *Shell) processQuery(ctx context.Context, line string) (string, error) {
n := len(columns)

var sb strings.Builder
writer := tabwriter.NewWriter(&sb, 0, 8, 1, '\t', 0)
for _, col := range columns {
fmt.Fprintf(writer, "%s\t", col)
}
fmt.Fprintln(writer)

for rows.Next() {
row := make([]interface{}, n)
rowPointers := make([]interface{}, n)
Expand All @@ -355,14 +362,10 @@ func (s *Shell) processQuery(ctx context.Context, line string) (string, error) {
return "", err
}

for i, column := range row {
if i == 0 {
fmt.Fprintf(&sb, "%v", column)
} else {
fmt.Fprintf(&sb, "|%v", column)
}
for _, column := range row {
fmt.Fprintf(writer, "%v\t", column)
}
sb.WriteByte('\n')
fmt.Fprintln(writer)
}

if err := rows.Err(); err != nil {
Expand All @@ -377,6 +380,10 @@ func (s *Shell) processQuery(ctx context.Context, line string) (string, error) {
return "", fmt.Errorf("commit: %w", err)
}

if err := writer.Flush(); err != nil {
return "", fmt.Errorf("flush: %w", err)
}

return strings.TrimRight(sb.String(), "\n"), nil
}

Expand Down

0 comments on commit 982e8f0

Please sign in to comment.