Building 70 Projects ranging from beginner to advanced so anyone can — learn from, build upon, use as a reference, or even copy directly. Owned by my cybersecurity company 👇
Go aicertificationcloud-engineeringcybercybersecuritycybersecurity-analystcybersecurity-certificationcybersecurity-certificationscybersecurity-educationcybersecurity-jobscybersecurity-portfoliocybersecurity-projectscybersecurity-toolsgrchackinglearningpentestingproject-repositoryprojetcspython
// ©AngelaMos | 2026
// app.rs
use std::time::{Duration, Instant};
use lisdex_monitor::snapshot::{MonitorError, Snapshot, Source};
pub const DEFAULT_REFRESH_MS: u64 = 1000;
pub const INPUT_POLL_MS: u64 = 80;
pub const DETAIL_SCROLL_STEP: u16 = 5;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tab {
Overview,
Findings,
Instances,
}
impl Tab {
pub const ALL: [Tab; 3] = [Tab::Overview, Tab::Findings, Tab::Instances];
#[must_use]
pub fn title(self) -> &'static str {
match self {
Tab::Overview => "OVERVIEW",
Tab::Findings => "FINDINGS",
Tab::Instances => "INSTANCES",
}
}
#[must_use]
pub fn index(self) -> usize {
match self {
Tab::Overview => 0,
Tab::Findings => 1,
Tab::Instances => 2,
}
}
#[must_use]
pub fn next(self) -> Tab {
Tab::ALL[(self.index() + 1) % Tab::ALL.len()]
}
#[must_use]
pub fn previous(self) -> Tab {
Tab::ALL[(self.index() + Tab::ALL.len() - 1) % Tab::ALL.len()]
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
Quit,
NextTab,
PreviousTab,
SelectTab(Tab),
Down,
Up,
First,
Last,
ScrollDetailDown,
ScrollDetailUp,
TogglePause,
Refresh,
Ignore,
}
pub struct App {
source: Source,
snapshot: Snapshot,
error: Option<String>,
tab: Tab,
selected: usize,
detail_scroll: u16,
paused: bool,
refresh: Duration,
last_refresh: Instant,
ticks: u64,
running: bool,
}
impl App {
#[must_use]
pub fn new(source: Source, refresh: Duration) -> App {
let mut app = App {
source,
snapshot: Snapshot::empty(),
error: None,
tab: Tab::Overview,
selected: 0,
detail_scroll: 0,
paused: false,
refresh,
last_refresh: Instant::now(),
ticks: 0,
running: true,
};
app.reload();
app
}
#[must_use]
pub fn from_snapshot(snapshot: Snapshot) -> App {
App {
source: Source {
db: std::path::PathBuf::new(),
findings: std::path::PathBuf::new(),
campaign: None,
},
snapshot,
error: None,
tab: Tab::Overview,
selected: 0,
detail_scroll: 0,
paused: true,
refresh: Duration::from_millis(DEFAULT_REFRESH_MS),
last_refresh: Instant::now(),
ticks: 0,
running: true,
}
}
#[must_use]
pub fn snapshot(&self) -> &Snapshot {
&self.snapshot
}
#[must_use]
pub fn tab(&self) -> Tab {
self.tab
}
#[must_use]
pub fn set_tab(&mut self, tab: Tab) -> Tab {
self.tab = tab;
tab
}
#[must_use]
pub fn selected(&self) -> usize {
self.selected
}
#[must_use]
pub fn detail_scroll(&self) -> u16 {
self.detail_scroll
}
#[must_use]
pub fn is_paused(&self) -> bool {
self.paused
}
#[must_use]
pub fn is_running(&self) -> bool {
self.running
}
#[must_use]
pub fn ticks(&self) -> u64 {
self.ticks
}
#[must_use]
pub fn error(&self) -> Option<&str> {
self.error.as_deref()
}
#[must_use]
pub fn refresh_interval(&self) -> Duration {
self.refresh
}
pub fn tick(&mut self) {
self.ticks = self.ticks.wrapping_add(1);
}
pub fn reload(&mut self) {
match Snapshot::read(&self.source) {
Ok(snapshot) => {
self.snapshot = snapshot;
self.error = None;
self.clamp_selection();
}
Err(err) => self.error = Some(describe(&err)),
}
self.last_refresh = Instant::now();
}
pub fn reload_if_due(&mut self) {
if !self.paused && self.last_refresh.elapsed() >= self.refresh {
self.reload();
}
}
pub fn apply(&mut self, action: Action) {
match action {
Action::Quit => self.running = false,
Action::NextTab => self.tab = self.tab.next(),
Action::PreviousTab => self.tab = self.tab.previous(),
Action::SelectTab(tab) => self.tab = tab,
Action::Down => self.move_selection(true),
Action::Up => self.move_selection(false),
Action::First => {
self.selected = 0;
self.detail_scroll = 0;
}
Action::Last => {
self.selected = self.snapshot.findings().len().saturating_sub(1);
self.detail_scroll = 0;
}
Action::ScrollDetailDown => {
self.detail_scroll = self.detail_scroll.saturating_add(DETAIL_SCROLL_STEP);
}
Action::ScrollDetailUp => {
self.detail_scroll = self.detail_scroll.saturating_sub(DETAIL_SCROLL_STEP);
}
Action::TogglePause => self.paused = !self.paused,
Action::Refresh => self.reload(),
Action::Ignore => {}
}
}
fn move_selection(&mut self, forward: bool) {
self.detail_scroll = 0;
let count = self.snapshot.findings().len();
if count == 0 {
self.selected = 0;
return;
}
self.selected = if forward {
(self.selected + 1).min(count - 1)
} else {
self.selected.saturating_sub(1)
};
}
fn clamp_selection(&mut self) {
let count = self.snapshot.findings().len();
if count == 0 {
self.selected = 0;
} else if self.selected >= count {
self.selected = count - 1;
}
}
}
fn describe(err: &MonitorError) -> String {
err.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
fn app() -> App {
App::from_snapshot(Snapshot::empty())
}
#[test]
fn tabs_cycle_forwards_and_backwards_without_falling_off_the_end() {
assert_eq!(Tab::Overview.next(), Tab::Findings);
assert_eq!(Tab::Instances.next(), Tab::Overview);
assert_eq!(Tab::Overview.previous(), Tab::Instances);
assert_eq!(Tab::Findings.previous(), Tab::Overview);
}
#[test]
fn every_tab_has_a_distinct_title_and_index() {
let mut titles: Vec<&str> = Tab::ALL.iter().map(|tab| tab.title()).collect();
titles.sort_unstable();
titles.dedup();
assert_eq!(titles.len(), Tab::ALL.len());
for (index, tab) in Tab::ALL.iter().enumerate() {
assert_eq!(tab.index(), index);
}
}
#[test]
fn quit_stops_the_loop() {
let mut app = app();
assert!(app.is_running());
app.apply(Action::Quit);
assert!(!app.is_running());
}
#[test]
fn moving_the_selection_over_an_empty_finding_list_never_indexes_past_the_end() {
let mut app = app();
for action in [Action::Down, Action::Down, Action::Last, Action::Up] {
app.apply(action);
assert_eq!(
app.selected(),
0,
"a campaign with no crashes yet must not select a row the renderer would then index"
);
}
}
#[test]
fn scrolling_the_detail_pane_up_from_the_top_stays_at_the_top() {
let mut app = app();
app.apply(Action::ScrollDetailUp);
assert_eq!(app.detail_scroll(), 0);
app.apply(Action::ScrollDetailDown);
assert_eq!(app.detail_scroll(), DETAIL_SCROLL_STEP);
app.apply(Action::ScrollDetailUp);
assert_eq!(app.detail_scroll(), 0);
}
#[test]
fn changing_the_selection_returns_the_detail_pane_to_the_top() {
let mut app = app();
app.apply(Action::ScrollDetailDown);
app.apply(Action::Down);
assert_eq!(
app.detail_scroll(),
0,
"keeping the old offset would open a shorter report scrolled past its end"
);
}
#[test]
fn pausing_toggles_and_a_paused_app_does_not_reload_on_its_own() {
let mut app = app();
assert!(app.is_paused());
app.apply(Action::TogglePause);
assert!(!app.is_paused());
app.apply(Action::TogglePause);
assert!(app.is_paused());
}
#[test]
fn a_tab_can_be_selected_directly() {
let mut app = app();
app.apply(Action::SelectTab(Tab::Instances));
assert_eq!(app.tab(), Tab::Instances);
}
#[test]
fn ticks_wrap_rather_than_overflowing_the_animation_counter() {
let mut app = app();
app.ticks = u64::MAX;
app.tick();
assert_eq!(app.ticks(), 0);
}
}