bacnet-cli 0.14.0

BACnet command-line tool for device discovery, reading, and writing
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
//! The status bar and the footer (#1894). Both fit their width by
//! priority ([`super::fit`]): at 80 columns the least useful parts give way
//! first, and `? help` and `q quit` always stay. A footer notice sits
//! beside the key hints rather than over them; from [`WIDE_WIDTH`] columns
//! the hints give way so it shows in full where they can (#1927).

use ratatui::layout::{Alignment, Constraint, Layout, Rect};
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use super::fit::{self, hint, span, Part, ALWAYS};
use super::{ellipsize, text_width, Theme, WIDE_WIDTH};
use crate::tui::app::command_bar;
use crate::tui::app::watch::Screen;
use crate::tui::app::{App, Link, Overlay};
use crate::tui::safety::Level as Safety;

/// Columns the status bar keeps for `? help` at its right.
const HELP_WIDTH: u16 = 9;

/// Hints kept at this priority or above stay clear of a footer notice:
/// the way back (or a failed link's recovery key), `? help` and `q quit`.
/// From [`WIDE_WIDTH`], when giving up the way back shows the whole
/// notice, only `? help` and `q quit` do; never while the link has failed.
const RESERVED: u8 = 8;

/// `bacnet | BIP 10.0.1.5:47808 | up | READ-ONLY | COV 0 FD held | drop 0`
/// and `? help`. `COV` and `FD` are the footprint: subscriptions held on
/// devices, and the foreign-device registration with its state (shown only
/// with one). An armed write session turns the bar red with its countdown;
/// `ledger n` counts changes still in effect.
pub(super) fn status_bar(frame: &mut Frame, area: Rect, app: &App, theme: &Theme) {
    let level = app.level();
    let bar = match level {
        Safety::Armed { .. } => theme.danger(),
        _ => theme.bar(),
    };
    let [left_area, right_area] =
        Layout::horizontal([Constraint::Min(0), Constraint::Length(HELP_WIDTH)]).areas(area);
    let room = usize::from(left_area.width).saturating_sub(1);
    let mut spans = vec![Span::styled(" ", bar)];
    spans.extend(fit::fit(
        status_parts(app, theme, bar),
        &Span::styled(" | ", bar),
        room,
    ));
    frame.render_widget(Paragraph::new(Line::from(spans)).style(bar), left_area);
    frame.render_widget(
        Paragraph::new("? help ")
            .style(bar)
            .alignment(Alignment::Right),
        right_area,
    );
}

/// The status bar's fields, in order, each with what it gives way at.
fn status_parts(app: &App, theme: &Theme, bar: Style) -> Vec<Part> {
    let transport = if app.transport.is_empty() {
        "no profile"
    } else {
        app.transport
    };
    let state = match &app.link {
        Link::Down => "disconnected",
        Link::Disconnecting => "disconnecting",
        Link::Picking if app.transport == "MSTP" => "confirm joining",
        Link::Picking => "choose an interface",
        Link::Connecting => "connecting",
        Link::Up { .. } => "up",
        Link::Failed(_) => "failed",
        Link::Passive if app.monitor.is_some() => "listen only",
        Link::Passive => "no client",
    };
    let level = match app.level() {
        Safety::Passive => Span::styled("PASSIVE", theme.safe()),
        Safety::ReadOnly | Safety::Expired => Span::styled("READ-ONLY", theme.safe()),
        Safety::Armed { dangerous } => {
            let left = app.safety.armed.as_ref().map_or(0, |armed| {
                armed.until.saturating_duration_since(app.now).as_secs()
            });
            let name = if dangerous { "WRITE+DANGER" } else { "WRITE" };
            Span::styled(format!("{name} {}:{:02}", left / 60, left % 60), bar)
        }
    };
    let mut parts = vec![
        Part::new(1, vec![span("bacnet", bar)]),
        strip(app, theme, bar),
    ];
    // The local address goes before the fields after it do; on SC before
    // the hub state, as the SC screen shows the VMAC anyway.
    let address = if app.transport == "SC" { 3 } else { 4 };
    parts.push(match &app.link {
        Link::Up { local } => Part::new(address, vec![span(format!("{transport} {local}"), bar)])
            .or(8, vec![span(transport, bar)]),
        _ => Part::new(8, vec![span(transport, bar)]),
    });
    parts.push(Part::new(ALWAYS, vec![span(state, bar)]));
    if let Some(hub) = hub_state(app) {
        parts.push(Part::new(4, vec![span(hub, bar)]));
    }
    parts.push(Part::new(ALWAYS, vec![level]));
    // Subscriptions held on devices matter more than none.
    let subscriptions = app.watch.stats.subscriptions;
    parts.push(Part::new(
        if subscriptions > 0 { 5 } else { 2 },
        vec![span(format!("COV {subscriptions}"), bar)],
    ));
    if let Some(registration) = app.network.registration_state() {
        parts.push(Part::new(
            5,
            vec![span(format!("FD {}", registration.label()), bar)],
        ));
    }
    if !app.safety.ledger.is_empty() {
        parts.push(Part::new(
            7,
            vec![span(format!("ledger {}", app.safety.ledger.len()), bar)],
        ));
    }
    parts.push(Part::new(
        6,
        vec![span(format!("drop {}", app.dropped), bar)],
    ));
    parts
}

