Skip to content

Commit b930191

Browse files
committed
Handle space
1 parent efde19d commit b930191

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

input.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Package input reads user input at the console. http://github.com/tcnksm/go-input
1616
package input
1717

1818
import (
19+
"bufio"
1920
"errors"
2021
"io"
2122
"os"
@@ -54,6 +55,8 @@ type UI struct {
5455
mask bool
5556
maskVal string
5657

58+
bReader *bufio.Reader
59+
5760
once sync.Once
5861
}
5962

@@ -75,6 +78,10 @@ func (i *UI) setDefault() {
7578
if i.Reader == nil {
7679
i.Reader = defaultReader
7780
}
81+
82+
if i.bReader == nil {
83+
i.bReader = bufio.NewReader(i.Reader)
84+
}
7885
}
7986

8087
// ValidateFunc is function to validate the user input.
@@ -163,8 +170,7 @@ func (o *Options) readOpts() *readOptions {
163170
}
164171
}
165172

166-
// maskString is used to mask string which should not be displayed
167-
// directly like auth token
173+
// maskString is used to mask string which should not be displayed.
168174
func maskString(s string) string {
169175
if len(s) < 3 {
170176
return "*******"

read.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"os"
77
"os/signal"
8+
"strings"
89
)
910

1011
// readOptions is option for read func
@@ -40,10 +41,12 @@ func (i *UI) read(opts *readOptions) (string, error) {
4041
i.mask, i.maskVal = opts.mask, opts.maskVal
4142
resultStr, resultErr = i.rawRead(f)
4243
} else {
43-
_, err := fmt.Fscanln(i.Reader, &resultStr)
44-
if err != nil && err.Error() != "unexpected newline" {
44+
line, err := i.bReader.ReadString('\n')
45+
if err != nil && err != io.EOF {
4546
resultErr = fmt.Errorf("failed to read the input: %s", err)
4647
}
48+
49+
resultStr = strings.TrimSuffix(line, "\n")
4750
}
4851
}()
4952

read_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func TestRead(t *testing.T) {
2222
expect: "passw0rd",
2323
},
2424

25+
{
26+
opts: &readOptions{
27+
mask: false,
28+
maskVal: "",
29+
},
30+
userInput: bytes.NewBufferString("taichi nakashima"),
31+
expect: "taichi nakashima",
32+
},
33+
2534
// No good way to test masking...
2635
}
2736

0 commit comments

Comments
 (0)