Skip to content

cmd: Use readline library. #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 39 additions & 48 deletions cmd/gitql/shell.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package main

import (
"bufio"
"bytes"
"fmt"
"os"
"strings"

"github.com/chzyer/readline"
)

const (
initPrompt = "!> "
multilinePrompt = "!>>> "
)

type CmdShell struct {
Expand All @@ -21,69 +25,56 @@ func (c *CmdShell) Execute(args []string) error {
return err
}

fmt.Print(`
rl, err := readline.NewEx(&readline.Config{
Prompt: initPrompt,
HistoryFile: "/tmp/gitql-history",
DisableAutoSaveHistory: true,
})
if err != nil {
return err
}

rl.Terminal.Print(fmt.Sprint(`
gitQL SHELL
-----------
You must end your queries with ';'

`)

s := bufio.NewScanner(os.Stdin)

s.Split(scanQueries)
`))

var cmds []string
for {
fmt.Print("!> ")

if !s.Scan() {
line, err := rl.Readline()
if err != nil {
break
}
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
cmds = append(cmds, line)
if !strings.HasSuffix(line, ";") {
rl.SetPrompt(multilinePrompt)
continue
}

query := s.Text()

query = strings.Replace(query, "\n", " ", -1)
query = strings.TrimSpace(query)
query := strings.Join(cmds, " ")
cmds = cmds[:0]
rl.SetPrompt(initPrompt)
rl.SaveHistory(query)

fmt.Printf("\n--> Executing query: %s\n\n", query)
rl.Terminal.Print(fmt.Sprintf("\n--> Executing query: %s\n\n", query))

schema, rowIter, err := c.executeQuery(query)
if err != nil {
c.printError(err)
rl.Terminal.Print(fmt.Sprintf("ERROR: %v\n\n", err))
continue
}

if err := c.printQuery(schema, rowIter, "pretty"); err != nil {
c.printError(err)
rl.Terminal.Print(fmt.Sprintf("ERROR: %v\n\n", err))
continue
}
}

return s.Err()
}

func (c *CmdShell) printError(err error) {
fmt.Printf("ERROR: %v\n\n", err)
}

func scanQueries(data []byte, atEOF bool) (int, []byte, error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexByte(data, ';'); i >= 0 {
// We have a full newline-terminated line.
return i + 1, dropCR(data[0:i]), nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), dropCR(data), nil
}
// Request more data.
return 0, nil, nil
}

// dropCR drops a terminal \r from the data.
func dropCR(data []byte) []byte {
if len(data) > 0 && data[len(data)-1] == '\r' {
return data[0 : len(data)-1]
}
return data
return rl.Close()
}