aranet-cli 0.2.0

Command-line interface for Aranet environmental sensors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Main UI layout and rendering for the TUI dashboard.
//!
//! This module provides the primary layout structure and draw functions for
//! the Aranet TUI dashboard. The layout consists of:
//!
//! - **Header**: Title and current time display
//! - **Main content**: Device list (left) and readings panel (right)
//! - **Status bar**: Help text and status messages

pub mod colors;
pub mod theme;
pub mod widgets;

mod dashboard;
mod history;
mod overlays;
mod service;
mod settings;

use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Paragraph};

use super::app::{App, Tab, Theme};
use colors::co2_color;
use theme::BORDER_TYPE;

/// Draw the complete TUI interface.
///
/// This function creates the main layout and delegates rendering to helper
/// functions for each area.
pub fn draw(frame: &mut Frame, app: &App) {
    // Apply theme background
    if matches!(app.theme, Theme::Light) {
        frame.render_widget(
            Block::default().style(Style::default().bg(app.theme.bg())),
            frame.area(),
        );
    }

    // Full-screen chart view
    if app.show_fullscreen_chart {
        overlays::draw_fullscreen_chart(frame, app);
        return; // Don't render anything else
    }

    // Comparison view
    if app.show_comparison {
        overlays::draw_comparison_view(frame, app);
        return; // Don't render anything else
    }

    let main_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Header bar
            Constraint::Length(3), // Tab bar
            Constraint::Min(1),    // Main content
            Constraint::Length(1), // Status bar
        ])
        .split(frame.area());

    draw_header(frame, main_layout[0], app);
    draw_tab_bar(frame, main_layout[1], app);

    // Responsive layout: hide sidebar on narrow terminals or when toggled off
    let area = main_layout[2];
    let is_narrow = area.width < 80;
    let show_sidebar = app.show_sidebar && !is_narrow;

    let content_constraints = if show_sidebar {
        vec![
            Constraint::Length(app.sidebar_width), // Device list sidebar
            Constraint::Min(1),                    // Main content
        ]
    } else {
        vec![
            Constraint::Length(0), // Hidden sidebar
            Constraint::Min(1),    // Full width content
        ]
    };

    let content_layout = Layout::default()
        .direction(Direction::Horizontal)
        .constraints(content_constraints)
        .split(area);

    if show_sidebar {
        dashboard::draw_device_list(frame, content_layout[0], app);
    }

    // Render different content based on active tab
    match app.active_tab {
        Tab::Dashboard => dashboard::draw_readings_panel(frame, content_layout[1], app),
        Tab::History => history::draw_history_panel(frame, content_layout[1], app),
        Tab::Settings => settings::draw_settings_panel(frame, content_layout[1], app),
        Tab::Service => service::draw_service_panel(frame, content_layout[1], app),
    }

    draw_status_bar(frame, main_layout[3], app);

    // Draw help overlay if active
    if app.show_help {
        overlays::draw_help_overlay(frame);
    }

    // Alert history overlay
    overlays::draw_alert_history(frame, app);

    // Alias editor overlay
    overlays::draw_alias_editor(frame, app);

    // Error details popup
    overlays::draw_error_popup(frame, app);

    // Confirmation dialog (on top of everything)
    overlays::draw_confirmation_dialog(frame, app);
}

