Skip to content

Commit

Permalink
Add promptCheckbox function #3483
Browse files Browse the repository at this point in the history
  • Loading branch information
Nereboss committed Jan 23, 2024
1 parent ac3c7c0 commit aeef242
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class ParserDialog {

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

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

println(Inquirer.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
@@ -1,5 +1,6 @@
package de.maibornwolff.codecharta.tools.inquirer

import com.varabyte.kotter.foundation.collections.liveListOf
import com.varabyte.kotter.foundation.input.Completions
import com.varabyte.kotter.foundation.input.Keys
import com.varabyte.kotter.foundation.input.input
Expand Down Expand Up @@ -109,5 +110,42 @@ class Inquirer {
}
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()}
}
}
}
}
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 aeef242

Please sign in to comment.