Skip to content

Commit da3c16d

Browse files
committed
Rough in \chart command
1 parent 69b3635 commit da3c16d

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

handler/handler.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ func (h *Handler) Execute(ctx context.Context, w io.Writer, opt metacmd.Option,
429429
f = h.doExecSet
430430
case metacmd.ExecWatch:
431431
f = h.doExecWatch
432+
case metacmd.ExecChart:
433+
f = h.doExecChart
432434
}
433435
if err = drivers.WrapErr(h.u.Driver, f(ctx, w, opt, prefix, sqlstr, qtyp, bind)); err != nil {
434436
if forceTrans {
@@ -1031,6 +1033,11 @@ func (h *Handler) doExecWatch(ctx context.Context, w io.Writer, opt metacmd.Opti
10311033
}
10321034
}
10331035

1036+
// doExecChart executes a single query against the database, displaying its output as a chart.
1037+
func (h *Handler) doExecChart(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, qtyp bool, bind []interface{}) error {
1038+
return nil
1039+
}
1040+
10341041
// doExecSingle executes a single query against the database based on its query type.
10351042
func (h *Handler) doExecSingle(ctx context.Context, w io.Writer, opt metacmd.Option, prefix, sqlstr string, qtyp bool, bind []interface{}) error {
10361043
// exec or query
@@ -1158,12 +1165,11 @@ func (h *Handler) doQuery(ctx context.Context, w io.Writer, opt metacmd.Option,
11581165
case drivers.UseColumnTypes(h.u):
11591166
extra = append(extra, tblfmt.WithUseColumnTypes(true))
11601167
}
1161-
// wrap query with crosstab
11621168
resultSet := tblfmt.ResultSet(rows)
1169+
// wrap query with crosstab
11631170
if opt.Exec == metacmd.ExecCrosstab {
11641171
var err error
1165-
resultSet, err = tblfmt.NewCrosstabView(rows, append(extra, tblfmt.WithParams(opt.Crosstab...))...)
1166-
if err != nil {
1172+
if resultSet, err = tblfmt.NewCrosstabView(rows, append(extra, tblfmt.WithParams(opt.Crosstab...))...); err != nil {
11671173
return err
11681174
}
11691175
extra = nil

metacmd/cmds.go

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ func init() {
249249
{"gexec", "", "execute query and execute each value of the result"},
250250
{"gset", "[PREFIX]", "execute query and store results in " + text.CommandName + " variables"},
251251
{"crosstabview", "[(OPTIONS)] [COLUMNS]", "execute query and display results in crosstab"},
252+
{"chart", "CHART [(OPTIONS)]", "execute query and display results as a chart"},
252253
{"watch", "[(OPTIONS)] [DURATION]", "execute query every specified interval"},
253254
},
254255
Process: func(p *Params) error {
@@ -295,6 +296,8 @@ func init() {
295296
break
296297
}
297298
}
299+
case "chart":
300+
p.Option.Exec = ExecChart
298301
case "watch":
299302
p.Option.Exec = ExecWatch
300303
p.Option.Watch = 2 * time.Second

metacmd/types.go

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ const (
9999
ExecExec
100100
// ExecCrosstab indicates execution using crosstabview (\crosstabview).
101101
ExecCrosstab
102+
// ExecChart indicates execution using chart (\chart).
103+
ExecChart
102104
// ExecWatch indicates repeated execution with a fixed time interval.
103105
ExecWatch
104106
)

run.go

+12-14
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,19 @@ func Run(ctx context.Context, args *Args, connections map[string]interface{}, in
237237
cygwin := isatty.IsCygwinTerminal(os.Stdout.Fd()) && isatty.IsCygwinTerminal(os.Stdin.Fd())
238238
forceNonInteractive := len(args.CommandOrFiles) != 0
239239

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

256254
// configured named connections
257255
for name, v := range connections {

0 commit comments

Comments
 (0)