Skip to content

Commit

Permalink
Merge pull request #299 from uzzz/master
Browse files Browse the repository at this point in the history
Fix ui.Ask to return strings with spaces from stdin
  • Loading branch information
tylerschultz committed Nov 24, 2014
2 parents c7801ad + db9cdc9 commit 6cc8469
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions cf/terminal/ui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terminal

import (
"bufio"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -124,8 +125,13 @@ func (c *terminalUI) Confirm(message string, args ...interface{}) bool {
func (c *terminalUI) Ask(prompt string, args ...interface{}) (answer string) {
c.printer.Println("")
c.printer.Printf(prompt+PromptColor(">")+" ", args...)
fmt.Fscanln(c.stdin, &answer)
return

rd := bufio.NewReader(c.stdin)
line, err := rd.ReadString('\n')
if err == nil {
return strings.TrimSpace(line)
}
return ""
}

func (c *terminalUI) Ok() {
Expand Down
16 changes: 16 additions & 0 deletions cf/terminal/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ var _ = Describe("UI", func() {
})
})

Describe("Asking user for input", func() {
It("allows string with whitespaces", func() {
io_helpers.SimulateStdin("foo bar\n", func(reader io.Reader) {
ui := NewUI(reader, NewTeePrinter())
Expect(ui.Ask("?")).To(Equal("foo bar"))
})
})

It("returns empty string if an error occured while reading string", func() {
io_helpers.SimulateStdin("string without expected delimiter", func(reader io.Reader) {
ui := NewUI(reader, NewTeePrinter())
Expect(ui.Ask("?")).To(Equal(""))
})
})
})

Describe("Confirming user input", func() {
It("treats 'y' as an affirmative confirmation", func() {
io_helpers.SimulateStdin("y\n", func(reader io.Reader) {
Expand Down

0 comments on commit 6cc8469

Please sign in to comment.