use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub enum Panel {
#[default]
Editor,
Registers,
Stack,
CallStack,
Memory,
Disassembly,
Breakpoints,
Output,
Explain,
Syscalls,
Explorer,
Scratchpad,
Learn,
}
impl Panel {
pub const ALL: [Panel; 13] = [
Panel::Editor,
Panel::Registers,
Panel::Stack,
Panel::CallStack,
Panel::Memory,
Panel::Disassembly,
Panel::Breakpoints,
Panel::Output,
Panel::Explain,
Panel::Syscalls,
Panel::Explorer,
Panel::Scratchpad,
Panel::Learn,
];
pub const fn id(self) -> &'static str {
match self {
Panel::Editor => "editor",
Panel::Registers => "registers",
Panel::Stack => "stack",
Panel::CallStack => "call-stack",
Panel::Memory => "memory",
Panel::Disassembly => "disassembly",
Panel::Breakpoints => "breakpoints",
Panel::Output => "output",
Panel::Explain => "explain",
Panel::Syscalls => "syscalls",
Panel::Explorer => "explorer",
Panel::Scratchpad => "scratchpad",
Panel::Learn => "learn",
}
}
pub const fn title(self) -> &'static str {
match self {
Panel::Editor => "Editor",
Panel::Registers => "Registers",
Panel::Stack => "Stack",
Panel::CallStack => "Call stack",
Panel::Memory => "Memory",
Panel::Disassembly => "Disassembly",
Panel::Breakpoints => "Breakpoints",
Panel::Output => "Output",
Panel::Explain => "Explain",
Panel::Syscalls => "Syscalls",
Panel::Explorer => "Explorer",
Panel::Scratchpad => "Scratchpad",
Panel::Learn => "Learn",
}
}
pub const fn needs_debug_session(self) -> bool {
matches!(
self,
Panel::Registers | Panel::Stack | Panel::CallStack | Panel::Memory
)
}
pub const fn minimum_content(self) -> u16 {
match self {
Panel::Editor | Panel::Learn | Panel::Scratchpad => 8,
Panel::Registers | Panel::Syscalls | Panel::Explorer => 6,
Panel::Disassembly => 5,
Panel::Memory | Panel::Stack | Panel::Explain => 4,
Panel::Output | Panel::CallStack | Panel::Breakpoints => 3,
}
}
pub const fn minimum_height(self) -> u16 {
self.minimum_content() + 2
}
pub const fn is_text_input(self) -> bool {
matches!(self, Panel::Editor | Panel::Scratchpad)
}
pub fn from_id(id: &str) -> Option<Self> {
let id = id.trim().to_ascii_lowercase();
Self::ALL.into_iter().find(|panel| panel.id() == id)
}
pub fn next(self) -> Self {
let index = Self::ALL.iter().position(|p| *p == self).unwrap_or(0);
Self::ALL[(index + 1) % Self::ALL.len()]
}
pub fn previous(self) -> Self {
let index = Self::ALL.iter().position(|p| *p == self).unwrap_or(0);
Self::ALL[(index + Self::ALL.len() - 1) % Self::ALL.len()]
}
}
impl fmt::Display for Panel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.title())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identifiers_round_trip() {
for panel in Panel::ALL {
assert_eq!(Panel::from_id(panel.id()), Some(panel));
}
assert_eq!(Panel::from_id("REGISTERS"), Some(Panel::Registers));
assert_eq!(Panel::from_id(" editor "), Some(Panel::Editor));
assert_eq!(Panel::from_id("nonsense"), None);
}
#[test]
fn identifiers_and_titles_are_unique() {
let mut ids: Vec<&str> = Panel::ALL.iter().map(|p| p.id()).collect();
let count = ids.len();
ids.sort_unstable();
ids.dedup();
assert_eq!(ids.len(), count, "duplicate panel identifier");
let mut titles: Vec<&str> = Panel::ALL.iter().map(|p| p.title()).collect();
titles.sort_unstable();
titles.dedup();
assert_eq!(titles.len(), count, "duplicate panel title");
}
#[test]
fn cycling_visits_every_panel_and_returns() {
let mut panel = Panel::Editor;
let mut seen = Vec::new();
for _ in 0..Panel::ALL.len() {
seen.push(panel);
panel = panel.next();
}
assert_eq!(panel, Panel::Editor, "the cycle must wrap");
for expected in Panel::ALL {
assert!(seen.contains(&expected), "{expected} was skipped");
}
}
#[test]
fn previous_undoes_next() {
for panel in Panel::ALL {
assert_eq!(panel.next().previous(), panel);
assert_eq!(panel.previous().next(), panel);
}
}
#[test]
fn every_panel_asks_for_room_it_could_actually_use() {
for panel in Panel::ALL {
let content = panel.minimum_content();
assert!(
content >= 3,
"{panel} would show less than it takes to read"
);
assert!(content <= 8, "{panel} would be dropped on ordinary screens");
assert_eq!(panel.minimum_height(), content + 2, "{panel} border");
}
}
#[test]
fn only_the_text_panels_consume_typed_characters() {
for panel in Panel::ALL {
let expected = matches!(panel, Panel::Editor | Panel::Scratchpad);
assert_eq!(panel.is_text_input(), expected, "{panel}");
}
}
#[test]
fn panels_needing_a_session_are_the_cpu_state_panels() {
assert!(Panel::Registers.needs_debug_session());
assert!(Panel::Stack.needs_debug_session());
assert!(Panel::Memory.needs_debug_session());
assert!(!Panel::Editor.needs_debug_session());
assert!(!Panel::Disassembly.needs_debug_session());
assert!(!Panel::Syscalls.needs_debug_session());
assert!(!Panel::Output.needs_debug_session());
}
#[test]
fn the_editor_is_the_default_focus() {
assert_eq!(Panel::default(), Panel::Editor);
}
}