Skip to content

Commit

Permalink
Rough in \chart command
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Jun 10, 2024
1 parent 69b3635 commit da3c16d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
12 changes: 9 additions & 3 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ func (h *Handler) Execute(ctx context.Context, w io.Writer, opt metacmd.Option,
f = h.doExecSet
case metacmd.ExecWatch:
f = h.doExecWatch
case metacmd.ExecChart:
f = h.doExecChart
}
if err = drivers.WrapErr(h.u.Driver, f(ctx, w, opt, prefix, sqlstr, qtyp, bind)); err != nil {
if forceTrans {
Expand Down Expand Up @@ -1031,6 +1033,11 @@ func (h *Handler) doExecWatch(ctx context.Context, w io.Writer, opt metacmd.Opti
}
}

// doExecChart executes a single query against the database, displaying its output as a chart.
func (h *Handler) doExecChart(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, qtyp bool, bind []interface{}) error {
return nil
}

// doExecSingle executes a single query against the database based on its query type.
func (h *Handler) doExecSingle(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, qtyp bool, bind []interface{}) error {
// exec or query
Expand Down Expand Up @@ -1158,12 +1165,11 @@ func (h *Handler) doQuery(ctx context.Context, w io.Writer, opt metacmd.Option,
case drivers.UseColumnTypes(h.u):
extra = append(extra, tblfmt.WithUseColumnTypes(true))
}
// wrap query with crosstab
resultSet := tblfmt.ResultSet(rows)
// wrap query with crosstab
if opt.Exec == metacmd.ExecCrosstab {
var err error
resultSet, err = tblfmt.NewCrosstabView(rows, append(extra, tblfmt.WithParams(opt.Crosstab...))...)
if err != nil {
if resultSet, err = tblfmt.NewCrosstabView(rows, append(extra, tblfmt.WithParams(opt.Crosstab...))...); err != nil {
return err
}
extra = nil
Expand Down
3 changes: 3 additions & 0 deletions metacmd/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ func init() {
{"gexec", "", "execute query and execute each value of the result"},
{"gset", "[PREFIX]", "execute query and store results in " + text.CommandName + " variables"},
{"crosstabview", "[(OPTIONS)] [COLUMNS]", "execute query and display results in crosstab"},
{"chart", "CHART [(OPTIONS)]", "execute query and display results as a chart"},
{"watch", "[(OPTIONS)] [DURATION]", "execute query every specified interval"},
},
Process: func(p *Params) error {
Expand Down Expand Up @@ -295,6 +296,8 @@ func init() {
break
}
}
case "chart":
p.Option.Exec = ExecChart
case "watch":
p.Option.Exec = ExecWatch
p.Option.Watch = 2 * time.Second
Expand Down
2 changes: 2 additions & 0 deletions metacmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ const (
ExecExec
// ExecCrosstab indicates execution using crosstabview (\crosstabview).
ExecCrosstab
// ExecChart indicates execution using chart (\chart).
ExecChart
// ExecWatch indicates repeated execution with a fixed time interval.
ExecWatch
)
Expand Down
26 changes: 12 additions & 14 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,19 @@ func Run(ctx context.Context, args *Args, connections map[string]interface{}, in
cygwin := isatty.IsCygwinTerminal(os.Stdout.Fd()) && isatty.IsCygwinTerminal(os.Stdin.Fd())
forceNonInteractive := len(args.CommandOrFiles) != 0

/*
// enable term graphics
if !forceNonInteractive && interactive && !cygwin {
// NOTE: this is done here and not in the env.init() package, because
// NOTE: we need to determine if it is interactive first, otherwise it
// NOTE: could mess up the non-interactive output with control characters
var typ string
if s, _ := env.Getenv(text.CommandUpper()+"_TERM_GRAPHICS", "TERM_GRAPHICS"); s != "" {
typ = s
}
if err := env.Set("TERM_GRAPHICS", typ); err != nil {
return err
}
// enable term graphics
if !forceNonInteractive && interactive && !cygwin {
// NOTE: this is done here and not in the env.init() package, because
// NOTE: we need to determine if it is interactive first, otherwise it
// NOTE: could mess up the non-interactive output with control characters
var typ string
if s, _ := env.Getenv(text.CommandUpper()+"_TERM_GRAPHICS", "TERM_GRAPHICS"); s != "" {
typ = s
}
*/
if err := env.Set("TERM_GRAPHICS", typ); err != nil {
return err
}
}

// configured named connections
for name, v := range connections {
Expand Down

0 comments on commit da3c16d

Please sign in to comment.