Skip to content

Commit d379649

Browse files
committed
Support SQL-Keyword completion (SELECT/FROM/WHERE...)
1 parent 3d38b02 commit d379649

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

main.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"github.com/mattn/go-colorable"
2222

2323
"github.com/hymkor/go-multiline-ny"
24+
"github.com/nyaosorg/go-readline-ny/completion"
25+
"github.com/nyaosorg/go-readline-ny/keys"
2426
)
2527

2628
func cutField(s string) (string, string) {
@@ -354,6 +356,35 @@ func (ss *Session) Loop(ctx context.Context, commandIn CommandIn, onErrorAbort b
354356
return nil
355357
}
356358

359+
type sqlCompletion struct{}
360+
361+
func (sqlCompletion) Enclosures() string {
362+
return `'"`
363+
}
364+
365+
func (sqlCompletion) Delimiters() string {
366+
return ","
367+
}
368+
369+
func (sqlCompletion) List(field []string) (fullnames []string, basenames []string) {
370+
if len(field) <= 1 {
371+
fullnames = []string{
372+
"ALTER", "COMMIT", "CREATE", "DELETE", "DESC", "DROP", "EXIT",
373+
"HISTORY", "INSERT", "QUIT", "REM", "ROLLBACK", "SELECT", "SPOOL",
374+
"START", "TRUNCATE", "UPDATE", "\\D",
375+
}
376+
} else if len(field) >= 5 {
377+
fullnames = []string{
378+
"AND", "FROM", "INTO", "OR", "WHERE",
379+
}
380+
} else if len(field) >= 3 {
381+
fullnames = []string{
382+
"FROM", "INTO",
383+
}
384+
}
385+
return
386+
}
387+
357388
func mains(args []string) error {
358389
if len(args) < 2 {
359390
usage(os.Stdout)
@@ -424,6 +455,9 @@ func mains(args []string) error {
424455
}
425456
return fmt.Fprintf(w, "%3d> ", i+1)
426457
})
458+
editor.BindKey(keys.CtrlI, completion.CmdCompletion{
459+
Completion: sqlCompletion{},
460+
})
427461
return session.Loop(ctx, &editor, false)
428462
}
429463

0 commit comments

Comments
 (0)