/// The numbered screen strip (#1895), with only the screens that apply to
/// the session and the one shown in brackets: `1 Devices 2 Browse [3 Watch]
/// 4 Network 7 Capture`, or in less room `1 2 [3 Watch] 4 7`. It sits in
/// the status bar so it costs no row at 80x24.
fn strip(app: &App, theme: &Theme, bar: Style) -> Part {
    let tabs = crate::tui::help::screens(app);
    let form = |labels: bool| {
        let mut spans = Vec::new();
        for (index, tab) in tabs.iter().enumerate() {
            if index > 0 {
                spans.push(span(" ", bar));
            }
            if tab.current {
                spans.push(span(
                    format!("[{} {}]", tab.key, tab.label),
                    theme.selected(),
                ));
            } else if labels {
                spans.push(span(format!("{} {}", tab.key, tab.label), bar));
            } else {
                spans.push(span(tab.key.to_string(), bar));
            }
        }
        spans
    };
    Part::new(2, form(true)).or(7, form(false))
}

/// The SC hub connector in a few words, once the SC worker has reported:
/// `hub primary`, `hub failover`, `hub connecting`, `hub down`.
fn hub_state(app: &App) -> Option<String> {
    use bacnet_transport::sc::ScConnectionState;
    let (link, _) = app.sc.link.as_ref()?;
    Some(match (link.state, link.hub) {
        (ScConnectionState::Connected, Some(hub)) => {
            format!("hub {}", crate::tui::sc::hub_name(hub))
        }
        (ScConnectionState::Connecting, _) => "hub connecting".into(),
        _ => "hub down".into(),
    })
}

