github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/testhelpers/terminal/ui.go (about)

     1  package terminal
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"sync"
     7  	"time"
     8  
     9  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    10  	term "github.com/cloudfoundry/cli/cf/terminal"
    11  	"github.com/codegangsta/cli"
    12  )
    13  
    14  const QuietPanic = "I should not print anything"
    15  
    16  type FakeUI struct {
    17  	Outputs                    []string
    18  	UncapturedOutput           []string
    19  	WarnOutputs                []string
    20  	Prompts                    []string
    21  	PasswordPrompts            []string
    22  	Inputs                     []string
    23  	FailedWithUsage            bool
    24  	FailedWithUsageCommandName string
    25  	PanickedQuietly            bool
    26  	ShowConfigurationCalled    bool
    27  
    28  	sayMutex sync.Mutex
    29  }
    30  
    31  func (ui *FakeUI) PrintPaginator(rows []string, err error) {
    32  	if err != nil {
    33  		ui.Failed(err.Error())
    34  		return
    35  	}
    36  
    37  	for _, row := range rows {
    38  		ui.Say(row)
    39  	}
    40  }
    41  
    42  func (ui *FakeUI) PrintCapturingNoOutput(message string, args ...interface{}) {
    43  	ui.sayMutex.Lock()
    44  	defer ui.sayMutex.Unlock()
    45  
    46  	message = fmt.Sprintf(message, args...)
    47  	ui.UncapturedOutput = append(ui.UncapturedOutput, strings.Split(message, "\n")...)
    48  	return
    49  }
    50  
    51  func (ui *FakeUI) Say(message string, args ...interface{}) {
    52  	ui.sayMutex.Lock()
    53  	defer ui.sayMutex.Unlock()
    54  
    55  	message = fmt.Sprintf(message, args...)
    56  	ui.Outputs = append(ui.Outputs, strings.Split(message, "\n")...)
    57  	return
    58  }
    59  
    60  func (ui *FakeUI) Warn(message string, args ...interface{}) {
    61  	message = fmt.Sprintf(message, args...)
    62  	ui.WarnOutputs = append(ui.WarnOutputs, strings.Split(message, "\n")...)
    63  	ui.Say(message, args...)
    64  	return
    65  }
    66  
    67  func (ui *FakeUI) Ask(prompt string, args ...interface{}) (answer string) {
    68  	ui.Prompts = append(ui.Prompts, fmt.Sprintf(prompt, args...))
    69  	if len(ui.Inputs) == 0 {
    70  		panic("No input provided to Fake UI for prompt: " + fmt.Sprintf(prompt, args...))
    71  	}
    72  
    73  	answer = ui.Inputs[0]
    74  	ui.Inputs = ui.Inputs[1:]
    75  	return
    76  }
    77  
    78  func (ui *FakeUI) ConfirmDelete(modelType, modelName string) bool {
    79  	return ui.Confirm(
    80  		"Really delete the %s %s?%s",
    81  		modelType,
    82  		term.EntityNameColor(modelName),
    83  		term.PromptColor(">"))
    84  }
    85  
    86  func (ui *FakeUI) ConfirmDeleteWithAssociations(modelType, modelName string) bool {
    87  	return ui.ConfirmDelete(modelType, modelName)
    88  }
    89  
    90  func (ui *FakeUI) Confirm(prompt string, args ...interface{}) bool {
    91  	response := ui.Ask(prompt, args...)
    92  	switch strings.ToLower(response) {
    93  	case "y", "yes":
    94  		return true
    95  	}
    96  	return false
    97  }
    98  
    99  func (ui *FakeUI) AskForPassword(prompt string, args ...interface{}) (answer string) {
   100  	ui.PasswordPrompts = append(ui.PasswordPrompts, fmt.Sprintf(prompt, args...))
   101  	if len(ui.Inputs) == 0 {
   102  		println("__________________PANIC__________________")
   103  		println(ui.DumpOutputs())
   104  		println(ui.DumpPrompts())
   105  		println("_________________________________________")
   106  		panic("No input provided to Fake UI for prompt: " + fmt.Sprintf(prompt, args...))
   107  	}
   108  
   109  	answer = ui.Inputs[0]
   110  	ui.Inputs = ui.Inputs[1:]
   111  	return
   112  }
   113  
   114  func (ui *FakeUI) Ok() {
   115  	ui.Say("OK")
   116  }
   117  
   118  func (ui *FakeUI) Failed(message string, args ...interface{}) {
   119  	ui.Say("FAILED")
   120  	ui.Say(message, args...)
   121  	panic(QuietPanic)
   122  	return
   123  }
   124  
   125  func (ui *FakeUI) FailWithUsage(context *cli.Context) {
   126  	ui.FailedWithUsage = true
   127  	ui.FailedWithUsageCommandName = context.Command.Name
   128  	ui.Failed("Incorrect Usage.")
   129  	ui.PanickedQuietly = true
   130  }
   131  
   132  func (ui *FakeUI) PanicQuietly() {
   133  	ui.PanickedQuietly = true
   134  }
   135  
   136  func (ui *FakeUI) DumpWarnOutputs() string {
   137  	return "****************************\n" + strings.Join(ui.WarnOutputs, "\n")
   138  }
   139  
   140  func (ui *FakeUI) DumpOutputs() string {
   141  	return "****************************\n" + strings.Join(ui.Outputs, "\n")
   142  }
   143  
   144  func (ui *FakeUI) DumpPrompts() string {
   145  	return "****************************\n" + strings.Join(ui.Prompts, "\n")
   146  }
   147  
   148  func (ui *FakeUI) ClearOutputs() {
   149  	ui.Outputs = []string{}
   150  }
   151  
   152  func (ui *FakeUI) ShowConfiguration(config core_config.Reader) {
   153  	ui.ShowConfigurationCalled = true
   154  }
   155  
   156  func (ui FakeUI) LoadingIndication() {
   157  }
   158  
   159  func (c FakeUI) Wait(duration time.Duration) {
   160  	time.Sleep(duration)
   161  }
   162  
   163  func (ui *FakeUI) Table(headers []string) term.Table {
   164  	return term.NewTable(ui, headers)
   165  }