Skip to content

Commit

Permalink
Move some Terminal methods to extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ajalt committed Sep 7, 2024
1 parent 11efd7b commit 5ae773d
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 134 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- In raw mode on POSIX systems, pressing the escape key once will now immediately return an `Escape` event. [(#193)](https://github.com/ajalt/mordant/issues/193)
- Renamed `TerminalInfo.crClearsLine` to `supportsAnsiCursor`
- Combined all `ColumnWidth` subclasses into a single class with factory methods. If you were using `ColumnWidth.Custom`, you should now use the `ColumnWidth` constructor.
- The following `Terminal` methods are now extensions: `prompt()`, `info()`, `danger()`, `warning()`, `success()`, `muted()`

### Removed
- Removed constructor overloads for `Terminal`. There is now one constructor with all default parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,67 @@ class ConfirmationPrompt<T : Any>(
}
}
}


/**
* Print a [prompt] to the user and return the value they enter.
*
* To customize the prompt behavior, see the [Prompt] class.
*
* @param prompt The message asking for input to show the user
* @param default The value to return if the user enters an empty line, or `null` to require a value
* @param showDefault If true and a [default] is specified, add the default value to the prompt
* @param hideInput If true, the user's input will be treated like a password and hidden from
* the screen. This value will be ignored on platforms where it is not supported.
* @param choices The set of values that the user must choose from.
* @param promptSuffix A string to append after [prompt] when showing the user the prompt
* @param invalidChoiceMessage The message to show the user if [choices] is specified,
* and they enter a value that isn't one of the choices.
*/
fun Terminal.prompt(
prompt: String,
default: String? = null,
showDefault: Boolean = true,
showChoices: Boolean = true,
hideInput: Boolean = false,
choices: Collection<String> = emptyList(),
promptSuffix: String = ": ",
invalidChoiceMessage: String = "Invalid value, choose from ",
): String? = prompt(
prompt,
default,
showDefault,
showChoices,
hideInput,
choices,
promptSuffix,
invalidChoiceMessage
) { ConversionResult.Valid(it) }

inline fun <T> Terminal.prompt(
prompt: String,
default: T? = null,
showDefault: Boolean = true,
showChoices: Boolean = true,
hideInput: Boolean = false,
choices: Collection<T> = emptyList(),
promptSuffix: String = ": ",
invalidChoiceMessage: String = "Invalid value, choose from ",
crossinline convert: (String) -> ConversionResult<T>,
): T? {
return object : Prompt<T>(
prompt = prompt,
terminal = this,
default = default,
showDefault = showDefault,
showChoices = showChoices,
hideInput = hideInput,
choices = choices,
promptSuffix = promptSuffix,
invalidChoiceMessage = invalidChoiceMessage,
) {
override fun convert(input: String): ConversionResult<T> {
return convert(input)
}
}.ask()
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,77 +122,6 @@ class Terminal private constructor(
else -> DisabledTerminalCursor
}

/**
* Print a line styled with the theme's [success][Theme.success] style.
*/
fun success(
// TODO(3.0): make extensions
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.success(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [danger][Theme.danger] style.
*/
fun danger(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.danger(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [warning][Theme.warning] style.
*/
fun warning(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.warning(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [info][Theme.info] style.
*/
fun info(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.info(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [muted][Theme.muted] style.
*/
fun muted(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.muted(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a [message] to the terminal.
*
Expand Down Expand Up @@ -310,69 +239,6 @@ class Terminal private constructor(
return terminalInterface.readLineOrNull(hideInput)
}

inline fun <T> prompt(
prompt: String,
default: T? = null,
showDefault: Boolean = true,
showChoices: Boolean = true,
hideInput: Boolean = false,
choices: Collection<T> = emptyList(),
promptSuffix: String = ": ",
invalidChoiceMessage: String = "Invalid value, choose from ",
crossinline convert: (String) -> ConversionResult<T>,
): T? {
return object : Prompt<T>(
prompt = prompt,
terminal = this,
default = default,
showDefault = showDefault,
showChoices = showChoices,
hideInput = hideInput,
choices = choices,
promptSuffix = promptSuffix,
invalidChoiceMessage = invalidChoiceMessage,
) {
override fun convert(input: String): ConversionResult<T> {
return convert(input)
}
}.ask()
}

/**
* Print a [prompt] to the user and return the value they enter.
*
* To customize the prompt behavior, see the [Prompt] class.
*
* @param prompt The message asking for input to show the user
* @param default The value to return if the user enters an empty line, or `null` to require a value
* @param showDefault If true and a [default] is specified, add the default value to the prompt
* @param hideInput If true, the user's input will be treated like a password and hidden from
* the screen. This value will be ignored on platforms where it is not supported.
* @param choices The set of values that the user must choose from.
* @param promptSuffix A string to append after [prompt] when showing the user the prompt
* @param invalidChoiceMessage The message to show the user if [choices] is specified,
* and they enter a value that isn't one of the choices.
*/
fun prompt(
prompt: String,
default: String? = null,
showDefault: Boolean = true,
showChoices: Boolean = true,
hideInput: Boolean = false,
choices: Collection<String> = emptyList(),
promptSuffix: String = ": ",
invalidChoiceMessage: String = "Invalid value, choose from ",
): String? = prompt(
prompt,
default,
showDefault,
showChoices,
hideInput,
choices,
promptSuffix,
invalidChoiceMessage
) { ConversionResult.Valid(it) }

/**
* Print a [message] to the terminal without parsing or rendering it.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.ajalt.mordant.terminal

import com.github.ajalt.mordant.rendering.OverflowWrap
import com.github.ajalt.mordant.rendering.TextAlign
import com.github.ajalt.mordant.rendering.Whitespace

/**
* Print a line styled with the theme's [danger][Theme.danger] style.
*/
fun Terminal.danger(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.danger(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [warning][Theme.warning] style.
*/
fun Terminal.warning(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.warning(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [info][Theme.info] style.
*/
fun Terminal.info(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.info(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [muted][Theme.muted] style.
*/
fun Terminal.muted(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.muted(message.toString()), whitespace, align, overflowWrap, width, stderr)
}

/**
* Print a line styled with the theme's [success][Theme.success] style.
*/
fun Terminal.success(
message: Any?,
whitespace: Whitespace = Whitespace.PRE,
align: TextAlign = TextAlign.NONE,
overflowWrap: OverflowWrap = OverflowWrap.NORMAL,
width: Int? = null,
stderr: Boolean = false,
) {
println(theme.success(message.toString()), whitespace, align, overflowWrap, width, stderr)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.ajalt.mordant.rendering.TextStyle
import com.github.ajalt.mordant.table.Borders
import com.github.ajalt.mordant.table.table
import com.github.ajalt.mordant.terminal.Terminal
import com.github.ajalt.mordant.terminal.danger
import okio.FileSystem
import okio.Path.Companion.toPath
import okio.SYSTEM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.github.ajalt.mordant.samples
import com.github.ajalt.mordant.input.interactiveMultiSelectList
import com.github.ajalt.mordant.input.interactiveSelectList
import com.github.ajalt.mordant.terminal.Terminal
import com.github.ajalt.mordant.terminal.danger
import com.github.ajalt.mordant.terminal.success


fun main() {
Expand Down

0 comments on commit 5ae773d

Please sign in to comment.