Skip to content

Commit 4f0c9fe

Browse files
ajnavarrosmola
authored andcommitted
cmd: add colors to shell (#101)
Added some colors to the interactive shell, specially message errors.
1 parent b66aa93 commit 4f0c9fe

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

cmd/gitql/shell.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66

77
"github.com/chzyer/readline"
8+
"github.com/fatih/color"
89
)
910

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

29+
blue := color.New(color.FgHiBlue).SprintfFunc()
30+
white := color.New(color.FgWhite).SprintfFunc()
31+
red := color.New(color.FgHiRed).PrintfFunc()
32+
33+
prompt := blue(initPrompt)
34+
mlPrompt := blue(multilinePrompt)
35+
2836
rl, err := readline.NewEx(&readline.Config{
29-
Prompt: initPrompt,
37+
Prompt: prompt,
3038
HistoryFile: "/tmp/gitql-history",
3139
DisableAutoSaveHistory: true,
3240
})
3341
if err != nil {
3442
return err
3543
}
3644

37-
rl.Terminal.Print(fmt.Sprint(`
38-
gitQL SHELL
39-
-----------
40-
You must end your queries with ';'
41-
42-
`))
45+
fmt.Println(" ", white("git")+blue("QL"), "SHELL")
46+
fmt.Println(" -----------")
47+
fmt.Println("You must end your queries with ';'")
48+
fmt.Println("")
4349

4450
var cmds []string
4551
for {
@@ -53,25 +59,25 @@ You must end your queries with ';'
5359
}
5460
cmds = append(cmds, line)
5561
if !strings.HasSuffix(line, ";") {
56-
rl.SetPrompt(multilinePrompt)
62+
rl.SetPrompt(mlPrompt)
5763
continue
5864
}
5965

6066
query := strings.Join(cmds, " ")
6167
cmds = cmds[:0]
62-
rl.SetPrompt(initPrompt)
68+
rl.SetPrompt(prompt)
6369
rl.SaveHistory(query)
6470

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

6773
schema, rowIter, err := c.executeQuery(query)
6874
if err != nil {
69-
rl.Terminal.Print(fmt.Sprintf("ERROR: %v\n\n", err))
75+
red("ERROR: %v\n\n", err)
7076
continue
7177
}
7278

7379
if err := c.printQuery(schema, rowIter, "pretty"); err != nil {
74-
rl.Terminal.Print(fmt.Sprintf("ERROR: %v\n\n", err))
80+
red("ERROR: %v\n\n", err)
7581
continue
7682
}
7783
}

0 commit comments

Comments
 (0)