/// Key hints, the filter line, or the command bar. A notice (a flash, or
/// a failed connection's summary) takes the left and the hints sit at the
/// right, cut to the ones that fit. `tab_focus`: the Network screen's wide
/// layout, where Tab moves the focus between panes.
pub(super) fn footer(frame: &mut Frame, area: Rect, app: &App, theme: &Theme, tab_focus: bool) {
    let width = usize::from(area.width);
    if let Overlay::Command(text) = &app.overlay {
        frame.render_widget(Paragraph::new(command_line(text, width, theme)), area);
        return;
    }
    if app.filter_editing {
        let line = Line::from(vec![
            Span::styled(" /", theme.accent()),
            Span::styled(format!("{}_", app.filter_text()), theme.selected()),
            Span::styled("  Enter keep  Esc clear", theme.dim()),
        ]);
        frame.render_widget(Paragraph::new(line), area);
        return;
    }
    let flash = app.flash.as_ref().map(|f| (f.text.as_str(), theme.warn()));
    if app.overlay != Overlay::None {
        // The open dialog lists its own keys; a notice set while it is
        // open still shows.
        if let Some((text, style)) = flash {
            let line = Line::styled(
                format!(" {}", ellipsize(text, width.saturating_sub(1))),
                style,
            );
            frame.render_widget(Paragraph::new(line), area);
        }
        return;
    }
    let failed;
    let notice = match (&app.link, flash) {
        (_, Some(flash)) => Some(flash),
        (Link::Failed(failure), None) => {
            failed = format!("Connection failed: {}", failure.summary);
            Some((failed.as_str(), theme.danger()))
        }
        _ => None,
    };
    let parts = screen_hints(app, theme, tab_focus);
    let Some((text, style)) = notice else {
        let line = fit::hints(parts, width.saturating_sub(1));
        let mut spans = vec![Span::raw(" ")];
        spans.extend(line.spans);
        frame.render_widget(Paragraph::new(Line::from(spans)), area);
        return;
    };
    // The way back (or a failed link's recovery key), `? help` and `q quit`
    // are kept clear of the notice; the other hints get what the notice
    // leaves, the lowest priority giving way first. On a wide terminal the
    // way back gives way too, but only when that shows the whole notice;
    // the recovery key never does.
    let room = |keep| width.saturating_sub(1 + fit::reserve(&parts, keep) + 2);
    let failed = matches!(app.link, Link::Failed(_));
    let whole = text_width(text) <= room(ALWAYS);
    let reserved = if area.width >= WIDE_WIDTH && whole && !failed {
        ALWAYS
    } else {
        RESERVED
    };
    let notice_room = room(reserved);
    let notice = ellipsize(text, notice_room);
    let used = 1 + text_width(&notice);
    let hints = fit::hints(parts, width.saturating_sub(used + 2));
    let [left, right] =
        Layout::horizontal([Constraint::Length(used as u16), Constraint::Min(0)]).areas(area);
    frame.render_widget(
        Paragraph::new(Line::styled(format!(" {notice}"), style)),
        left,
    );
    frame.render_widget(Paragraph::new(hints).alignment(Alignment::Right), right);
}

/// The command bar: what is typed, then the syntax of the commands it
/// matches (#1895), from the table the parser reads, cut to fit before
/// the keys.
fn command_line(text: &str, width: usize, theme: &Theme) -> Line<'static> {
    const KEYS: &str = "  Enter run  Esc close";
    let typed = format!("{text}_");
    let matches = command_bar::matching(text);
    let syntax = if matches.is_empty() {
        "no such command".to_string()
    } else {
        let forms: Vec<&str> = matches
            .iter()
            .flat_map(|s| s.usage.iter().copied())
            .collect();
        forms.join(" | ")
    };
    let room = width.saturating_sub(2 + text_width(&typed) + 2 + text_width(KEYS));
    let mut spans = vec![
        Span::styled(" :", theme.accent()),
        Span::styled(typed, theme.selected()),
    ];
    if room > 0 {
        spans.push(Span::raw("  "));
        spans.push(Span::styled(ellipsize(&syntax, room), theme.warn()));
    }
    spans.push(Span::styled(KEYS, theme.dim()));
    Line::from(spans)
}

