From 855682ff2be7d4fc21eb4c34a6397ac3ab131463 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 3 Sep 2014 17:01:06 +0800 Subject: [PATCH] make cli output pretty, needs more work --- cmd/ledis-cli/main.go | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/cmd/ledis-cli/main.go b/cmd/ledis-cli/main.go index 70f0b93..be509f3 100644 --- a/cmd/ledis-cli/main.go +++ b/cmd/ledis-cli/main.go @@ -65,22 +65,19 @@ func main() { if cmd == "help" || cmd == "?" { printHelp(cmds) } 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...) + if err == nil && strings.ToLower(cmds[0]) == "select" { + *dbn, _ = strconv.Atoi(cmds[1]) + } + if err != nil { fmt.Printf("%s", err.Error()) } else { if cmd == "info" { printInfo(r.([]byte)) } else { - printReply(cmd, r) + printReply(0, r) } } @@ -95,7 +92,7 @@ func printInfo(s []byte) { fmt.Printf("%s", s) } -func printReply(cmd string, reply interface{}) { +func printReply(level int, reply interface{}) { switch reply := reply.(type) { case int64: fmt.Printf("(integer) %d", reply) @@ -108,13 +105,14 @@ func printReply(cmd string, reply interface{}) { case ledis.Error: fmt.Printf("%s", string(reply)) case []interface{}: + if level > 0 { + //todo for better pretty + fmt.Printf("%q", reply) + return + } for i, v := range reply { fmt.Printf("%d) ", i+1) - if v == nil { - fmt.Printf("(nil)") - } else { - fmt.Printf("%q", v) - } + printReply(level+1, v) if i != len(reply)-1 { fmt.Printf("\n") }