matchmaker/preview/
view.rs1use cli_boilerplate_automation::bait::ResultExt;
2use cli_boilerplate_automation::unwrap;
3use ratatui::text::{Line, Text};
4use std::sync::atomic::{AtomicBool, Ordering};
5use std::sync::{Arc, Mutex};
6
7use super::AppendOnly;
8
9#[derive(Debug)]
11pub struct Preview {
12 lines: AppendOnly<Line<'static>>,
13 string: Arc<Mutex<Option<Text<'static>>>>,
14 changed: Arc<AtomicBool>,
16}
17
18impl Preview {
19 pub fn results(&self) -> Text<'_> {
20 if let Some(s) = unwrap!(self.string.lock().prefix("Previewer panicked")._elog()).as_ref() {
21 s.clone()
22 } else {
23 let output = unwrap!(self.lines.read().prefix("Previewer panicked")._elog());
24 Text::from_iter(output.iter().map(|(_, line)| line.clone()))
25 }
26 }
27
28 pub fn len(&self) -> usize {
29 if let Some(s) = unwrap!(self.string.lock().prefix("Previewer panicked")._elog()).as_ref() {
30 s.height()
31 } else {
32 let output = unwrap!(self.lines.read().prefix("Previewer panicked")._elog());
33 output.iter().count()
34 }
35 }
36
37 pub fn is_empty(&self) -> bool {
38 if let Some(s) = unwrap!(self.string.lock().prefix("Previewer panicked")._elog()).as_ref() {
39 s.height() == 0
40 } else {
41 let output = unwrap!(self.lines.read().prefix("Previewer panicked")._elog());
42 output.iter().next().is_none()
43 }
44 }
45
46 pub fn changed(&self) -> bool {
47 self.changed.swap(false, Ordering::Relaxed)
48 }
49
50 pub fn new(
51 lines: AppendOnly<Line<'static>>,
52 string: Arc<Mutex<Option<Text<'static>>>>,
53 changed: Arc<AtomicBool>,
54 ) -> Self {
55 Self {
56 lines,
57 string,
58 changed,
59 }
60 }
61}