Skip to content

Commit

Permalink
fix: empty string when new newline at the end of stdin (#96)
Browse files Browse the repository at this point in the history
The code reads the token from `stdin` until newline. If there is no
newline, it counts the content as empty string.

We use `bufio.NewReader` with `reader.ReadString('\n')` which "reads
until the first occurrence of delim in the input". When it can't find a
newline character before the stream ends we return with an error and an
empty string.

Fix: As `reader.ReadString` returns with the content before the error in
all cases, we can return with the content even if we meet an error.

Note: We should check if the error was EOF on caller side everywhere.

Fixes #95

References:
* #95

Signed-off-by: Efertone <[email protected]>
  • Loading branch information
yitsushi authored Oct 20, 2023
1 parent 2486b6b commit ac5543a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion internal/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (t Terminal) Read(prompt string) (string, error) {

text, readErr := reader.ReadString('\n')
if readErr != nil {
return "", fmt.Errorf("error reading from input: %w", readErr)
return text, fmt.Errorf("error reading from input: %w", readErr)
}

text = strings.TrimSpace(text)
Expand Down

0 comments on commit ac5543a

Please sign in to comment.