Skip to content

Commit

Permalink
Test Inquirer on StructureModifier; WIP Commit #3483
Browse files Browse the repository at this point in the history
Still a lot of todos that need to be closed and comments that need to be implemented, as well as finding a solution for the session problem
  • Loading branch information
Nereboss committed Feb 2, 2024
1 parent acad86c commit d57166b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package de.maibornwolff.codecharta.filter.structuremodifier

import com.github.kinquirer.KInquirer
import com.github.kinquirer.components.promptInput
import com.github.kinquirer.components.promptInputNumber
import com.github.kinquirer.components.promptList
import com.varabyte.kotter.foundation.session
import com.varabyte.kotter.runtime.Session
import de.maibornwolff.codecharta.tools.inquirer.myPromptCheckbox
import de.maibornwolff.codecharta.tools.inquirer.myPromptConfirm
import de.maibornwolff.codecharta.tools.inquirer.myPromptInput
Expand All @@ -13,81 +12,91 @@ import de.maibornwolff.codecharta.tools.inquirer.myPromptList
import de.maibornwolff.codecharta.tools.interactiveparser.ParserDialogInterface
import de.maibornwolff.codecharta.util.InputHelper
import java.io.File
import java.math.BigDecimal
import java.nio.file.Paths

class ParserDialog {
companion object : ParserDialogInterface {

override fun collectParserArgs(): List<String> {
var inputFileName: String
var result: List<String> = listOf()

// temporary test for kotter methods TODO: remove
println("before tester")
session {
myPromptInput("Input a test input:")
println("inside tester")
myPromptInput("Input a test input:", Paths.get("").toAbsolutePath().toString() + File.separator + "yourInput.cc.json")
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"))
}
println("after tester")

do {
inputFileName = KInquirer.promptInput(
message = "What is the cc.json file that has to be modified?",
hint = Paths.get("").toAbsolutePath().toString() + File.separator + "yourInput.cc.json"
)
} while (!InputHelper.isInputValidAndNotNull(arrayOf(File(inputFileName)), canInputContainFolders = false))

val selectedAction: String = KInquirer.promptList(
message = "Which action do you want to perform?",
choices = listOf(
StructureModifierAction.PRINT_STRUCTURE.descripton,
StructureModifierAction.SET_ROOT.descripton,
StructureModifierAction.MOVE_NODES.descripton,
StructureModifierAction.REMOVE_NODES.descripton
println("test before session")
session {
println("test inside session")
do {
inputFileName = myPromptInput(
message = "What is the cc.json file that has to be modified?",
hint = Paths.get("").toAbsolutePath().toString() + File.separator + "yourInput.cc.json",
canContainFolders = false
)
)
} while (!InputHelper.isInputValidAndNotNull(arrayOf(File(inputFileName)), canInputContainFolders = false))

val selectedAction: String = myPromptList(
message = "Which action do you want to perform?",
choices = listOf(
StructureModifierAction.PRINT_STRUCTURE.descripton,
StructureModifierAction.SET_ROOT.descripton,
StructureModifierAction.MOVE_NODES.descripton,
StructureModifierAction.REMOVE_NODES.descripton
)
)

return when (selectedAction) {
StructureModifierAction.PRINT_STRUCTURE.descripton -> listOf(inputFileName, *collectPrintArguments())
StructureModifierAction.SET_ROOT.descripton -> listOf(inputFileName, *collectSetRootArguments())
StructureModifierAction.MOVE_NODES.descripton -> listOf(inputFileName, *collectMoveNodesArguments())
StructureModifierAction.REMOVE_NODES.descripton -> listOf(inputFileName, *collectRemoveNodesArguments())
else -> listOf()
result = when (selectedAction) {
StructureModifierAction.PRINT_STRUCTURE.descripton -> listOf(inputFileName, *collectPrintArguments())
StructureModifierAction.SET_ROOT.descripton -> listOf(inputFileName, *collectSetRootArguments())
StructureModifierAction.MOVE_NODES.descripton -> listOf(inputFileName, *collectMoveNodesArguments())
StructureModifierAction.REMOVE_NODES.descripton -> listOf(inputFileName, *collectRemoveNodesArguments())
else -> listOf()
}
}
println("test after session")
return result
}
}
}

private fun collectPrintArguments(): Array<String> {
val printLevels: BigDecimal =
KInquirer.promptInputNumber(message = "How many print levels do you want to print?", default = "0", hint = "0")
return arrayOf("--print-levels=$printLevels")
}
private fun Session.collectPrintArguments(): Array<String> { //diese funktionen als extension vom section scope setzen? können wir sie dann noch private lassen?
val printLevels: String =
myPromptInputNumber(message = "How many print levels do you want to print?", hint = "0")
return arrayOf("--print-levels=$printLevels")
}

private fun collectSetRootArguments(): Array<String> {
val setRoot: String =
KInquirer.promptInput(message = "What path within the project should be extracted as the new root?")
val outputFileName = collectOutputFileName()
return arrayOf("--set-root=$setRoot", "--output-file=$outputFileName")
}
private fun Session.collectSetRootArguments(): Array<String> {
val setRoot: String =
myPromptInput(message = "What path within the project should be extracted as the new root?")
val outputFileName = collectOutputFileName()
return arrayOf("--set-root=$setRoot", "--output-file=$outputFileName")
}

private fun collectMoveNodesArguments(): Array<String> {
val moveFrom: String =
KInquirer.promptInput(message = "What path should be moved (contained children will be moved as well)?")
val moveTo: String =
KInquirer.promptInput(message = "What is the target path to move them?")
val outputFileName = collectOutputFileName()
return arrayOf("--move-from=$moveFrom", "--move-to=$moveTo", "--output-file=$outputFileName")
}
private fun Session.collectMoveNodesArguments(): Array<String> {
val moveFrom: String =
myPromptInput(message = "What path should be moved (contained children will be moved as well)?")
val moveTo: String =
myPromptInput(message = "What is the target path to move them?")
val outputFileName = collectOutputFileName()
return arrayOf("--move-from=$moveFrom", "--move-to=$moveTo", "--output-file=$outputFileName")
}

private fun collectRemoveNodesArguments(): Array<String> {
val remove: String =
KInquirer.promptInput(message = "What are the paths of the nodes to be removed?")
val outputFileName = collectOutputFileName()
return arrayOf("--remove=$remove", "--output-file=$outputFileName")
}
private fun Session.collectRemoveNodesArguments(): Array<String> {
val remove: String =
myPromptInput(message = "What are the paths of the nodes to be removed?")
val outputFileName = collectOutputFileName()
return arrayOf("--remove=$remove", "--output-file=$outputFileName")
}

private fun collectOutputFileName(): String {
return KInquirer.promptInput(message = "What is the name of the output file?")
}
}
private fun collectOutputFileName(): String {
return KInquirer.promptInput(message = "What is the name of the output file?")
}
1 change: 1 addition & 0 deletions analysis/tools/Inquirer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ repositories {

dependencies {
implementation group: 'com.varabyte.kotter', name: 'kotter-jvm', version: '1.1.1'
implementation project(path: ':model')
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.varabyte.kotter.foundation.input.input
import com.varabyte.kotter.foundation.input.onInputChanged
import com.varabyte.kotter.foundation.input.onInputEntered
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.text.black
Expand All @@ -18,23 +17,39 @@ 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
import de.maibornwolff.codecharta.util.InputHelper
import java.io.File

fun Session.myPromptInput(message: String, hint: String = ""): String { // todo, add flag to accept empty input or not
fun Session.myPromptInput(
message: String,
hint: String = "",
allowEmptyInput: Boolean = false,
canContainFolders: Boolean = false,
invalidInputMessage: String = "Input is invalid!"
): String {
var returnValue = ""
var isInputValid = true
section {
bold { green { text("? ") }; textLine(message) }
bold { green { text("? ") }; text(message); red { textLine(if (isInputValid) "" else " $invalidInputMessage") } }
text("> "); input(Completions(hint), initialText = "")
}.runUntilInputEntered {
onInputEntered { returnValue = input; return@onInputEntered }
}.runUntilSignal {
onInputChanged { isInputValid = true }
onInputEntered {
isInputValid = InputHelper.isInputValidAndNotNull(arrayOf(File(input)), canContainFolders) //TODO maybe refactor so the checking function is parameter of this
if ((allowEmptyInput && input.isEmpty()) || (isInputValid && input.isNotEmpty())) {
returnValue = input
signal()
}
}
}
return returnValue
}

fun Session.myPromptInputNumber(
message: String, hint: String = "",
allowEmpty: Boolean = false,
fun Session.myPromptInputNumber( //should this have a 'default' parameter like in Kinquirer?
message: String, hint: String = "", //meaning if nothing is put in then that result gets submitted
allowEmptyInput: Boolean = false, // should floats be allowed or only whole numbers?
invalidInputMessage: String = "Input is invalid!"
): String {
): String { //TODO: decide which return type this should be (KIinquirer does bigint)
var returnValue = ""
var showError = false
section {
Expand All @@ -46,7 +61,7 @@ fun Session.myPromptInputNumber(
}.runUntilSignal {
onInputChanged { showError = false; input = input.filter { it.isDigit() } }
onInputEntered {
if (allowEmpty || input.isNotEmpty()) {
if (allowEmptyInput || input.isNotEmpty()) {
returnValue = input
signal()
} else {
Expand Down Expand Up @@ -75,7 +90,7 @@ fun Session.myPromptConfirm(message: String): Boolean {
return result
}

fun Session.myPromptList(message: String, choices: List<String>, hint: String = ""): String {
fun Session.myPromptList(message: String, choices: List<String>, hint: String = ""): String { // should options disappear after selection?
var result = ""
var selection by liveVarOf(0)
section {
Expand All @@ -99,7 +114,7 @@ fun Session.myPromptList(message: String, choices: List<String>, hint: String =
return result
}

fun Session.myPromptCheckbox(message: String, choices: List<String>, hint: String = ""): List<String> {
fun Session.myPromptCheckbox(message: String, choices: List<String>, hint: String = ""): List<String> { // should options disappear after selection?
var result = listOf<String>()
var pos by liveVarOf(0)
val selectedElems = liveListOf(MutableList(choices.size) { false })
Expand Down

0 comments on commit d57166b

Please sign in to comment.