forked from Roflin/gamenight
49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
use std::fmt::Display;
|
|
|
|
use async_trait::async_trait;
|
|
use gamenight_api_client_rs::apis::default_api::games_get;
|
|
use inquire::{ui::RenderConfig, Select};
|
|
|
|
use crate::flows::{view_game::ViewGame, exit::Exit};
|
|
|
|
use super::*;
|
|
|
|
|
|
#[derive(Clone)]
|
|
pub struct ListGames {
|
|
}
|
|
|
|
impl ListGames {
|
|
pub fn new() -> Self {
|
|
Self {}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl<'a> Flow<'a> for ListGames {
|
|
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
|
|
let games = games_get(&state.api_configuration).await?;
|
|
|
|
let mut flows = games.into_iter().map(|game| -> Box<dyn Flow + Send> {
|
|
Box::new(ViewGame::new(game.into()))
|
|
}).collect::<Vec::<Box::<dyn Flow + Send>>>();
|
|
|
|
flows.push(Box::new(Exit::new()));
|
|
|
|
let choice = Select::new("What would you like to do?", flows)
|
|
.with_help_message("Select the action you want to take or quit the program")
|
|
.with_render_config(RenderConfig {
|
|
option_index_prefix: inquire::ui::IndexPrefix::Simple,
|
|
..Default::default()
|
|
})
|
|
.prompt_skippable()?;
|
|
|
|
self.continue_choice(state, &choice).await
|
|
}
|
|
}
|
|
|
|
impl Display for ListGames {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "List all games")
|
|
}
|
|
} |