use inquire::{Confirm, Select, Text, error::InquireResult, validator::Validation};
use std::fmt::{self, Display, Formatter};
use std::path::PathBuf;
use crate::choices::{Choices, Kind, is_valid_name, project_path, title_from_name};
pub(crate) const NAME_RULE: &str = "Use ASCII letters, digits, - and _, not starting with a digit.";
#[derive(Clone, Copy)]
enum KindItem {
Empty,
ThreeD,
TwoD,
}
impl Display for KindItem {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let label = match self {
KindItem::Empty => "Empty",
KindItem::ThreeD => "3D",
KindItem::TwoD => "2D (coming soon)",
};
write!(f, "{label}")
}
}
#[derive(Clone, Copy)]
enum MultiplayerItem {
No,
Yes,
}
impl Display for MultiplayerItem {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let label = match self {
MultiplayerItem::No => "No",
MultiplayerItem::Yes => "Yes (coming soon)",
};
write!(f, "{label}")
}
}
pub(crate) fn ask_choices(name: Option<String>, folder: Option<PathBuf>) -> InquireResult<Choices> {
let name = match name {
Some(name) => name,
None => ask_name()?,
};
let folder = match folder {
Some(folder) => folder,
None => ask_folder()?,
};
let title = ask_title(&name)?;
let kind = ask_kind()?;
ask_multiplayer()?;
Ok(Choices {
name,
title,
folder,
kind,
})
}
fn ask_name() -> InquireResult<String> {
Text::new("Project name:")
.with_default("bevy-template")
.with_validator(|input: &str| {
if is_valid_name(input) {
Ok(Validation::Valid)
} else {
Ok(Validation::Invalid(NAME_RULE.into()))
}
})
.prompt()
}
fn ask_folder() -> InquireResult<PathBuf> {
Text::new("Where to create it:")
.with_default(".")
.prompt()
.map(PathBuf::from)
}
fn ask_title(name: &str) -> InquireResult<String> {
let default = title_from_name(name);
Text::new("Game title:").with_default(&default).prompt()
}
fn ask_kind() -> InquireResult<Kind> {
loop {
let item = Select::new(
"Kind:",
vec![KindItem::Empty, KindItem::ThreeD, KindItem::TwoD],
)
.prompt()?;
match item {
KindItem::Empty => return Ok(Kind::Empty),
KindItem::ThreeD => return Ok(Kind::ThreeD),
KindItem::TwoD => println!("2D is coming soon. Pick Empty or 3D."),
}
}
}
fn ask_multiplayer() -> InquireResult<()> {
loop {
let item = Select::new(
"Multiplayer:",
vec![MultiplayerItem::No, MultiplayerItem::Yes],
)
.prompt()?;
match item {
MultiplayerItem::No => return Ok(()),
MultiplayerItem::Yes => println!("Multiplayer is coming soon. Pick No."),
}
}
}
pub(crate) fn ask_confirmation(choices: &Choices) -> InquireResult<bool> {
let path = project_path(&choices.folder, &choices.name);
let message = format!("Create {} in {}?", choices.name, path.display());
Confirm::new(&message).with_default(true).prompt()
}