Debian Codegraph @

src/canvas/components/pipe_gauge.rs

raw symbols SCIP
1 use ratatui::{
2 buffer::Buffer,
3 layout::Rect,
4 style::Style,
5 text::Line,
6 widgets::{Block, Widget},
7 };
8
9 #[derive(Debug, Clone, Copy, Default)]
10 pub enum LabelLimit {
11 #[default]
12 None,
13 #[expect(dead_code)]
14 Auto(u16),
15 Bars,
16 StartLabel,
17 }
18
19 /// A widget to measure something, using pipe characters ('|') as a unit.
20 #[derive(Debug, Clone)]
21 pub struct PipeGauge<'a> {
22 block: Option<Block<'a>>,
23 ratio: f64,
24 start_label: Option<Line<'a>>,
25 inner_label: Option<Line<'a>>,
26 label_style: Style,
27 gauge_style: Style,
28 hide_parts: LabelLimit,
29 }
30
31 impl Default for PipeGauge<'_> {
32 fn default() -> Self {
33 Self {
34 block: None,
35 ratio: 0.0,
36 start_label: None,
37 inner_label: None,
38 label_style: Style::default(),
39 gauge_style: Style::default(),
40 hide_parts: LabelLimit::default(),
41 }
42 }
43 }
44
45 impl<'a> PipeGauge<'a> {
46 /// The ratio, a value from 0.0 to 1.0 (any other greater or less will be
47 /// clamped) represents the portion of the pipe gauge to fill.
48 ///
49 /// Note: passing in NaN will potentially cause problems.
50 pub fn ratio(mut self, ratio: f64) -> Self {
51 self.ratio = ratio.clamp(0.0, 1.0);
52
53 self
54 }
55
56 /// The label displayed before the bar.
57 pub fn start_label<T>(mut self, start_label: T) -> Self
58 where
59 T: Into<Line<'a>>,
60 {
61 self.start_label = Some(start_label.into());
62 self
63 }
64
65 /// The label displayed inside the bar.
66 pub fn inner_label<T>(mut self, inner_label: T) -> Self
67 where
68 T: Into<Line<'a>>,
69 {
70 self.inner_label = Some(inner_label.into());
71 self
72 }
73
74 /// The style of the labels.
75 pub fn label_style(mut self, label_style: Style) -> Self {
76 self.label_style = label_style;
77 self
78 }
79
80 /// The style of the gauge itself.
81 pub fn gauge_style(mut self, style: Style) -> Self {
82 self.gauge_style = style;
83 self
84 }
85
86 /// Whether to hide parts of the gauge/label if the inner label wouldn't
87 /// fit.
88 pub fn hide_parts(mut self, hide_parts: LabelLimit) -> Self {
89 self.hide_parts = hide_parts;
90 self
91 }
92 }
93
94 impl Widget for PipeGauge<'_> {
95 fn render(mut self, area: Rect, buf: &mut Buffer) {
96 buf.set_style(area, self.label_style);
97 let gauge_area = match self.block.take() {
98 Some(b) => {
99 let inner_area = b.inner(area);
100 b.render(area, buf);
101 inner_area
102 }
103 None => area,
104 };
105
106 if gauge_area.height < 1 {
107 return;
108 }
109
110 let (col, row) = {
111 let inner_label_width = self
112 .inner_label
113 .as_ref()
114 .map(|l| l.width())
115 .unwrap_or_default();
116
117 let start_label_width = self
118 .start_label
119 .as_ref()
120 .map(|l| l.width())
121 .unwrap_or_default();
122
123 match self.hide_parts {
124 LabelLimit::StartLabel => {
125 let inner_label = self.inner_label.unwrap_or_else(|| Line::from(""));
126 let _ = buf.set_line(
127 gauge_area.left(),
128 gauge_area.top(),
129 &inner_label,
130 inner_label.width() as u16,
131 );
132
133 // Short circuit.
134 return;
135 }
136 LabelLimit::Auto(_)
137 if gauge_area.width < (inner_label_width + start_label_width + 1) as u16 =>
138 {
139 let inner_label = self.inner_label.unwrap_or_else(|| Line::from(""));
140 let _ = buf.set_line(
141 gauge_area.left(),
142 gauge_area.top(),
143 &inner_label,
144 inner_label.width() as u16,
145 );
146
147 // Short circuit.
148 return;
149 }
150 _ => {
151 let start_label = self.start_label.unwrap_or_else(|| Line::from(""));
152 buf.set_line(
153 gauge_area.left(),
154 gauge_area.top(),
155 &start_label,
156 start_label.width() as u16,
157 )
158 }
159 }
160 };
161
162 let end_label = self.inner_label.unwrap_or_else(|| Line::from(""));
163 match self.hide_parts {
164 LabelLimit::Bars => {
165 let _ = buf.set_line(
166 gauge_area
167 .right()
168 .saturating_sub(end_label.width() as u16 + 1),
169 row,
170 &end_label,
171 end_label.width() as u16,
172 );
173 }
174 LabelLimit::Auto(width_limit)
175 if gauge_area.right().saturating_sub(col) < width_limit =>
176 {
177 let _ = buf.set_line(
178 gauge_area
179 .right()
180 .saturating_sub(end_label.width() as u16 + 1),
181 row,
182 &end_label,
183 1,
184 );
185 }
186 LabelLimit::Auto(_) | LabelLimit::None => {
187 let (start, _) = buf.set_line(col, row, &Line::from("["), gauge_area.width);
188 if start >= gauge_area.right() {
189 return;
190 }
191
192 let (end, _) = buf.set_line(
193 (gauge_area.x + gauge_area.width).saturating_sub(1),
194 row,
195 &Line::from("]"),
196 gauge_area.width,
197 );
198
199 let pipe_end = end.saturating_sub(1).min(
200 start + (f64::from(end.saturating_sub(start)) * self.ratio).floor() as u16,
201 );
202
203 for col in start..pipe_end {
204 if let Some(cell) = buf.cell_mut((col, row)) {
205 cell.set_symbol("|").set_style(Style {
206 fg: self.gauge_style.fg,
207 bg: None,
208 add_modifier: self.gauge_style.add_modifier,
209 sub_modifier: self.gauge_style.sub_modifier,
210 underline_color: None,
211 });
212 }
213 }
214
215 if (end_label.width() as u16) < end.saturating_sub(start) {
216 let gauge_end = gauge_area
217 .right()
218 .saturating_sub(end_label.width() as u16 + 1);
219 buf.set_line(gauge_end, row, &end_label, end_label.width() as u16);
220 }
221 }
222 LabelLimit::StartLabel => unreachable!(),
223 }
224 }
225 }