Skip to content

Commit 52bedf7

Browse files
authored
Merge pull request #40 from Balaji01-4D/feat/basic-auto-completion
Feat/basic auto completion
2 parents ace454b + 7175b36 commit 52bedf7

10 files changed

Lines changed: 579 additions & 659 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/fatih/color v1.19.0
1212
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1313
github.com/jedib0t/go-pretty/v6 v6.7.9
14-
github.com/jedib0t/go-prompter v0.1.0
14+
github.com/jedib0t/go-prompter v0.1.1-0.20260227172322-e85600bef9c9
1515
github.com/muesli/termenv v0.16.0
1616
github.com/pganalyze/pg_query_go/v6 v6.2.2
1717
github.com/spf13/cobra v1.10.2

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo
104104
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
105105
github.com/jedib0t/go-pretty/v6 v6.7.9 h1:frarzQWmkZd97syT81+TH8INKPpzoxQnk+Mk5EIHSrM=
106106
github.com/jedib0t/go-pretty/v6 v6.7.9/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
107-
github.com/jedib0t/go-prompter v0.1.0 h1:d90s+xWEp4+B52wI3NH5pO4zLG5ZMxoch7GPOxIlpHk=
108-
github.com/jedib0t/go-prompter v0.1.0/go.mod h1:r0rLW2jSPjaUdNsPyg+gYryZ5oaHOcow3OL9RFOT/bM=
107+
github.com/jedib0t/go-prompter v0.1.1-0.20260227172322-e85600bef9c9 h1:e+BsMawvjlbHkKVtrElEWFMhCsDOvxX2FSWDXbOnjYU=
108+
github.com/jedib0t/go-prompter v0.1.1-0.20260227172322-e85600bef9c9/go.mod h1:r0rLW2jSPjaUdNsPyg+gYryZ5oaHOcow3OL9RFOT/bM=
109109
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
110110
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
111111
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=

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
},

internal/parser/pg_kw.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package parser
2+
3+
import (
4+
_ "embed"
5+
"strings"
6+
)
7+
8+
//go:embed pg_kw.txt
9+
var pgKeywords string
10+
11+
func LoadPgKeywords() []string {
12+
return suggestionsFromFile(pgKeywords)
13+
}
14+
15+
func suggestionsFromFile(contents string) []string {
16+
var suggestions []string
17+
for _, line := range strings.Split(contents, "\n") {
18+
line = strings.TrimSpace(line)
19+
if line != "" {
20+
suggestions = append(suggestions, line)
21+
}
22+
}
23+
24+
return suggestions
25+
}

0 commit comments

Comments
 (0)