Skip to content

Commit 7175b36

Browse files
committed
add basic auto completion using postgres keyword
Signed-off-by: Balaji J <j.balaji2468@gmail.com>
1 parent 362f214 commit 7175b36

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

internal/app/app.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type PgxCLI struct {
3030
logger *slog.Logger
3131
}
3232

33-
func NewPgxCLI(config *config.Config, printer cliio.Printer, logger *slog.Logger) (*PgxCLI, error) {
33+
func New(config *config.Config, printer cliio.Printer, logger *slog.Logger) (*PgxCLI, error) {
3434
history, entries := newHistory(config.Main.HistoryFile, logger)
3535
reader, err := NewPgxReader()
3636
if err != nil {
@@ -126,6 +126,10 @@ func (p *PgxCLI) Start(ctx context.Context, client *database.Client) {
126126
}
127127
}
128128

129+
func (p *PgxCLI) SetAutocompleter(keywords []string) {
130+
p.prompt.SetAutocompleter(keywords)
131+
}
132+
129133
func (p *PgxCLI) Close() error {
130134
p.History.saveHistory(p.prompt.History())
131135
return nil

internal/app/reader.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var chromaFormatter = detectTerminalColorProfile()
2424
type Reader interface {
2525
Read(prefix string, ctx context.Context) (string, error)
2626
History() []prompt.HistoryCommand
27+
SetAutocompleter(keywords []string)
2728
}
2829

2930
type PgxReader struct {
@@ -48,6 +49,14 @@ func (r *PgxReader) Read(prefix string, ctx context.Context) (string, error) {
4849
return text, nil
4950
}
5051

52+
func (r *PgxReader) SetAutocompleter(keywords []string) {
53+
suggestions := make([]prompt.Suggestion, len(keywords))
54+
for i, kw := range keywords {
55+
suggestions[i] = prompt.Suggestion{Value: kw}
56+
}
57+
r.prompt.SetAutoCompleterContextual(prompt.AutoCompleteSimple(suggestions, true))
58+
}
59+
5160
func (r *PgxReader) History() []prompt.HistoryCommand {
5261
return r.prompt.History()
5362
}

internal/cli/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/balaji01-4d/pgxcli/internal/config"
1313
"github.com/balaji01-4d/pgxcli/internal/database"
1414
"github.com/balaji01-4d/pgxcli/internal/logger"
15+
"github.com/balaji01-4d/pgxcli/internal/parser"
1516
"github.com/balaji01-4d/pgxcli/internal/ui"
1617
"github.com/spf13/cobra"
1718
)
@@ -41,6 +42,8 @@ func NewRootCmd(ctx context.Context, cliCtx *CliContext) *cobra.Command {
4142
finalPassword string
4243
)
4344

45+
var pgKws []string
46+
4447
rootCmd := &cobra.Command{
4548
Use: "pgxcli [DBNAME] [USERNAME]",
4649
Short: "Interactive PostgreSQL command-line client for querying and managing databases.",
@@ -64,6 +67,8 @@ func NewRootCmd(ctx context.Context, cliCtx *CliContext) *cobra.Command {
6467

6568
cliCtx.Logger = logger
6669

70+
pgKws = parser.LoadPgKeywords()
71+
6772
return nil
6873
},
6974

@@ -190,11 +195,12 @@ func NewRootCmd(ctx context.Context, cliCtx *CliContext) *cobra.Command {
190195
return err
191196
}
192197

193-
app, err := app.NewPgxCLI(cliCtx.config, cliCtx.Printer, cliCtx.Logger.Logger)
198+
app, err := app.New(cliCtx.config, cliCtx.Printer, cliCtx.Logger.Logger)
194199
if err != nil {
195200
cliCtx.Logger.Error("Failed to initialize app", "error", err)
196201
return err
197202
}
203+
app.SetAutocompleter(pgKws)
198204
cliCtx.App = app
199205
return nil
200206
},

0 commit comments

Comments
 (0)