diff --git a/interfaces.go b/interfaces.go index 6582498..a220396 100644 --- a/interfaces.go +++ b/interfaces.go @@ -1,5 +1,9 @@ package strumt +import ( + "io" +) + // Prompter defines a generic common prompt. // // ID returns a string id to identify prompter and to let other prompter call it. @@ -45,18 +49,18 @@ type MultilinePrompter interface { // PromptRenderer can be implemented to customize // the way prompt is rendered, PromptString result is given as parameter type PromptRenderer interface { - PrintPrompt(string) + PrintPrompt(io.Writer, string) } // ErrorRenderer can be implemented to customize // the way an error returned by Parse is rendered type ErrorRenderer interface { - PrintError(err error) + PrintError(io.Writer, error) } // SeparatorRenderer can be implemented to customize // the way a prompt is separated from another, default // is to add a new line type SeparatorRenderer interface { - PrintSeparator() + PrintSeparator(io.Writer) } diff --git a/prompt_example_customize_output_test.go b/prompt_example_customize_output_test.go index 9c15ddf..e8c778c 100644 --- a/prompt_example_customize_output_test.go +++ b/prompt_example_customize_output_test.go @@ -61,10 +61,10 @@ func (a *AreYouOkPrompt) NextOnError(err error) string { return "okprompt" } -func (a *AreYouOkPrompt) PrintPrompt(prompt string) { - fmt.Printf("==> %s\n", prompt) +func (a *AreYouOkPrompt) PrintPrompt(w io.Writer, prompt string) { + fmt.Fprintf(w, "==> %s\n", prompt) } -func (a *AreYouOkPrompt) PrintError(err error) { - fmt.Printf("An error occurred : %s\n", err) +func (a *AreYouOkPrompt) PrintError(w io.Writer, err error) { + fmt.Fprintf(w, "An error occurred : %s", err) } diff --git a/prompts.go b/prompts.go index 99e8ee3..c41da05 100644 --- a/prompts.go +++ b/prompts.go @@ -205,7 +205,7 @@ func parseLine(reader *bufio.Reader, prompt LinePrompter) (string, error) { func renderPrompt(writer io.Writer, prompt Prompter) { switch pr := prompt.(type) { case PromptRenderer: - pr.PrintPrompt(prompt.PromptString()) + pr.PrintPrompt(writer, prompt.PromptString()) default: fmt.Fprintf(writer, "%s\n", prompt.PromptString()) } @@ -214,7 +214,7 @@ func renderPrompt(writer io.Writer, prompt Prompter) { func renderError(writer io.Writer, prompt Prompter, err error) { switch pr := prompt.(type) { case ErrorRenderer: - pr.PrintError(err) + pr.PrintError(writer, err) default: fmt.Fprintf(writer, "%s\n", err.Error()) } @@ -223,7 +223,7 @@ func renderError(writer io.Writer, prompt Prompter, err error) { func renderSeparator(writer io.Writer, prompt Prompter) { switch pr := prompt.(type) { case SeparatorRenderer: - pr.PrintSeparator() + pr.PrintSeparator(writer) default: fmt.Fprintf(writer, "\n") } diff --git a/prompts_test.go b/prompts_test.go index 5529a9d..d9c9743 100644 --- a/prompts_test.go +++ b/prompts_test.go @@ -231,7 +231,6 @@ type StringWithCustomRendererPrompt struct { currentID string nextPrompt string nextPromptOnError string - writer io.Writer } func (s *StringWithCustomRendererPrompt) ID() string { @@ -260,26 +259,26 @@ func (s *StringWithCustomRendererPrompt) NextOnError(err error) string { return s.nextPromptOnError } -func (s *StringWithCustomRendererPrompt) PrintPrompt(prompt string) { - fmt.Fprintf(s.writer, "==> %s : \n", prompt) +func (s *StringWithCustomRendererPrompt) PrintPrompt(w io.Writer, prompt string) { + fmt.Fprintf(w, "==> %s : \n", prompt) } -func (s *StringWithCustomRendererPrompt) PrintError(err error) { - fmt.Fprintf(s.writer, "==> Something went wrong : %s\n", err.Error()) +func (s *StringWithCustomRendererPrompt) PrintError(w io.Writer, err error) { + fmt.Fprintf(w, "==> Something went wrong : %s\n", err.Error()) } -func (s *StringWithCustomRendererPrompt) PrintSeparator() { - fmt.Fprintf(s.writer, "\n+++\n") +func (s *StringWithCustomRendererPrompt) PrintSeparator(w io.Writer) { + fmt.Fprintf(w, "\n+++\n") } func TestPromptRunWithCustomRenderer(t *testing.T) { var actualStdout bytes.Buffer - p := NewPromptsFromReaderAndWriter(bytes.NewBufferString("\ntest\n"), ioutil.Discard) + p := NewPromptsFromReaderAndWriter(bytes.NewBufferString("\ntest\n"), &actualStdout) var value string - p.AddLinePrompter(&StringWithCustomRendererPrompt{&value, "Give a value", "test", "", "test", &actualStdout}) + p.AddLinePrompter(&StringWithCustomRendererPrompt{&value, "Give a value", "test", "", "test"}) p.SetFirst("test") p.Run()