Skip to content

Commit

Permalink
restart and quit
Browse files Browse the repository at this point in the history
  • Loading branch information
pablf committed Jul 5, 2023
1 parent a62054f commit dd258ff
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
10 changes: 6 additions & 4 deletions zio-cli/shared/src/main/scala/zio/cli/CliApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ object CliApp {
}
}
case ShowWizard(command) => {
val fancyName = p(code(self.figFont.render(self.name)))

val fancyName = p(code(self.figFont.render(self.name)))
val header = p(text("WIZARD of ") + text(self.name) + text(self.version) + text(" -- ") + self.summary)
val explanation = p(s"Wizard mode assist you in constructing commands for $name$version")
for {

(for {
parameters <- Wizard(command, config, fancyName + header + explanation).execute
_ <- run(parameters)
} yield ()
} yield ()).catchSome { case Wizard.QuitException() =>
ZIO.unit
}
}

}
Expand Down
47 changes: 35 additions & 12 deletions zio-cli/shared/src/main/scala/zio/cli/Wizard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
}
case None => p(instruction)
}
val help: Span = Span.text("Write ") + Span.code("help") + Span.text(" for more information.")
val help: Span =
Span.text("Write ") +
Span.code("help") +
Span.text(" for more information about this parameter, ") +
Span.code("restart") +
Span.text(" to start again Wizard mode and ") +
Span.code("quit") +
Span.text(" to exit.")
val info: HelpDoc =
if (needHelp) parameter.helpDoc
else
Expand All @@ -56,7 +63,7 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
param: Alternatives,
invalidInput: Option[String] = None,
needHelp: Boolean = false
): UIO[(List[String], Parameter)] = {
): IO[Wizard.WizardException, (List[String], Parameter)] = {

val map = param.getSubparameters

Expand Down Expand Up @@ -91,8 +98,10 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
_ <- printLine(mkColumns(lines.toList).toPlaintext()).orDie
input <- readLine(prompt).orDie
res <- input match {
case "" => chooseParam(param, Some(input), false)
case "help" => chooseParam(param, None, true)
case "" => chooseParam(param, Some(input), false)
case "help" => chooseParam(param, None, true)
case "quit" => ZIO.fail(Wizard.QuitException())
case "restart" => ZIO.fail(Wizard.RestartException())
case _ =>
map.get(input) match {
case Some(command) => ZIO.succeed((List(command._1), command._2))
Expand All @@ -117,14 +126,20 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
/**
* Allows the user enterin the value of a parameter represented by Input trait.
*/
def inputParam(param: Input, invalidInput: Option[String] = None, needHelp: Boolean = false): UIO[List[String]] =
def inputParam(
param: Input,
invalidInput: Option[String] = None,
needHelp: Boolean = false
): IO[Wizard.WizardException, List[String]] =
for {
_ <- printHeader
_ <- printInfo(invalidInput, s"Please, specify the following ${param.tag}.", param, needHelp)
input <- readLine(prompt).orDie
params <- input match {
case "" => inputParam(param, Some(input), false)
case "help" => inputParam(param, None, true)
case "" => inputParam(param, Some(input), false)
case "help" => inputParam(param, None, true)
case "quit" => ZIO.fail(Wizard.QuitException())
case "restart" => ZIO.fail(Wizard.RestartException())
case _ =>
param.isValid(input, conf).catchAll { case _: ValidationError =>
inputParam(param, Some(input), false)
Expand All @@ -135,7 +150,7 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
/**
* Constructs a command interacting with the user.
*/
def generateParams(command: Parameter): UIO[List[String]] =
def generateParams(command: Parameter): IO[Wizard.WizardException, List[String]] =
command match {
case p: Alternatives =>
for {
Expand All @@ -157,7 +172,6 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
*/
def printFinalCommand(params: List[String]) = for {
_ <- printHeader

_ <- printLine(
(p("You may bypass the wizard and execute your command directly with the following options and arguments:")
+ p(" " + params.filter(_ != "").mkString(" "))
Expand All @@ -168,9 +182,18 @@ final case class Wizard(command: Command[_], conf: CliConfig, header: HelpDoc) {
/**
* Entry point for Wizard mode.
*/
def execute: UIO[List[String]] = for {
parameters <- generateParams(command)
_ <- printFinalCommand(parameters).orDie
def execute: IO[Wizard.QuitException, List[String]] = for {
parameters <- generateParams(command).catchAll {
case Wizard.RestartException() => execute
case quit @ Wizard.QuitException() => ZIO.fail(quit)
}
_ <- printFinalCommand(parameters).orDie
} yield parameters.filter(_ != "")

}

object Wizard {
sealed trait WizardException extends Exception
case class RestartException() extends WizardException
case class QuitException() extends WizardException
}

0 comments on commit dd258ff

Please sign in to comment.