1#![deny(missing_docs)]
2
3use barley_runtime::prelude::*;
11use std::sync::Arc;
12use tokio::sync::RwLock;
13use colored::*;
14
15pub struct Interface {
21 ctx: Arc<RwLock<Context>>
22}
23
24impl Interface {
25 pub fn new() -> Self {
27 let callbacks = ContextCallbacks {
28 on_action_started: Some(Self::on_action_started),
29 on_action_finished: Some(Self::on_action_finished),
30 on_action_failed: Some(Self::on_action_failed)
31 };
32
33 Self {
34 ctx: Context::new(callbacks)
35 }
36 }
37
38 pub async fn add_action<A: Action + 'static>(&self, action: A) -> ActionObject {
40 self.ctx.clone().add_action(action).await
41 }
42
43 pub async fn update_action(&self, action: ActionObject) {
45 self.ctx.clone().update_action(action).await
46 }
47
48 pub async fn run(&self) -> Result<()> {
50 self.ctx.clone().run().await
51 }
52
53 pub async fn get_output(&self, action: ActionObject) -> Option<ActionOutput> {
61 self.ctx.clone().get_output(action).await
62 }
63
64 pub(crate) fn on_action_started(action: ActionObject) {
65 let display_name = action.display_name();
66
67 if !display_name.is_empty() {
68 println!("{} {}", "[STARTED]".yellow(), display_name);
69 }
70 }
71
72 pub(crate) fn on_action_finished(action: ActionObject) {
73 let display_name = action.display_name();
74
75 if !display_name.is_empty() {
76 println!("{} {}", "[FINISHED]".green(), display_name);
77 }
78 }
79
80 pub(crate) fn on_action_failed(action: ActionObject, _err: &Error) {
81 let display_name = action.display_name();
82
83 if !display_name.is_empty() {
84 println!("{} {}", "[FAILED]".red(), display_name);
85 }
86 }
87}
88
89impl Default for Interface {
90 fn default() -> Self {
91 Self::new()
92 }
93}