#![deny(missing_docs)]
use barley_runtime::prelude::*;
use std::sync::Arc;
use tokio::sync::RwLock;
use colored::*;
pub struct Interface {
ctx: Arc<RwLock<Context>>
}
impl Interface {
pub fn new() -> Self {
let callbacks = ContextCallbacks {
on_action_started: Some(Self::on_action_started),
on_action_finished: Some(Self::on_action_finished),
on_action_failed: Some(Self::on_action_failed)
};
Self {
ctx: Context::new(callbacks)
}
}
pub async fn add_action<A: Action + 'static>(&self, action: A) -> ActionObject {
self.ctx.clone().add_action(action).await
}
pub async fn update_action(&self, action: ActionObject) {
self.ctx.clone().update_action(action).await
}
pub async fn run(&self) -> Result<()> {
self.ctx.clone().run().await
}
pub async fn get_output(&self, action: ActionObject) -> Option<ActionOutput> {
self.ctx.clone().get_output(action).await
}
pub(crate) fn on_action_started(action: ActionObject) {
let display_name = action.display_name();
if !display_name.is_empty() {
println!("{} {}", "[STARTED]".yellow(), display_name);
}
}
pub(crate) fn on_action_finished(action: ActionObject) {
let display_name = action.display_name();
if !display_name.is_empty() {
println!("{} {}", "[FINISHED]".green(), display_name);
}
}
pub(crate) fn on_action_failed(action: ActionObject, _err: &Error) {
let display_name = action.display_name();
if !display_name.is_empty() {
println!("{} {}", "[FAILED]".red(), display_name);
}
}
}
impl Default for Interface {
fn default() -> Self {
Self::new()
}
}