matchmaker/preview/
view.rs

1use ratatui::text::{Line, Text};
2use std::sync::{Arc, Mutex};
3use std::sync::atomic::{AtomicBool, Ordering};
4
5use super::AppendOnly;
6
7// Images?
8#[derive(Debug)]
9pub struct Preview {
10    lines: AppendOnly<Line<'static>>,
11    string: Arc<Mutex<Option<Text<'static>>>>, /// Overrides lines when present
12    changed: Arc<AtomicBool>,
13}
14
15impl Preview {
16    pub fn results(&self) -> Text<'_> {
17        if let Some(s) = self.string.lock().unwrap().as_ref() {
18            s.clone()
19        } else {
20            let output = self.lines.read().unwrap(); // acquire read lock
21            Text::from_iter(output.iter().map(|(_, line)| line.clone()))
22        }
23    }
24
25    pub fn len(&self) -> usize {
26        if let Some(s) = self.string.lock().unwrap().as_ref() {
27            s.height()
28        } else {
29            let output = self.lines.read().unwrap();
30            output.iter().count()
31        }
32    }
33
34    pub fn is_empty(&self) -> bool {
35        if let Some(s) = self.string.lock().unwrap().as_ref() {
36            s.height() == 0
37        } else {
38            let output = self.lines.read().unwrap();
39            output.iter().next().is_none()
40        }
41    }
42
43    pub fn changed(&self) -> bool {
44        self.changed.swap(false, Ordering::Relaxed)
45    }
46
47    pub fn new(lines: AppendOnly<Line<'static>>, string: Arc<Mutex<Option<Text<'static>>>>, changed: Arc<AtomicBool>) -> Self {
48        Self { lines, string, changed }
49    }
50}