use super::app::command_bar::COMMANDS;
use super::app::watch::Screen;
use super::app::{App, Link};
use super::keymap::{Binding, BROWSER, CAPTURE, DEVICES, GLOBAL, MSTP, NETWORK, SC, WATCH};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Tab {
pub(crate) key: char,
pub(crate) label: &'static str,
pub(crate) current: bool,
}
pub(crate) fn screens(app: &App) -> Vec<Tab> {
let serial = app.transport == "MSTP" || app.monitor.is_some();
let connected = matches!(app.link, Link::Up { .. });
let current = match app.screen {
Screen::Browse => '2',
Screen::Main => '1',
Screen::Watch => '3',
Screen::Network => '4',
Screen::Sc => '5',
Screen::Mstp => '6',
Screen::Capture => '7',
};
[
('1', "Devices", true),
('2', "Browse", true),
('3', "Watch", true),
('4', "Network", connected),
('5', "SC", app.transport == "SC"),
('6', "MS/TP", serial),
('7', "Capture", true),
]
.into_iter()
.filter(|(key, _, applies)| *applies || *key == current)
.map(|(key, label, _)| Tab {
key,
label,
current: key == current,
})
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Section {
pub(crate) title: &'static str,
pub(crate) rows: Vec<(String, String)>,
pub(crate) on_screen: bool,
}
impl Section {
fn keys(title: &'static str, table: &[Binding]) -> Self {
Self {
title,
on_screen: true,
rows: table
.iter()
.map(|row| (row.label(), row.what.to_string()))
.collect(),
}
}
}
const WHO_IS: &[(&str, &str)] = &[
("Tab Up Down", "Next or previous field"),
("Left Right Space", "Change the scope"),
("Enter Esc", "Send (again past a warning); close"),
];
pub(crate) fn sections(app: &App) -> Vec<Section> {
let mut out = vec![Section::keys("Global", GLOBAL)];
match app.screen {
Screen::Browse => out.push(Section::keys("Browse (2)", BROWSER)),
Screen::Main => {
out.push(Section::keys("Devices (1)", DEVICES));
out.push(Section {
title: "Who-Is form (d)",
on_screen: false,
rows: WHO_IS
.iter()
.map(|(keys, what)| (keys.to_string(), what.to_string()))
.collect(),
});
}
Screen::Watch => out.push(Section::keys("Watch (3)", WATCH)),
Screen::Network => out.push(Section::keys("Network (4)", NETWORK)),
Screen::Sc => out.push(Section::keys("BACnet/SC (5)", SC)),
Screen::Mstp => out.push(Section::keys("MS/TP (6)", MSTP)),
Screen::Capture => out.push(Section::keys("Capture (7)", CAPTURE)),
}
out.push(Section {
title: "Commands (:)",
on_screen: false,
rows: COMMANDS
.iter()
.flat_map(|syntax| {
syntax
.usage
.iter()
.map(|form| (form.to_string(), syntax.what.to_string()))
})
.collect(),
});
out
}