Skip to content

Commit

Permalink
Fix multi-line input when stdin is redirected
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
ajalt committed Nov 15, 2018
1 parent 63ff88c commit d1bccd6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### Added
- `.multiple().unique()` modifier for options and arguments.

### Fixed
- Support multi-line input when redirecting stdin

## [1.5.0] - 2018-08-26
### Added
- Ability to use alternate output streams rather than stdin and stdout by setting `Context.console` or by passing a console to `TermUI` functions.
Expand Down
2 changes: 1 addition & 1 deletion clikt/src/main/kotlin/com/github/ajalt/clikt/core/Context.kt
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Context(val parent: Context?,
*
* The default uses [System.in] and [System.out].
*/
var console: CliktConsole = defaultCliktConsole()
var console: CliktConsole = parent?.console ?: defaultCliktConsole()

This comment has been minimized.

Copy link
@JLLeitschuh

JLLeitschuh Nov 20, 2018

👍

}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ class InteractiveCliktConsole(private val console: Console) : CliktConsole {
}

class NonInteractiveCliktConsole : CliktConsole {
private val inReader by lazy { System.`in`.bufferedReader() }

override fun promptForLine(prompt: String, hideInput: Boolean) = try {
print(prompt, false)
System.`in`.bufferedReader().readLine()
inReader.readLine() ?: throw RuntimeException("EOF")
} catch (err: IOException) {
null
throw err
}

override fun print(text: String, error: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ class PromptOptionsTest {

@Test
fun `prompt option`() {
stdin.provideLines("bar")
stdin.provideLines("foo", "bar")

class C : CliktCommand() {
val foo by option().prompt()
val bar by option().prompt()
override fun run() {
foo shouldBe "bar"
foo shouldBe "foo"
bar shouldBe "bar"
}
}
C().parse(emptyArray())
stdout.logWithNormalizedLineSeparator shouldBe "Foo: "
stdout.logWithNormalizedLineSeparator shouldBe "Foo: Bar: "
}

@Test
Expand Down

0 comments on commit d1bccd6

Please sign in to comment.