1use log::{error};
2use ratatui::{
3 layout::Rect,
4 widgets::{Paragraph, Wrap},
5};
6
7use crate::{
8 config::{PreviewConfig, PreviewLayoutSetting},
9 spawn::preview::PreviewerView,
10};
11
12#[derive(Debug)]
13pub struct PreviewUI {
14 pub view: PreviewerView,
15 config: PreviewConfig,
16 pub layout_idx: usize,
17 pub area: Rect,
18 pub offset: u16,
19}
20
21impl PreviewUI {
22 pub fn new(view: PreviewerView, config: PreviewConfig) -> Self {
23 Self {
24 view,
25 config,
26 layout_idx: 0,
27 offset: 0,
28 area: Rect::default()
29 }
30 }
31
32 pub fn is_show(&self) -> bool {
33 self.config.show && self.layout().max != 0 }
36 pub fn show<const SHOW: bool>(&mut self) -> bool {
37 let previous = self.config.show;
38 self.config.show = SHOW;
39 previous != SHOW
40 }
41 pub fn toggle_show(&mut self) {
42 self.config.show = !self.config.show;
43 }
44
45 pub fn layout(&self) -> &PreviewLayoutSetting {
46 &self.config.layout[self.layout_idx].layout
47 }
48 pub fn command(&self) -> &String {
49 &self.config.layout[self.layout_idx].command
50 }
51
52 pub fn up(&mut self, n: u16) {
65 if self.offset >= n {
66 self.offset -= n;
67 } else if self.config.scroll_wrap {
68 let total_lines = self.view.len() as u16;
69 self.offset = total_lines.saturating_sub(n - self.offset);
70 } else {
71 self.offset = 0;
72 }
73 }
74
75 pub fn down(&mut self, n: u16) {
76 let total_lines = self.view.len() as u16;
77
78 if self.offset + n > total_lines {
79 if self.config.scroll_wrap {
80 self.offset = 0;
81 } else {
82 self.offset = total_lines;
83 }
84 } else {
85 self.offset += n;
86 }
87 }
88
89
90 pub fn update_dimensions(&mut self, area: &Rect) {
91 let mut height = area.height;
92 height -= self.config.border.height();
93 self.area.height = height;
94
95 let mut width = area.width;
96 width -= self.config.border.width();
97 self.area.width = width;
98 }
99
100 pub fn cycle_layout(&mut self) {
101 self.layout_idx = (self.layout_idx + 1) % self.config.layout.len()
102 }
103
104
105 pub fn set_idx(&mut self, idx: u8) -> bool {
106 let idx = idx as usize;
107 if idx <= self.config.layout.len() {
108 let changed = self.layout_idx != idx;
109 self.layout_idx = idx;
110 changed
111 } else {
112 error!("Layout idx {idx} out of bounds, ignoring.");
113 false
114 }
115 }
116
117 pub fn make_preview(&self) -> Paragraph<'_> {
118 let results = self.view.results();
119 let height = self.area.height as usize;
120 let offset = self.offset as usize;
121
122 let visible_lines: Vec<_> = results
124 .iter()
125 .skip(offset)
126 .take(height)
127 .cloned()
128 .collect();
129
130 let mut preview = Paragraph::new(visible_lines);
131 preview = preview.block(self.config.border.as_block());
132 if self.config.wrap {
133 preview = preview.wrap(Wrap { trim: true });
134 }
135 preview
136 }
137}