Files
gamenight/gamenight-cli/src/flows/list_locations.rs
T

49 lines
1.4 KiB
Rust

use std::fmt::Display;
use async_trait::async_trait;
use gamenight_api_client_rs::apis::default_api::locations_get;
use inquire::{ui::RenderConfig, Select};
use crate::flows::{exit::Exit, view_location::ViewLocation};
use super::*;
#[derive(Clone)]
pub struct ListLocations {
}
impl ListLocations {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl<'a> Flow<'a> for ListLocations {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let locations = locations_get(&state.api_configuration).await?;
let mut flows = locations.into_iter().map(|location| -> Box<dyn Flow + Send> {
Box::new(ViewLocation::new(location.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 ListLocations {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "List all locations")
}
}