genpac 0.1.0

Sandbox for Gentoo ebuild development using bubblewrap
// Copyright (C) 2023 Gokul Das B
// SPDX-License-Identifier: GPL-3.0-or-later
//! Log initialization and Progress bar instantiation
//!
//! Logger and progress bar have to be managed together, since they may interfere. This is handled
//! by this module.

use super::{ConfigStatus, Globals, GlobalsLogReady, GlobalsNew, NotReady};
use anyhow::Result as AResult;
use indicatif::{MultiProgress, ProgressBar};
use indicatif_log_bridge::LogWrapper;
use simple_logger::SimpleLogger;

impl GlobalsNew {
    pub fn init_logger(self) -> AResult<GlobalsLogReady> {
        let logger = SimpleLogger::new()
            .with_level(self.cli.log_level)
            .with_colors(true);

        let multi = MultiProgress::new();

        LogWrapper::new(multi.clone(), logger).try_init()?;

        Ok(GlobalsLogReady {
            cli: self.cli,
            multi,
            cfg: NotReady,
        })
    }
}

impl<T: ConfigStatus> Globals<MultiProgress, T> {
    pub fn spinner(&self) -> ProgressBar {
        self.multi.add(ProgressBar::new_spinner())
    }
}