/// The footer's key hints for the screen shown, each with its priority:
/// `? help` and `q quit` always stay, then the way back, then the
/// screen's main keys. Only keys that work there are listed.
fn screen_hints(app: &App, theme: &Theme, tab_focus: bool) -> Vec<Part> {
    let profiles = app.connection.is_some();
    let h = |keep, key, what| hint(theme, keep, key, what);
    let mut parts = Vec::new();
    match app.screen {
        Screen::Network if app.networks_tab() => {
            parts.push(h(6, "r", "ask routers"));
            parts.push(h(5, "Tab", "tables"));
            parts.push(h(3, ":find", "NAME"));
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Network => {
            parts.push(h(6, "r", "read tables"));
            parts.push(h(4, "m", "survey BBMDs"));
            parts.push(if tab_focus {
                h(4, "Tab", "focus pane/networks").or_hint(theme, 6, "Tab", "focus pane")
            } else {
                h(4, "Tab", "BDT/FDT/matrix/networks").or_hint(theme, 6, "Tab", "tables")
            });
            // After `:bbmd OTHER`, the way back to our own BBMD's tables.
            parts.push(match app.away_from_own_bbmd() {
                Some(_) => h(7, ":bbmd", "own BBMD"),
                None => h(3, ":bbmd", "ADDRESS"),
            });
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Sc if app.transport == "SC" => {
            parts.push(
                h(4, "Tab", "certificates/doctor/credentials").or_hint(theme, 6, "Tab", "view"),
            );
            parts.push(h(4, "p", "profiles"));
            parts.push(h(5, "x", "disconnect"));
            parts.push(h(8, "Esc", "back"));
        }
        // Off SC only leaving, or connecting a profile, does anything.
        Screen::Sc => {
            if profiles {
                parts.push(h(6, "p", "profiles"));
            }
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Mstp if app.mstp.station.is_some() => {
            parts.push(h(4, "z", "clear counters"));
            parts.push(h(5, "Space", "pause/follow"));
            parts.push(h(6, "x", "disconnect"));
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Mstp if app.monitor.is_some() => {
            parts.push(h(6, "Tab", "view"));
            parts.push(h(7, "J", "safe join"));
            parts.push(h(3, "z", "clear"));
            parts.push(h(4, "Space", "pause"));
            parts.push(h(5, "x", "stop"));
            parts.push(h(8, "Esc", "back"));
        }
        // Before a station joins: joining again, or a profile.
        Screen::Mstp => {
            // With profiles on, `c` opens the profile dialog like `p`.
            if profiles {
                parts.push(h(6, "p", "profiles"));
            } else if app.transport == "MSTP" && matches!(app.link, Link::Down | Link::Failed(_)) {
                parts.push(h(6, "c", "join"));
            }
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Capture => {
            parts.push(h(5, "Space", "pause"));
            parts.push(h(4, "Tab", "pane"));
            parts.push(h(6, "/", "filter"));
            parts.push(h(3, "e", "export"));
            parts.push(h(2, "z", "clear"));
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Watch => {
            parts.push(h(6, "x", "remove"));
            parts.push(h(5, ":watch", "DEV OBJ"));
            parts.push(h(8, "Esc", "back"));
        }
        Screen::Main | Screen::Browse => main_hints(app, theme, &mut parts),
    }
    parts.push(h(ALWAYS, "?", "help"));
    parts.push(h(ALWAYS, "q", "quit"));
    parts
}

/// The device table's and the browser's hints, by the link's state.
fn main_hints(app: &App, theme: &Theme, parts: &mut Vec<Part>) {
    let profiles = app.connection.is_some();
    let h = |keep, key, what| hint(theme, keep, key, what);
    match &app.link {
        Link::Up { .. } if app.screen == Screen::Browse => {
            parts.push(h(7, "Enter", "read"));
            parts.push(h(6, "a", "watch"));
            parts.push(h(5, "Tab", "pane"));
            parts.push(h(4, "/", "filter"));
            parts.push(h(3, "r", "refresh"));
            parts.push(h(8, "Esc", "devices"));
        }
        Link::Up { .. } => {
            parts.push(h(7, "Enter", "browse"));
            parts.push(h(7, "d", "Who-Is"));
            parts.push(h(5, "/", "filter"));
            parts.push(h(4, "s", "sort"));
            parts.push(h(3, "x", "disconnect"));
        }
        // The summary is beside these; the whole error is on the device
        // screen and in the log.
        // With profiles on, `c` only opens the profile dialog, as `p`
        // does, so one hint names it; it stays clear of the summary.
        Link::Failed(_) => {
            parts.push(match profiles {
                true => h(8, "p", "settings"),
                false => h(8, "c", "retry"),
            });
            parts.push(h(4, "L", "log"));
        }
        link => {
            if profiles {
                parts.push(h(6, "p", "profiles"));
            } else if matches!(link, Link::Down) {
                parts.push(h(7, "c", "connect"));
            }
            if matches!(link, Link::Connecting | Link::Picking) {
                parts.push(h(5, "x", "cancel"));
            }
            parts.push(h(4, "L", "log"));
        }
    }
}