/// Draw the header bar with app title, quick stats, and indicators.
fn draw_header(frame: &mut Frame, area: Rect, app: &App) {
    let theme = app.app_theme();
    let width = area.width;

    let mut spans = vec![Span::styled(
        " Aranet ",
        Style::default()
            .fg(theme.primary)
            .add_modifier(Modifier::BOLD),
    )];

    if width >= 32 {
        spans.push(Span::styled(
            format!("v{} ", env!("CARGO_PKG_VERSION")),
            Style::default().fg(theme.text_muted),
        ));
    }

    // Connected count
    let connected = app.connected_count();
    let total = app.devices.len();
    let conn_color = if connected == 0 {
        theme.danger
    } else {
        theme.success
    };
    if width >= 22 {
        spans.push(Span::styled(
            format!(" {}/{} online ", connected, total),
            Style::default().fg(conn_color),
        ));
    }

    // Average CO2 if available
    if width >= 40
        && let Some(avg_co2) = app.average_co2()
    {
        let co2_color = co2_color(&theme, avg_co2);
        spans.push(Span::styled(
            format!(" CO2 {} ", avg_co2),
            Style::default().fg(co2_color),
        ));
    }

    // Alert count
    let alert_count = app.alerts.len();
    if width >= 50 && alert_count > 0 {
        spans.push(Span::styled(
            format!(" Alerts {} ", alert_count),
            Style::default()
                .fg(theme.danger)
                .add_modifier(Modifier::BOLD),
        ));
    }

    // Theme indicator
    if width >= 62 {
        let (theme_label, theme_color) = if matches!(app.theme, Theme::Light) {
            (" Light ", theme.warning)
        } else {
            (" Dark ", theme.info)
        };
        spans.push(Span::styled(theme_label, Style::default().fg(theme_color)));
    }

    if width >= 76 {
        if app.sticky_alerts {
            spans.push(Span::styled(" Sticky ", Style::default().fg(theme.warning)));
        }
        if app.bell_enabled {
            spans.push(Span::styled(" Bell ", Style::default().fg(theme.warning)));
        }
        if app.last_error.is_some() && !app.show_error_details {
            spans.push(Span::styled(" Error ", Style::default().fg(theme.danger)));
        }
        if app.smart_home_enabled {
            spans.push(Span::styled(" Home ", Style::default().fg(theme.success)));
        }
    }

    let header = Paragraph::new(Line::from(spans)).style(theme.header_style());

    frame.render_widget(header, area);
}

/// Get context-sensitive help hints based on current state.
fn context_hints(app: &App) -> Vec<(&'static str, &'static str)> {
    let mut hints = Vec::new();

    // Always show help key
    hints.push(("?", "help"));

    match app.active_tab {
        Tab::Dashboard => {
            if app.devices.is_empty() {
                hints.push(("s", "scan"));
            } else {
                hints.push(("j/k", "select"));
                if app.selected_device().is_some() {
                    if app
                        .selected_device()
                        .map(|d| matches!(d.status, super::app::ConnectionStatus::Connected))
                        .unwrap_or(false)
                    {
                        hints.push(("r", "refresh"));
                        hints.push(("d", "disconnect"));
                        hints.push(("S", "sync"));
                    } else {
                        hints.push(("c", "connect"));
                    }
                }
                hints.push(("s", "scan"));
            }
        }
        Tab::History => {
            hints.push(("S", "sync"));
            hints.push(("0-4", "filter"));
            hints.push(("PgUp/Dn", "scroll"));
            hints.push(("g", "fullscreen"));
        }
        Tab::Settings => {
            hints.push(("+/-", "adjust"));
            hints.push(("n", "alias"));
        }
        Tab::Service => {
            hints.push(("r", "refresh"));
            hints.push(("Enter", "start/stop"));
            hints.push(("j/k", "select"));
        }
    }

    hints.push(("q", "quit"));
    hints
}

