Interactive prompts made easy!
repositories {
mavenCentral()
}
dependencies {
implementation("xyz.davidsimon:interakt:0.3.0")
}
<dependency>
<groupId>xyz.davidsimon</groupId>
<artifactId>interakt</artifactId>
<version>0.3.0</version>
</dependency>
To construct and run an interactive prompt, you need to:
- Obtain a
Prompt
instance - Add
PromptField
s to the prompt - Execute the prompt
Using a few convenience methods, we can achieve all this in just a few lines of code:
prompt {
text("foo:", default = "default value")
integer("bar:", default = 42)
singleList("baz:", listOf("1", "2", "3"), true)
list("multi:", listOf(
ListField.Choice("one", 1),
ListField.Choice("two", 2),
ListField.Choice("three", 3)
))
println("Result:")
for((key, value) in execute()) {
println("${key.promptMessage} $value")
}
}
Base class for all other fields
Options:
Parameter | Type | Description |
---|---|---|
promptMessage | String |
Message to display before user input |
shouldPrompt | (PromptResult, T?) -> Boolean |
Controls whether the field should be prompted. Receives the answers entered so far and the current field's instance |
default | (PromptResult, T?) -> T? |
Provides a default value that will be pre-filled |
Base class for fields that have a complex rendering function. Has event handlers that receive the current RenderState
.
Prompts user for simple text input
Prompts the user for text input and masks it with *
characters
Prompts user for integer input
Prompts the user to choose items from a list. The displayed name and the actual value of the items may differ.
Options:
Parameter | Type | Description |
---|---|---|
choices | (PromptResult) -> List<Choice<T>> |
Returns the available choices |
Prompts the user to choose ONE item from a list.
Prompts the user to choose from a list of text items. Optionally the user may enter a custom value.
Options:
Parameter | Type | Description |
---|---|---|
choices | (tr: PromptResult) -> List<Choice<String>> |
Returns the available choices |
allowCustom | Boolean |
Allow the user to enter a custom value. If true an additional choice will be added with an empty string value |
customPromptMessage | String |
Message to display when user enter custom value |
interakt is built using JLine