Skip to content

Commit

Permalink
Add completions for tables and sql
Browse files Browse the repository at this point in the history
  • Loading branch information
crosbymichael committed Nov 9, 2014
1 parent 51c48a0 commit 48c2f05
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"database/sql"
"os"
"strings"

ln "github.com/GeertJohan/go.linenoise"
"github.com/Sirupsen/logrus"
Expand Down Expand Up @@ -46,13 +47,46 @@ func loadDatabase(context *cli.Context) (*sql.DB, error) {
return db, nil
}

var completions = []string{
"select",
"where",
"from",
"containers",
"images",
}

// only complete on the last full word of the completion
func completion(input string) []string {
var (
out []string
parts = strings.Split(input, " ")
lower = strings.ToLower(parts[len(parts)-1])
l = len(lower)
)
for _, c := range completions {
if len(c) < l {
continue
}
if strings.HasPrefix(c, lower) {
if len(parts) == 1 {
out = append(out, c)
} else {
parts[len(parts)-1] = c
out = append(out, strings.Join(parts, " "))
}
}
}
return out
}

func mainAction(context *cli.Context) {
db, err := loadDatabase(context)
if err != nil {
logger.Fatal(err)
}
defer db.Close()
ln.SetMultiline(true)
ln.SetCompletionHandler(completion)
for {
query, err := ln.Line("> ")
if err != nil {
Expand Down

0 comments on commit 48c2f05

Please sign in to comment.