/// Draw the status bar with context-sensitive help.
fn draw_status_bar(frame: &mut Frame, area: Rect, app: &App) {
    let theme = app.app_theme();
    let width = area.width;
    let time_str = {
        let now =
            time::OffsetDateTime::now_local().unwrap_or_else(|_| time::OffsetDateTime::now_utc());
        now.format(&time::format_description::parse("[hour]:[minute]:[second]").unwrap_or_default())
            .unwrap_or_default()
    };

    // Build left content with context-sensitive hints
    let left_spans = if app.scanning {
        vec![
            Span::styled(
                format!("{} ", app.spinner_char()),
                Style::default().fg(theme.primary),
            ),
            Span::styled("Scanning...", Style::default().fg(theme.text_secondary)),
        ]
    } else if app.is_any_connecting() {
        vec![
            Span::styled(
                format!("{} ", app.spinner_char()),
                Style::default().fg(theme.primary),
            ),
            Span::styled("Connecting...", Style::default().fg(theme.text_secondary)),
        ]
    } else if app.is_syncing() {
        vec![
            Span::styled(
                format!("{} ", app.spinner_char()),
                Style::default().fg(theme.primary),
            ),
            Span::styled("Syncing...", Style::default().fg(theme.text_secondary)),
        ]
    } else if let Some(msg) = app.current_status_message() {
        vec![Span::styled(
            format!(" {}", msg),
            Style::default().fg(theme.text_secondary),
        )]
    } else {
        // Context-sensitive hints with styled keys
        let hints = context_hints(app);
        let hints: Vec<_> = if width < 46 {
            let mut compact = vec![hints[0]];
            if hints.len() > 2 {
                compact.push(hints[1]);
            }
            if let Some(last) = hints.last().copied()
                && compact.last().copied() != Some(last)
            {
                compact.push(last);
            }
            compact
        } else if width < 72 {
            let mut compact = vec![hints[0]];
            compact.extend(hints.iter().skip(1).take(2).copied());
            if let Some(last) = hints.last().copied()
                && compact.last().copied() != Some(last)
            {
                compact.push(last);
            }
            compact
        } else {
            hints
        };
        let mut spans = vec![Span::raw(" ")];
        for (i, (key, desc)) in hints.iter().enumerate() {
            if i > 0 {
                spans.push(Span::styled(" | ", Style::default().fg(theme.text_muted)));
            }
            spans.push(Span::styled(
                *key,
                Style::default()
                    .fg(theme.primary)
                    .add_modifier(Modifier::BOLD),
            ));
            spans.push(Span::styled(
                format!(" {}", desc),
                Style::default().fg(theme.text_muted),
            ));
        }
        spans
    };

    // Split status bar into left (hints), indicators, and right (time)
    let logging_width = if app.logging_enabled { 5 } else { 0 };
    let status_layout = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Min(1),
            Constraint::Length(logging_width),
            Constraint::Length(10),
        ])
        .split(area);

    let left = Paragraph::new(Line::from(left_spans));
    frame.render_widget(left, status_layout[0]);

    // Logging indicator
    if app.logging_enabled {
        let log_indicator = Paragraph::new(" REC").style(
            Style::default()
                .fg(theme.danger)
                .add_modifier(Modifier::BOLD),
        );
        frame.render_widget(log_indicator, status_layout[1]);
    }

    let right = Paragraph::new(time_str)
        .style(Style::default().fg(theme.text_muted))
        .alignment(Alignment::Right);

    frame.render_widget(right, status_layout[2]);
}

/// Draw the tab bar with modern styling.
fn draw_tab_bar(frame: &mut Frame, area: Rect, app: &App) {
    let theme = app.app_theme();

    let tabs = [
        ("Dashboard", Tab::Dashboard),
        ("History", Tab::History),
        ("Settings", Tab::Settings),
        ("Service", Tab::Service),
    ];

    // Build custom tab line with underline indicator for active tab
    let tab_titles: Vec<Line> = tabs
        .iter()
        .map(|(name, tab)| {
            let is_active = *tab == app.active_tab;
            let style = if is_active {
                Style::default()
                    .fg(theme.primary)
                    .add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(theme.text_muted)
            };
            // Add underline to active tab for visual emphasis
            let styled_name = if is_active {
                Span::styled(
                    format!(" {} ", name),
                    style.add_modifier(Modifier::UNDERLINED),
                )
            } else {
                Span::styled(format!(" {} ", name), style)
            };
            Line::from(styled_name)
        })
        .collect();

    let tabs_widget = ratatui::widgets::Tabs::new(tab_titles)
        .block(
            Block::default()
                .borders(Borders::BOTTOM)
                .border_type(BORDER_TYPE)
                .border_style(Style::default().fg(theme.border_inactive)),
        )
        .highlight_style(Style::default().fg(theme.primary))
        .divider(Span::styled(" | ", Style::default().fg(theme.text_muted)))
        .select(match app.active_tab {
            Tab::Dashboard => 0,
            Tab::History => 1,
            Tab::Settings => 2,
            Tab::Service => 3,
        });

    frame.render_widget(tabs_widget, area);
}