nedots 0.1.2

A tool to manage configuration files/(ne)dots.
use dialoguer::{theme::ColorfulTheme, Select};

#[derive(Debug, clap::Args)]
pub(crate) struct RestoreCmd;

impl super::Command for RestoreCmd {
    fn run(
        &self,
        _: &super::nedots::RootCmd,
        config: &crate::config::Config,
    ) -> Result<(), super::CommandError> {
        super::backup::backup(
            &config.sources,
            &config.root.join(super::backup::get_dst()),
            true,
        )?;

        // Read backup directories - filter errors, and return Strings.
        let mut items: Vec<String> = config
            .root
            .join("backups")
            .read_dir()?
            .filter(|e| e.is_ok())
            .map(|e| e.unwrap().path().to_string_lossy().to_string())
            .collect();

        // Remove the last entry - this is the backup we just took. No sense
        // in restoring the backup we just took.
        items.pop();

        // Place the latest backup at the top of the list - when we set default
        // selection to index 0, this is the latest backup.
        items.reverse();

        let selection = Select::with_theme(&ColorfulTheme::default())
            .items(&items)
            .default(0)
            .interact_on_opt(&console::Term::stderr())?;

        match selection {
            Some(index) => println!("User selected item : {}", items[index]),
            None => println!("User did not select anything"),
        }

        Ok(())
    }
}