make cli output pretty, needs more work

This commit is contained in:
siddontang 2014-09-03 17:01:06 +08:00
parent b4c66c7eed
commit 855682ff2b
1 changed files with 12 additions and 14 deletions

View File

@ -65,22 +65,19 @@ func main() {
if cmd == "help" || cmd == "?" { if cmd == "help" || cmd == "?" {
printHelp(cmds) printHelp(cmds)
} else { } else {
if len(cmds) == 2 && strings.ToLower(cmds[0]) == "select" {
if db, _ := strconv.Atoi(cmds[1]); db < 16 && db >= 0 {
*dbn = db
}
}
r, err := c.Do(cmds[0], args...) r, err := c.Do(cmds[0], args...)
if err == nil && strings.ToLower(cmds[0]) == "select" {
*dbn, _ = strconv.Atoi(cmds[1])
}
if err != nil { if err != nil {
fmt.Printf("%s", err.Error()) fmt.Printf("%s", err.Error())
} else { } else {
if cmd == "info" { if cmd == "info" {
printInfo(r.([]byte)) printInfo(r.([]byte))
} else { } else {
printReply(cmd, r) printReply(0, r)
} }
} }
@ -95,7 +92,7 @@ func printInfo(s []byte) {
fmt.Printf("%s", s) fmt.Printf("%s", s)
} }
func printReply(cmd string, reply interface{}) { func printReply(level int, reply interface{}) {
switch reply := reply.(type) { switch reply := reply.(type) {
case int64: case int64:
fmt.Printf("(integer) %d", reply) fmt.Printf("(integer) %d", reply)
@ -108,13 +105,14 @@ func printReply(cmd string, reply interface{}) {
case ledis.Error: case ledis.Error:
fmt.Printf("%s", string(reply)) fmt.Printf("%s", string(reply))
case []interface{}: case []interface{}:
if level > 0 {
//todo for better pretty
fmt.Printf("%q", reply)
return
}
for i, v := range reply { for i, v := range reply {
fmt.Printf("%d) ", i+1) fmt.Printf("%d) ", i+1)
if v == nil { printReply(level+1, v)
fmt.Printf("(nil)")
} else {
fmt.Printf("%q", v)
}
if i != len(reply)-1 { if i != len(reply)-1 {
fmt.Printf("\n") fmt.Printf("\n")
} }