1pub mod bus; 2pub mod chrome; 3mod events; 4mod keys; 5pub mod tabs; 6mod view; 7 8use crate::config::Config; 9use crate::editor::Editor; 10use crate::exec::run::Run; 11use crate::settings::Form; 12use crate::setup::Setup; 13use crate::ui::images::Images; 14use crate::vm::{self, Vm}; 15use crate::workflow::model::Workflow; 16use crate::workflow::review::Review; 17use crate::workspace::Workspace; 18use bus::{Bus, Event}; 19use rune::{TextEdit, Tx, Waker}; 20use std::collections::HashMap; 21use std::path::Path; 22use std::rc::Rc; 23use std::sync::mpsc::Receiver; 24use tabs::Tab; 25 26pub use keys::on_key; 27pub use view::view; 28 29pub enum JobKind { 30 Generate(String), 31 Check(String, Workflow), 32 Assist(String), 33 Task(String, usize), 34} 35 36pub struct App { 37 pub cfg: Config, 38 pub bus: Bus, 39 rx: Receiver<Event>, 40 pub images: Option<Rc<Images>>, 41 pub setup: Setup, 42 pub ws: Option<Workspace>, 43 pub vm: Vm, 44 machine: Option<vm::Machine>, 45 pub tabs: Vec<Tab>, 46 pub active: usize, 47 pub editors: Vec<Editor>, 48 pub reviews: Vec<Review>, 49 pub runs: Vec<Run>, 50 pub jobs: HashMap<u64, JobKind>, 51 pub settings: Form, 52 pub notice: Option<String>, 53 tick: f64, 54 scratch: TextEdit, 55} 56 57impl App { 58 pub fn init() -> App { 59 let (bus, rx) = Bus::new(); 60 let cfg = Config::load(); 61 let settings = Form::new(&cfg); 62 App { 63 setup: Setup::new(), 64 cfg, 65 bus, 66 rx, 67 images: None, 68 ws: None, 69 vm: Vm::Off, 70 machine: None, 71 tabs: vec![Tab::Welcome], 72 active: 0, 73 editors: vec![], 74 reviews: vec![], 75 runs: vec![], 76 jobs: HashMap::new(), 77 settings, 78 notice: None, 79 tick: 0.0, 80 scratch: TextEdit::new(""), 81 } 82 } 83 84 pub fn setup(s: &mut App, waker: Waker) { 85 s.images = Some(Rc::new(Images::new(waker.clone()))); 86 s.bus.set_waker(waker); 87 if let Some(dir) = std::env::args() 88 .nth(1) 89 .map(std::path::PathBuf::from) 90 .filter(|d| d.is_dir()) 91 { 92 s.open_dir(&dir); 93 } 94 } 95 96 pub fn poll(s: &mut App, clock: f64, tx: &mut Tx) -> bool { 97 let mut changed = s.autosave(clock, tx); 98 while let Ok(ev) = s.rx.try_recv() { 99 s.dispatch(ev); 100 changed = true; 101 } 102 if s.runs.iter().any(|r| r.busy()) { 103 tx.wake_after(std::time::Duration::from_secs(1)); 104 if clock.floor() != s.tick { 105 s.tick = clock.floor(); 106 changed = true; 107 } 108 } 109 changed 110 } 111 112 pub fn now(&self) -> f64 { 113 std::time::SystemTime::now() 114 .duration_since(std::time::UNIX_EPOCH) 115 .map_or(0.0, |d| d.as_secs_f64()) 116 } 117 118 pub fn fail(&mut self, msg: impl Into<String>) { 119 self.notice = Some(msg.into()); 120 } 121 122 pub fn open_dir(&mut self, dir: &Path) { 123 let Ok(dir) = dir.canonicalize() else { 124 return self.fail("that directory does not exist"); 125 }; 126 self.machine = None; 127 self.editors.clear(); 128 self.reviews.clear(); 129 self.runs.clear(); 130 self.jobs.clear(); 131 self.tabs.retain(|t| *t == Tab::Settings); 132 self.cfg.touch_recent(&dir); 133 let mut ws = Workspace::open(&dir); 134 let first = ws 135 .prompts 136 .first() 137 .cloned() 138 .unwrap_or_else(|| ws.new_prompt()); 139 self.ws = Some(ws); 140 self.boot_vm(); 141 self.open_prompt(&first); 142 } 143 144 pub fn boot_vm(&mut self) { 145 let Some((dir, state)) = self.ws.as_ref().map(|w| (w.dir.clone(), w.state.clone())) else { 146 return; 147 }; 148 if !vm::files::is_baked() || rune::tooling::headless_run().is_some() { 149 return; 150 } 151 self.machine = None; 152 match vm::boot(&self.bus, &dir, &state) { 153 Ok(m) => { 154 self.machine = Some(m); 155 self.vm = Vm::Booting; 156 } 157 Err(e) => self.vm = Vm::Failed(e.to_string()), 158 } 159 } 160 161 pub fn port(&mut self) -> Option<u16> { 162 let port = self.vm.port(); 163 if port.is_none() { 164 self.fail("the agent VM is not running yet"); 165 } 166 port 167 } 168 169 pub fn edit_of(&mut self, stem: &str) -> &mut TextEdit { 170 match self.editors.iter().position(|e| e.stem == stem) { 171 Some(i) => &mut self.editors[i].edit, 172 None => &mut self.scratch, 173 } 174 } 175 176 pub fn drop_machine(&mut self) { 177 self.machine = None; 178 } 179 180 pub fn scratch(&mut self) -> &mut TextEdit { 181 &mut self.scratch 182 } 183}