zero-cli 1.0.1

A command line tool for Zero Secrets Manager
use crate::common::{env_var::env_var, print_formatted_error::print_formatted_error};
use dialoguer::{console::style, theme::ColorfulTheme, Sort};

/// Displays a paginated list of items with user interaction.
///
/// `pagination_list` renders a paginated list of items on the terminal with the ability for the user
/// to navigate through pages using arrow keys. It utilizes the `dialoguer` crate to handle user input.
///
/// # Arguments
///
/// * `items` - A vector of strings representing items to display in the list.
/// * `prompt` - A prompt message displayed to the user.
/// * `error_message` - An error message to display if there's an issue during interaction.
///
/// # Examples
///
/// ```
/// use your_crate_name::pagination_list;
///
/// let items = vec![
///     "Item 1".to_string(),
///     "Item 2".to_string(),
///     // Add more items...
/// ];
///
/// pagination_list(items, "Choose an item:".to_string(), "Error occurred during selection.");
/// ```
///
/// # Panics
///
/// Panics if parsing the number of items per page fails, printing a formatted error message.
///
pub fn pagination_list(items: Vec<String>, prompt: String, error_message: &str) {
    let theme = ColorfulTheme {
        prompt_prefix: style("".to_string()),
        ..ColorfulTheme::default()
    };

    let items_per_page: usize = match env_var("ITEMS_PER_PAGE").parse() {
        Ok(items_per_page) => items_per_page,
        Err(_) => {
            print_formatted_error("Fail parsing the number of items per page.");
            std::process::exit(1);
        }
    };

    match Sort::with_theme(&theme)
        .with_prompt(prompt)
        .max_length(items_per_page)
        .items(&items)
        .clear(false)
        .report(false)
        .interact_opt()
    {
        Ok(_) => {
            std::process::exit(0);
        }

        Err(_) => {
            print_formatted_error(error_message);
            std::process::exit(1);
        }
    };
}