Skip to content

cmd: add colors to shell #101

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
Feb 9, 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
28 changes: 17 additions & 11 deletions cmd/gitql/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"

"github.com/chzyer/readline"
"github.com/fatih/color"
)

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

blue := color.New(color.FgHiBlue).SprintfFunc()
white := color.New(color.FgWhite).SprintfFunc()
red := color.New(color.FgHiRed).PrintfFunc()

prompt := blue(initPrompt)
mlPrompt := blue(multilinePrompt)

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

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

`))
fmt.Println(" ", white("git")+blue("QL"), "SHELL")
fmt.Println(" -----------")
fmt.Println("You must end your queries with ';'")
fmt.Println("")

var cmds []string
for {
Expand All @@ -53,25 +59,25 @@ You must end your queries with ';'
}
cmds = append(cmds, line)
if !strings.HasSuffix(line, ";") {
rl.SetPrompt(multilinePrompt)
rl.SetPrompt(mlPrompt)
continue
}

query := strings.Join(cmds, " ")
cmds = cmds[:0]
rl.SetPrompt(initPrompt)
rl.SetPrompt(prompt)
rl.SaveHistory(query)

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

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

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