Skip to content

Commit

Permalink
Pass the original writer to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
antham committed Dec 15, 2017
1 parent 2320723 commit 6776189
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
10 changes: 7 additions & 3 deletions interfaces.go
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
}
8 changes: 4 additions & 4 deletions prompt_example_customize_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand All @@ -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())
}
Expand All @@ -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")
}
Expand Down
17 changes: 8 additions & 9 deletions prompts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ type StringWithCustomRendererPrompt struct {
currentID string
nextPrompt string
nextPromptOnError string
writer io.Writer
}

func (s *StringWithCustomRendererPrompt) ID() string {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 6776189

Please sign in to comment.