Debian Codegraph @

src/canvas/widgets/network_basic.rs

raw symbols SCIP
1 use ratatui::{
2 Frame,
3 layout::{Constraint, Direction, Layout, Rect},
4 text::{Line, Span},
5 widgets::{Block, Paragraph},
6 };
7
8 use crate::{
9 app::App,
10 canvas::{
11 Painter,
12 drawing_utils::widget_block,
13 widgets::{PacketInfo, calculate_packet_info},
14 },
15 utils::data_units::{convert_bits, get_unit_prefix},
16 };
17
18 impl Painter {
19 pub fn draw_basic_network(
20 &self, f: &mut Frame<'_>, app_state: &mut App, draw_loc: Rect, widget_id: u64,
21 ) {
22 let show_packets = app_state.app_config_fields.network_show_packets;
23
24 f.render_widget(
25 widget_block(
26 true,
27 app_state.current_widget.widget_id == widget_id,
28 self.styles.border_type,
29 self.styles.general_widget_style,
30 )
31 .border_style(self.styles.highlighted_border_style),
32 draw_loc,
33 );
34
35 let use_binary_prefix = app_state.app_config_fields.network_use_binary_prefix;
36 let network_data = &(app_state.data_store.get_data().network_harvest);
37 let rx = get_unit_prefix(network_data.rx, use_binary_prefix);
38 let tx = get_unit_prefix(network_data.tx, use_binary_prefix);
39 let total_rx = convert_bits(network_data.total_rx, use_binary_prefix);
40 let total_tx = convert_bits(network_data.total_tx, use_binary_prefix);
41
42 let rx_label = format!("RX: {:.1}{}/s", rx.0, rx.1);
43 let tx_label = format!("TX: {:.1}{}/s", tx.0, tx.1);
44 let total_rx_label = format!("Total RX: {:.1}{}", total_rx.0, total_rx.1);
45 let total_tx_label = format!("Total TX: {:.1}{}", total_tx.0, total_tx.1);
46
47 // Determine if we need grid layout based on available width
48 // Assume we need at least ~15 chars per column for horizontal layout
49 // With 4 columns, that's ~60 chars minimum
50 // If width is less than 60, use grid layout (4 rows x 2 columns)
51 if show_packets {
52 let PacketInfo {
53 rx_packet_rate,
54 tx_packet_rate,
55 avg_rx_packet_size,
56 avg_tx_packet_size,
57 } = calculate_packet_info(network_data, use_binary_prefix);
58
59 // TODO: Stylize packet stuff later with something else? Or maybe make it so
60 // total is now (by default) just bolded RX/TX? I doubt anyone cares...
61 let rx_packet_rate_label = format!("RX Pkt: {}pkt/s", rx_packet_rate);
62 let tx_packet_rate_label = format!("TX Pkt: {}pkt/s", tx_packet_rate);
63 let avg_rx_packet_size_label = format!(
64 "Avg RX Pkt: {:.1}{}",
65 avg_rx_packet_size.0, avg_rx_packet_size.1
66 );
67 let avg_tx_packet_size_label = format!(
68 "Avg TX Pkt: {:.1}{}",
69 avg_tx_packet_size.0, avg_tx_packet_size.1
70 );
71
72 if draw_loc.width < 60 {
73 // 4 rows x 2 columns layout
74 // Column 1: RX, TX, Total RX, Total TX (top to bottom)
75 // Column 2: RX Packets, TX Packets, AVG RX, AVG TX (top to bottom)
76 let grid_loc = Layout::default()
77 .direction(Direction::Horizontal)
78 .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
79 .split(draw_loc);
80
81 // Column 1: RX, TX, Total RX, Total TX
82 let col1_loc = Layout::default()
83 .direction(Direction::Vertical)
84 .constraints([
85 Constraint::Percentage(25),
86 Constraint::Percentage(25),
87 Constraint::Percentage(25),
88 Constraint::Percentage(25),
89 ])
90 .split(grid_loc[0]);
91 f.render_widget(
92 Paragraph::new(Line::from(Span::styled(rx_label, self.styles.rx_style)))
93 .block(Block::default()),
94 col1_loc[0],
95 );
96 f.render_widget(
97 Paragraph::new(Line::from(Span::styled(tx_label, self.styles.tx_style)))
98 .block(Block::default()),
99 col1_loc[1],
100 );
101 f.render_widget(
102 Paragraph::new(Line::from(Span::styled(
103 total_rx_label,
104 self.styles.total_rx_style,
105 )))
106 .block(Block::default()),
107 col1_loc[2],
108 );
109 f.render_widget(
110 Paragraph::new(Line::from(Span::styled(
111 total_tx_label,
112 self.styles.total_tx_style,
113 )))
114 .block(Block::default()),
115 col1_loc[3],
116 );
117
118 // Column 2: RX Packets, TX Packets, AVG RX, AVG TX
119 let col2_loc = Layout::default()
120 .direction(Direction::Vertical)
121 .constraints([
122 Constraint::Percentage(25),
123 Constraint::Percentage(25),
124 Constraint::Percentage(25),
125 Constraint::Percentage(25),
126 ])
127 .split(grid_loc[1]);
128 f.render_widget(
129 Paragraph::new(Line::from(Span::styled(
130 rx_packet_rate_label,
131 self.styles.rx_style,
132 )))
133 .block(Block::default()),
134 col2_loc[0],
135 );
136 f.render_widget(
137 Paragraph::new(Line::from(Span::styled(
138 tx_packet_rate_label,
139 self.styles.tx_style,
140 )))
141 .block(Block::default()),
142 col2_loc[1],
143 );
144 f.render_widget(
145 Paragraph::new(Line::from(Span::styled(
146 avg_rx_packet_size_label,
147 self.styles.total_rx_style,
148 )))
149 .block(Block::default()),
150 col2_loc[2],
151 );
152 f.render_widget(
153 Paragraph::new(Line::from(Span::styled(
154 avg_tx_packet_size_label,
155 self.styles.total_tx_style,
156 )))
157 .block(Block::default()),
158 col2_loc[3],
159 );
160 } else {
161 // Horizontal 4-column layout
162 let constraints = [
163 Constraint::Percentage(25),
164 Constraint::Percentage(25),
165 Constraint::Percentage(25),
166 Constraint::Percentage(25),
167 ];
168
169 let divided_loc = Layout::default()
170 .direction(Direction::Horizontal)
171 .constraints(constraints)
172 .split(draw_loc);
173
174 // Column 1: RX/TX
175 let col1_loc = Layout::default()
176 .direction(Direction::Horizontal)
177 .constraints([Constraint::Percentage(100)])
178 .horizontal_margin(1)
179 .split(divided_loc[0]);
180 let col1_text = vec![
181 Line::from(Span::styled(rx_label, self.styles.rx_style)),
182 Line::from(Span::styled(tx_label, self.styles.tx_style)),
183 ];
184 f.render_widget(
185 Paragraph::new(col1_text).block(Block::default()),
186 col1_loc[0],
187 );
188
189 // Column 2: Total RX/TX
190 let col2_loc = Layout::default()
191 .direction(Direction::Horizontal)
192 .constraints([Constraint::Percentage(100)])
193 .horizontal_margin(1)
194 .split(divided_loc[1]);
195 let col2_text = vec![
196 Line::from(Span::styled(total_rx_label, self.styles.total_rx_style)),
197 Line::from(Span::styled(total_tx_label, self.styles.total_tx_style)),
198 ];
199 f.render_widget(
200 Paragraph::new(col2_text).block(Block::default()),
201 col2_loc[0],
202 );
203
204 // Column 3: RX/TX packets
205 let col3_loc = Layout::default()
206 .direction(Direction::Horizontal)
207 .constraints([Constraint::Percentage(100)])
208 .horizontal_margin(1)
209 .split(divided_loc[2]);
210 let col3_text = vec![
211 Line::from(Span::styled(rx_packet_rate_label, self.styles.rx_style)),
212 Line::from(Span::styled(tx_packet_rate_label, self.styles.tx_style)),
213 ];
214 f.render_widget(
215 Paragraph::new(col3_text).block(Block::default()),
216 col3_loc[0],
217 );
218
219 // Column 4: AVG RX/TX packets
220 let col4_loc = Layout::default()
221 .direction(Direction::Horizontal)
222 .constraints([Constraint::Percentage(100)])
223 .horizontal_margin(1)
224 .split(divided_loc[3]);
225 let col4_text = vec![
226 Line::from(Span::styled(
227 avg_rx_packet_size_label,
228 self.styles.total_rx_style,
229 )),
230 Line::from(Span::styled(
231 avg_tx_packet_size_label,
232 self.styles.total_tx_style,
233 )),
234 ];
235 f.render_widget(
236 Paragraph::new(col4_text).block(Block::default()),
237 col4_loc[0],
238 );
239 }
240 } else {
241 // No packets, 2-column layout
242 let constraints = [Constraint::Percentage(50), Constraint::Percentage(50)];
243
244 let divided_loc = Layout::default()
245 .direction(Direction::Horizontal)
246 .constraints(constraints)
247 .split(draw_loc);
248
249 // Column 1: RX/TX
250 let col1_loc = Layout::default()
251 .direction(Direction::Horizontal)
252 .constraints([Constraint::Percentage(100)])
253 .horizontal_margin(1)
254 .split(divided_loc[0]);
255 let col1_text = vec![
256 Line::from(Span::styled(rx_label, self.styles.rx_style)),
257 Line::from(Span::styled(tx_label, self.styles.tx_style)),
258 ];
259 f.render_widget(
260 Paragraph::new(col1_text).block(Block::default()),
261 col1_loc[0],
262 );
263
264 // Column 2: Total RX/TX
265 let col2_loc = Layout::default()
266 .direction(Direction::Horizontal)
267 .constraints([Constraint::Percentage(100)])
268 .horizontal_margin(1)
269 .split(divided_loc[1]);
270 let col2_text = vec![
271 Line::from(Span::styled(total_rx_label, self.styles.total_rx_style)),
272 Line::from(Span::styled(total_tx_label, self.styles.total_tx_style)),
273 ];
274 f.render_widget(
275 Paragraph::new(col2_text).block(Block::default()),
276 col2_loc[0],
277 );
278 }
279
280 // Update draw loc in widget map
281 if app_state.should_get_widget_bounds()
282 && let Some(widget) = app_state.widget_map.get_mut(&widget_id)
283 {
284 widget.top_left_corner = Some((draw_loc.x, draw_loc.y));
285 widget.bottom_right_corner =
286 Some((draw_loc.x + draw_loc.width, draw_loc.y + draw_loc.height));
287 }
288 }
289 }