Skip to content

Commit

Permalink
Refactor inquirer functions #3483
Browse files Browse the repository at this point in the history
Class is no longer needed as all functions extend the Session class from Kotter and are applicable in the correct scope
  • Loading branch information
Nereboss committed Jan 24, 2024
1 parent aeef242 commit acad86c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 129 deletions.
1 change: 1 addition & 0 deletions analysis/filter/StructureModifier/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {
implementation group: 'info.picocli', name: 'picocli', version: picocli_version
implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlin_logging_version
implementation group: 'com.github.kotlin-inquirer', name: 'kotlin-inquirer', version: kotlin_inquirer_version
implementation group: 'com.varabyte.kotter', name: 'kotter-jvm', version: '1.1.1'

testImplementation group: 'org.jetbrains.kotlin', name: 'kotlin-test', version: kotlin_version
testImplementation project(path: ':model', configuration: 'testOutput')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import com.github.kinquirer.KInquirer
import com.github.kinquirer.components.promptInput
import com.github.kinquirer.components.promptInputNumber
import com.github.kinquirer.components.promptList
import de.maibornwolff.codecharta.tools.inquirer.Inquirer
import com.varabyte.kotter.foundation.session
import de.maibornwolff.codecharta.tools.inquirer.myPromptCheckbox
import de.maibornwolff.codecharta.tools.inquirer.myPromptConfirm
import de.maibornwolff.codecharta.tools.inquirer.myPromptInput
import de.maibornwolff.codecharta.tools.inquirer.myPromptInputNumber
import de.maibornwolff.codecharta.tools.inquirer.myPromptList
import de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterface
import de.maibornwolff.codecharta.util.InputHelper
import java.io.File
Expand All @@ -17,20 +22,14 @@ class ParserDialog {
override fun collectParserArgs(): List<String> {
var inputFileName: String

//temporary test for kotter methods TODO: refactor methods to no longer include session tags

// println(Inquirer.myPromptInput(
// message = "Input a test input",
// hint = Paths.get("").toAbsolutePath().toString() + File.separator + "yourInput.cc.json"
// ))
//
// println(Inquirer.myPromptInputNumber("input a test number:", "42"))

//println(Inquirer.myPromptConfirm("Confirm the test?"))

//println(Inquirer.myPromptList("select any", listOf("a", "b", "c", "1", "2", "3")))

println(Inquirer.myPromptCheckbox("select any", listOf("a", "b", "c", "1", "2", "3")))
// temporary test for kotter methods TODO: remove
session {
myPromptInput("Input a test input:")
myPromptInputNumber("input a test number:", "42")
myPromptConfirm("Confirm the test?")
myPromptList("select any", listOf("a", "b", "c", "1", "2", "3"))
myPromptCheckbox("select any", listOf("a", "b", "c", "1", "2", "3"))
}

do {
inputFileName = KInquirer.promptInput(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,142 +10,126 @@ import com.varabyte.kotter.foundation.input.onKeyPressed
import com.varabyte.kotter.foundation.input.runUntilInputEntered
import com.varabyte.kotter.foundation.liveVarOf
import com.varabyte.kotter.foundation.runUntilSignal
import com.varabyte.kotter.foundation.session
import com.varabyte.kotter.foundation.text.black
import com.varabyte.kotter.foundation.text.bold
import com.varabyte.kotter.foundation.text.cyan
import com.varabyte.kotter.foundation.text.green
import com.varabyte.kotter.foundation.text.red
import com.varabyte.kotter.foundation.text.text
import com.varabyte.kotter.foundation.text.textLine
import com.varabyte.kotter.runtime.Session

class Inquirer {
companion object {
fun myPromptInput(message: String, hint: String = ""): String { //todo, add flag to accept empty input or not
var returnValue = ""
session {
section {
bold { green { text("? ") }; textLine(message) }
text("> "); input(Completions(hint), initialText = "")
}.runUntilInputEntered {
onInputEntered { returnValue = input; return@onInputEntered }
}
fun Session.myPromptInput(message: String, hint: String = ""): String { // todo, add flag to accept empty input or not
var returnValue = ""
section {
bold { green { text("? ") }; textLine(message) }
text("> "); input(Completions(hint), initialText = "")
}.runUntilInputEntered {
onInputEntered { returnValue = input; return@onInputEntered }
}
return returnValue
}

fun Session.myPromptInputNumber(
message: String, hint: String = "",
allowEmpty: Boolean = false,
invalidInputMessage: String = "Input is invalid!"
): String {
var returnValue = ""
var showError = false
section {
bold {
green { text("? ") }; text(message)
if (showError) red { textLine(" $invalidInputMessage") } else textLine("")
}
text("> "); input(Completions(hint), initialText = "")
}.runUntilSignal {
onInputChanged { showError = false; input = input.filter { it.isDigit() } }
onInputEntered {
if (allowEmpty || input.isNotEmpty()) {
returnValue = input
signal()
} else {
showError = true
}
return returnValue
}
}
return returnValue
}

fun myPromptInputNumber(
message: String, hint: String = "",
allowEmpty: Boolean = false,
invalidInputMessage: String = "Input is invalid!"
): String {
var returnValue = ""
var showError = false
session {
section {
bold {
green { text("? ") }; text(message)
if (showError) red { textLine(" $invalidInputMessage") } else textLine("")
}
text("> "); input(Completions(hint), initialText = "")
}.runUntilSignal {
onInputChanged { showError = false; input = input.filter { it.isDigit() } }
onInputEntered {
if (allowEmpty || input.isNotEmpty()) {
returnValue = input
signal()
}
else {
showError = true
}
}
}
fun Session.myPromptConfirm(message: String): Boolean {
var result = true
var res by liveVarOf(true)
section {
green { text("? ") }; text(message)
if (res) textLine(" [Yes] No ") else textLine(" Yes [No]")
}.runUntilSignal {
onKeyPressed {
when (key) {
Keys.LEFT -> res = true
Keys.RIGHT -> res = false
Keys.ENTER -> { result = res; signal() }
}
return returnValue
}
}
return result
}

fun myPromptConfirm(message: String): Boolean {
var result = true
session {
var res by liveVarOf(true)
section {
green { text("? ") }; text(message)
if (res) textLine(" [Yes] No ") else textLine(" Yes [No]")
}.runUntilSignal{
onKeyPressed {
when(key) {
Keys.LEFT -> res = true
Keys.RIGHT -> res = false
Keys.ENTER -> { result = res; signal() }
}
}
}
fun Session.myPromptList(message: String, choices: List<String>, hint: String = ""): String {
var result = ""
var selection by liveVarOf(0)
section {
green { text("? ") }; text(message); black(isBright = true) { textLine(" $hint") }
for (i in choices.indices) {
if (i == selection) {
cyan(isBright = true) { text("") }; cyan { textLine(choices[i]) }
} else {
textLine(" ${choices[i]}")
}
return result
}

fun myPromptList(message: String, choices: List<String>, hint: String = ""): String {
var result = ""
session {
var selection by liveVarOf(0)
section {
green { text("? ") }; text(message); black(isBright = true) { textLine(" $hint") }
for (i in choices.indices) {
if (i == selection) {
cyan(isBright = true) { text("") }; cyan { textLine(choices[i]) }
}
else {
textLine(" ${choices[i]}")
}
}
}.runUntilSignal {
onKeyPressed {
when(key) {
Keys.UP -> if (selection>0) {selection -= 1}
Keys.DOWN -> if (selection<choices.size-1) {selection +=1}
Keys.ENTER -> {result = choices[selection]; signal()}
}
}
}
}.runUntilSignal {
onKeyPressed {
when (key) {
Keys.UP -> if (selection> 0) { selection -= 1 }
Keys.DOWN -> if (selection <choices.size - 1) { selection += 1 }
Keys.ENTER -> { result = choices[selection]; signal() }
}
return result
}
}
return result
}

fun myPromptCheckbox(message: String, choices: List<String>, hint: String = ""): List<String> {
var result = listOf<String>()
session {
var pos by liveVarOf(0)
val selectedElems = liveListOf(MutableList(choices.size) {false})
section {
green { text("? ") }; text(message); black(isBright = true) { textLine(" $hint") }
for (i in choices.indices) {
cyan(isBright = true) { text(if (i == pos) "" else " ") }
if (selectedElems[i]) {
green { text("") }; cyan { textLine(choices[i]) }
} else {
textLine("${choices[i]}")
}
}
}.runUntilSignal {
onKeyPressed {
when(key) {
Keys.UP -> if (pos>0) {pos -= 1}
Keys.DOWN -> if (pos<choices.size-1) {pos +=1}
Keys.SPACE -> selectedElems[pos] = !selectedElems[pos]
Keys.ENTER -> {result = getSelectedElems(choices, selectedElems); signal()}
}
}
}
fun Session.myPromptCheckbox(message: String, choices: List<String>, hint: String = ""): List<String> {
var result = listOf<String>()
var pos by liveVarOf(0)
val selectedElems = liveListOf(MutableList(choices.size) { false })
section {
green { text("? ") }; text(message); black(isBright = true) { textLine(" $hint") }
for (i in choices.indices) {
cyan(isBright = true) { text(if (i == pos) "" else " ") }
if (selectedElems[i]) {
green { text("") }; cyan { textLine(choices[i]) }
} else {
textLine("${choices[i]}")
}
return result
}

private fun getSelectedElems(choices: List<String>, selection: List<Boolean>): List<String> {
val result = mutableListOf<String>()
for (i in choices.indices) {
if (selection[i]) result.add(choices[i])
}.runUntilSignal {
onKeyPressed {
when (key) {
Keys.UP -> if (pos> 0) { pos -= 1 }
Keys.DOWN -> if (pos <choices.size - 1) { pos += 1 }
Keys.SPACE -> selectedElems[pos] = !selectedElems[pos]
Keys.ENTER -> { result = getSelectedElems(choices, selectedElems); signal() }
}
return result
}
}
return result
}

private fun getSelectedElems(choices: List<String>, selection: List<Boolean>): List<String> {
val result = mutableListOf<String>()
for (i in choices.indices) {
if (selection[i]) result.add(choices[i])
}
return result
}

0 comments on commit acad86c

Please sign in to comment.