From 17747c72db9bfe6b9d1df6caa7a8e913d4087f95 Mon Sep 17 00:00:00 2001
From: Tamer Tas <contact@tmrts.com>
Date: Thu, 12 Jan 2017 20:15:53 +0300
Subject: [PATCH] pkg/util/tlog: pretty print multiple choice prompts

---
 pkg/prompt/prompt.go      |  2 +-
 pkg/prompt/prompt_test.go |  2 +-
 pkg/util/tlog/log.go      | 22 +++++++++++++++++++---
 3 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go
index 0e1058e..711ad3b 100644
--- a/pkg/prompt/prompt.go
+++ b/pkg/prompt/prompt.go
@@ -68,7 +68,7 @@ func (p boolPrompt) EvaluateChoice(c string) (interface{}, error) {
 type multipleChoicePrompt []string
 
 func (p multipleChoicePrompt) PromptMessage(name string) string {
-	return fmt.Sprintf("Please choose an index for %q", name)
+	return fmt.Sprintf("Please choose an option for %q", name)
 }
 
 func (p multipleChoicePrompt) EvaluateChoice(c string) (interface{}, error) {
diff --git a/pkg/prompt/prompt_test.go b/pkg/prompt/prompt_test.go
index c24e406..3e3b93c 100644
--- a/pkg/prompt/prompt_test.go
+++ b/pkg/prompt/prompt_test.go
@@ -90,7 +90,7 @@ func TestMultipleChoicePromptFunc(t *testing.T) {
 
 	msg := slicePrompt.PromptMessage(name)
 
-	expectedPromptMsg := "Please choose an index for \"fieldName\""
+	expectedPromptMsg := "Please choose an option for \"fieldName\""
 	if msg != expectedPromptMsg {
 		t.Errorf("slicePrompt(%q).PromptMessage(%q) expected %q got %q", defval, name, expectedPromptMsg, msg)
 	}
diff --git a/pkg/util/tlog/log.go b/pkg/util/tlog/log.go
index fd0f450..1b03f25 100644
--- a/pkg/util/tlog/log.go
+++ b/pkg/util/tlog/log.go
@@ -135,11 +135,27 @@ func Fatal(msg string) {
 
 // Prompt outputs the given message as a question along with a default value.
 func Prompt(msg string, defval interface{}) {
-	fmt.Print(strings.Join([]string{
+	tokens := []string{
 		color.New(color.FgBlue).SprintFunc()("[" + QuestionMark + "]"),
 		color.New(color.Bold, color.FgWhite).SprintFunc()(msg),
-		color.New(color.FgBlue).SprintFunc()(fmt.Sprintf("[default: %#v]: ", defval)),
-	}, " "))
+	}
+
+	// TODO refactor & eliminate duplication
+	switch val := defval.(type) {
+	case []interface{}:
+		tokens = append(tokens, "\n")
+		for i, v := range val {
+			tokens = append(tokens, color.New(color.Bold, color.FgWhite).SprintFunc()(fmt.Sprintf("   %v - %#v\n", i+1, v)))
+		}
+
+		tokens = append(tokens, color.New(color.Bold, color.FgWhite).SprintFunc()(fmt.Sprintf("   Choose from %v..%v", 1, len(val))))
+
+		tokens = append(tokens, color.New(color.Bold, color.FgBlue).SprintFunc()(fmt.Sprintf("[default: %v]: ", 1)))
+	default:
+		tokens = append(tokens, color.New(color.Bold, color.FgBlue).SprintFunc()(fmt.Sprintf("[default: %#v]: ", defval)))
+	}
+
+	fmt.Print(strings.Join(tokens, " "))
 }
 
 // TODO use dependency injection wrapper for fmt.Print usage in the code base
-- 
GitLab