xonecas.ioDon't click! Sean Caetano Martin Sign in
cast / src / editor / mod.rs 116 lines3.3 kB Raw
1pub mod assist;
2mod highlight;
3pub mod media;
4pub mod view;
5
6use crate::agent::Agent;
7use crate::app::App;
8use crate::app::tabs::Tab;
9use assist::Assist;
10use rune::{TextEdit, Tx};
11use std::time::Duration;
12
13pub struct Editor {
14    pub stem: String,
15    pub edit: TextEdit,
16    pub saved: String,
17    pub edited_at: f64,
18    pub agent: Agent,
19    pub assist: Assist,
20    pub generating: Option<u64>,
21    pub activity: String,
22}
23
24impl Editor {
25    pub fn dirty(&self) -> bool {
26        self.edit.text != self.saved
27    }
28}
29
30impl App {
31    pub fn title(&self, stem: &str) -> String {
32        self.editor(stem)
33            .and_then(|e| crate::workspace::title::title(&e.edit.text))
34            .or_else(|| self.ws.as_ref()?.titles.get(stem).cloned())
35            .unwrap_or_else(|| stem.to_string())
36    }
37
38    pub fn editor(&self, stem: &str) -> Option<&Editor> {
39        self.editors.iter().find(|e| e.stem == stem)
40    }
41
42    pub fn editor_mut(&mut self, stem: &str) -> Option<&mut Editor> {
43        self.editors.iter_mut().find(|e| e.stem == stem)
44    }
45
46    pub fn open_prompt(&mut self, stem: &str) {
47        if self.editor(stem).is_none() {
48            let text = self
49                .ws
50                .as_ref()
51                .map(|w| w.read_prompt(stem))
52                .unwrap_or_default();
53            let mut edit = TextEdit::new(&text);
54            edit.focused = true;
55            self.editors.push(Editor {
56                stem: stem.into(),
57                edit,
58                saved: text,
59                edited_at: 0.0,
60                agent: self.cfg.default_agent,
61                assist: Assist::Idle,
62                generating: None,
63                activity: String::new(),
64            });
65        }
66        self.open(Tab::Prompt(stem.into()));
67    }
68
69    pub fn save_prompt(&mut self, stem: &str) {
70        let Some(i) = self.editors.iter().position(|e| e.stem == stem) else {
71            return;
72        };
73        let Some(ws) = &mut self.ws else { return };
74        let text = self.editors[i].edit.text.clone();
75        match ws.write_prompt(stem, &text) {
76            Ok(()) => self.editors[i].saved = text,
77            Err(e) => self.notice = Some(format!("could not save {stem}.md: {e}")),
78        }
79    }
80
81    pub fn touch(&mut self, stem: &str, tx: &mut Tx) {
82        let now = tx.clock();
83        if let Some(e) = self.editor_mut(stem) {
84            e.edited_at = now;
85            if matches!(e.assist, Assist::Proposed { .. }) {
86                e.assist = Assist::Idle;
87            }
88        }
89        tx.wake_after(Duration::from_millis(1100));
90    }
91
92    pub(crate) fn autosave(&mut self, clock: f64, tx: &mut Tx) -> bool {
93        let due: Vec<String> = self
94            .editors
95            .iter()
96            .filter(|e| e.dirty() && clock - e.edited_at >= 1.0)
97            .map(|e| e.stem.clone())
98            .collect();
99        for stem in &due {
100            self.save_prompt(stem);
101        }
102        if self.editors.iter().any(|e| e.dirty()) {
103            tx.wake_after(Duration::from_millis(500));
104        }
105        !due.is_empty()
106    }
107
108    pub fn activity(&mut self, stem: &str, line: String) {
109        if let Some(e) = self.editor_mut(stem) {
110            e.activity = line.clone();
111        }
112        if let Some(r) = self.review_mut(stem) {
113            r.activity = line;
114        }
115    }
116}