use ratatui::widgets::{Block, Borders};
use ratatui::Frame;
use crate::app::App;
use crate::ui::layout::{self, LayoutMode};
use crate::ui::widgets::{self, chrome};
pub fn draw(frame: &mut Frame, app: &App) {
let area = frame.area();
frame.render_widget(
Block::default()
.borders(Borders::NONE)
.style(app.theme.base()),
area,
);
let layout = layout::compute(area, app.page, app.focus);
if layout.mode == LayoutMode::TooSmall {
chrome::draw_too_small(frame, area);
return;
}
chrome::draw_page_bar(frame, app, layout.page_bar);
if let Some(tab_bar) = layout.tab_bar {
chrome::draw_tab_bar(frame, app, tab_bar, &layout.tabbed);
}
for (panel, rect) in &layout.panels {
widgets::draw_panel(frame, app, *panel, *rect);
}
chrome::draw_status_bar(frame, app, layout.status_bar);
chrome::draw_overlay(frame, app, area);
}
pub fn sync_scroll(app: &mut App, width: u16, height: u16) {
use crate::app::panel::Panel;
use crate::app::scroll::ScrollState;
let layout = layout::compute(
ratatui::layout::Rect::new(0, 0, width, height),
app.page,
app.focus,
);
for (panel, area) in layout.panels.clone() {
if !ScrollState::is_scrollable(panel) {
continue;
}
let inner = inner_area(area);
let rows = widgets::content_rows(app, panel, inner.0);
app.scroll.fit(panel, rows, usize::from(inner.1));
if panel == Panel::Explorer {
let row = widgets::chrome::explorer_selected_row(app);
app.scroll.reveal(Panel::Explorer, row);
}
}
let Some(area) = layout.area_of(Panel::Editor) else {
return;
};
let (_, inner_height) = inner_area(area);
app.workspace.active_mut().fit(usize::from(inner_height));
}
fn inner_area(area: ratatui::layout::Rect) -> (u16, u16) {
(area.width.saturating_sub(4), area.height.saturating_sub(2))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Settings;
use crate::project::Project;
use ratatui::backend::TestBackend;
use ratatui::Terminal;
fn app() -> App {
let project = Project::for_file(std::path::Path::new("/tmp/ratasm-test/main.asm"));
App::new(project, Settings::default()).expect("databases load")
}
fn render(app: &App, width: u16, height: u16) -> Vec<String> {
let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).expect("terminal");
terminal.draw(|frame| draw(frame, app)).expect("draw");
let buffer = terminal.backend().buffer();
(0..height)
.map(|y| {
(0..width)
.map(|x| {
buffer
.cell((x, y))
.map_or(" ", ratatui::buffer::Cell::symbol)
})
.collect::<String>()
})
.collect()
}
fn screen(app: &App, width: u16, height: u16) -> String {
render(app, width, height).join("\n")
}
fn settled(app: &mut App, width: u16, height: u16) -> String {
sync_scroll(app, width, height);
screen(app, width, height)
}
#[test]
fn a_long_build_log_can_be_read_from_either_end() {
let mut app = app();
app.output = (1..=60).map(|n| format!("line-{n:02}")).collect();
app.focus_panel(crate::app::Panel::Output);
let text = settled(&mut app, 120, 30);
assert!(text.contains("line-60"), "a log opens at its newest line");
assert!(!text.contains("line-01"), "which is not its oldest");
app.apply(&crate::command::Command::ScrollToTop);
let text = settled(&mut app, 120, 30);
assert!(text.contains("line-01"), "and can be wound back:\n{text}");
app.apply(&crate::command::Command::ScrollToEnd);
let text = settled(&mut app, 120, 30);
assert!(text.contains("line-60"));
}
#[test]
fn a_panel_with_more_content_than_room_says_so() {
let mut app = app();
app.output = (1..=60).map(|n| format!("line-{n:02}")).collect();
let crowded = settled(&mut app, 120, 30);
app.output = vec!["only one line".to_owned()];
app.scroll.reset(crate::app::Panel::Output);
let roomy = settled(&mut app, 120, 30);
let thumb = app.theme.symbols().scroll_thumb;
assert!(
crowded.contains(thumb),
"a scrollbar marks what is off-screen"
);
assert!(
!roomy.contains(thumb),
"and stays away when everything fits"
);
}
#[test]
fn scrolling_stops_where_the_content_does() {
let mut app = app();
app.output = (1..=60).map(|n| format!("line-{n:02}")).collect();
app.focus_panel(crate::app::Panel::Output);
sync_scroll(&mut app, 120, 30);
for _ in 0..100 {
app.apply(&crate::command::Command::ScrollDown);
sync_scroll(&mut app, 120, 30);
}
let extent = app.scroll.extent(crate::app::Panel::Output);
assert_eq!(extent.offset, extent.max_offset());
assert!(settled(&mut app, 120, 30).contains("line-60"));
}
#[test]
fn a_long_tool_line_wraps_rather_than_running_off_the_edge() {
let mut app = app();
app.focus_panel(crate::app::Panel::Output);
app.output = vec![format!(
"main.asm:12: error: {}",
"symbol `a_very_long_undefined_name' not defined and cannot be resolved here"
)];
let text = settled(&mut app, 120, 30);
assert!(
text.contains("cannot be resolved here"),
"the informative end of the message must survive:\n{text}"
);
}
#[test]
fn the_output_border_counts_the_last_build_s_diagnostics() {
use crate::assembler::diagnostics;
let mut app = app();
app.output = vec!["$ nasm".to_owned()];
let (error, warning) = {
let symbols = app.theme.symbols();
(symbols.error, symbols.warning)
};
let text = settled(&mut app, 120, 30);
assert!(
!text.contains(&format!("{error} ")),
"nothing to count before a build"
);
app.diagnostics = diagnostics::parse_assembler_output(
"main.asm:1: error: one\nmain.asm:2: error: two\nmain.asm:3: warning: three\n",
);
app.build = Some(crate::assembler::BuildOutcome {
success: false,
steps: Vec::new(),
diagnostics: app.diagnostics.clone(),
executable: None,
duration: std::time::Duration::ZERO,
});
let border = settled(&mut app, 120, 30);
assert!(
border.contains(&format!("{error} 2")),
"two errors should be on the border:\n{border}"
);
assert!(border.contains(&format!("{warning} 1")));
}
#[test]
fn the_status_bar_gives_a_long_message_the_room_it_needs() {
let mut app = app();
app.status = crate::app::Status::error(
"main.asm:12: error: symbol `helper' undefined and cannot be resolved".to_owned(),
);
let rows = render(&app, 80, 30);
let status = rows.last().expect("a status bar").clone();
assert!(
status.contains("cannot be resolved"),
"the message must not be truncated to keep a format label:\n{status}"
);
assert!(status.contains("idle"), "the debugger state always stays");
app.status = crate::app::Status::info("Ready".to_owned());
let rows = render(&app, 80, 30);
let status = rows.last().expect("a status bar").clone();
assert!(
status.contains("hex"),
"with room to spare the extras come back:\n{status}"
);
}
#[test]
fn the_status_bar_names_the_file_being_edited() {
let mut app = app();
app.workspace.active_mut().set_path("src/util.asm");
let rows = render(&app, 120, 30);
let status = rows.last().expect("a status bar").clone();
assert!(status.contains("util.asm"), "{status}");
}
#[test]
fn the_page_bar_keeps_the_open_file_on_a_narrow_terminal() {
let dir = tempfile::tempdir().expect("temp dir");
let mut app = app();
for name in ["alpha.asm", "beta.asm", "gamma.asm", "delta.asm"] {
let path = dir.path().join(name);
std::fs::write(&path, "ret\n").expect("write");
app.workspace.open(&path).expect("open");
}
let bar = render(&app, 60, 20).remove(0);
assert!(
bar.contains("delta.asm"),
"the active file must survive a narrow bar:\n{bar}"
);
assert!(bar.contains('+'), "and the rest must be counted:\n{bar}");
assert!(
!bar.contains("Reference"),
"page names shrink first:\n{bar}"
);
}
#[test]
fn the_symbol_list_shows_what_the_panel_is_wide_enough_for() {
let mut app = app();
app.workspace
.active_mut()
.reload("section .text\n_start:\n ret\n");
let roomy = settled(&mut app, 160, 48);
assert!(roomy.contains("_start"), "the name always shows");
assert!(roomy.contains("label"), "and its kind when there is room");
let cramped = settled(&mut app, 120, 30);
assert!(cramped.contains("_start"), "{cramped}");
}
#[test]
fn the_registers_separate_what_the_program_set_from_what_the_cpu_did() {
let mut app = app();
app.open_page(crate::app::Page::Debug);
app.registers.update(
[
("rax".to_owned(), 1u64),
("rip".to_owned(), 0x40_00b0),
("rflags".to_owned(), 0x202),
]
.into_iter()
.collect(),
);
let lines = crate::ui::widgets::cpu::register_lines(&app, 60);
let text: Vec<String> = lines
.iter()
.map(|line| {
line.spans
.iter()
.map(|span| span.content.as_ref())
.collect::<String>()
})
.collect();
let rip = text
.iter()
.position(|line| line.contains("RIP"))
.expect("RIP is listed");
assert!(rip > 0, "RIP should not be the first row");
assert!(
text[rip - 1].trim().is_empty(),
"a blank row should divide the groups: {text:?}"
);
}
#[test]
fn a_wide_terminal_shows_the_main_panels() {
let mut app = app();
app.open_page(crate::app::Page::Debug);
let text = screen(&app, 160, 48);
for title in ["Editor", "Registers", "Flags", "Disassembly", "Explain"] {
assert!(text.contains(title), "{title} is missing from the screen");
}
}
#[test]
fn the_page_bar_names_every_page_and_marks_the_open_one() {
let app = app();
let bar = render(&app, 160, 48).remove(0);
for page in crate::app::Page::ALL {
assert!(bar.contains(page.title()), "{page} is missing: {bar}");
assert!(
bar.contains(&page.number().to_string()),
"the shortcut number for {page} is missing: {bar}"
);
}
assert!(
bar.contains(&app.workspace.active().display_name()),
"the open file is missing: {bar}"
);
}
#[test]
fn each_page_draws_its_own_panels_and_no_others() {
let mut app = app();
for page in crate::app::Page::ALL {
app.open_page(page);
let text = screen(&app, 160, 48);
for panel in page.panels() {
let shown = text.contains(panel.title());
assert!(shown, "{panel} is nowhere on the {page} page:\n{text}");
}
}
}
#[test]
fn the_status_bar_shows_the_state_and_file() {
let app = app();
let rows = render(&app, 160, 48);
let status = rows.last().expect("a status bar");
assert!(status.contains("untitled"), "the file on show: {status}");
assert!(status.contains("idle"), "debugger state: {status}");
}
#[test]
fn source_text_reaches_the_screen() {
let mut app = app();
app.workspace
.active_mut()
.reload("section .text\n_start:\n mov rax, 60\n");
let text = screen(&app, 160, 48);
assert!(text.contains("mov rax, 60"), "the source is not drawn");
assert!(text.contains("_start:"));
}
#[test]
fn panels_with_no_data_explain_themselves() {
let mut app = app();
app.open_page(crate::app::Page::Debug);
let text = screen(&app, 160, 48);
assert!(
text.contains("No debug session"),
"the register panel should explain why it is empty"
);
}
#[test]
fn a_narrow_terminal_still_renders_the_focused_panel() {
let app = app();
let text = screen(&app, 60, 20);
assert!(text.contains("Editor"), "the focused panel is missing");
}
#[test]
fn a_tiny_terminal_says_so_instead_of_drawing_rubbish() {
let app = app();
let text = screen(&app, 20, 8);
assert!(text.contains("too small"), "expected a message: {text}");
}
#[test]
fn every_size_renders_without_panicking() {
let app = app();
for width in [1u16, 5, 20, 40, 79, 80, 119, 120, 200] {
for height in [1u16, 3, 9, 10, 27, 28, 50] {
let _ = render(&app, width, height);
}
}
}
#[test]
fn every_panel_can_be_focused_and_drawn_at_every_size() {
let mut app = app();
for panel in crate::app::panel::Panel::ALL {
app.focus_panel(panel);
for (width, height) in [(160u16, 48u16), (100, 30), (60, 20), (45, 12)] {
let text = render(&app, width, height).join("\n");
assert!(!text.is_empty(), "{panel} drew nothing at {width}x{height}");
}
}
}
#[test]
fn the_palette_appears_over_the_interface() {
let mut app = app();
app.apply(&crate::command::Command::OpenPalette);
let text = screen(&app, 160, 48);
assert!(text.contains("Command palette"));
assert!(
text.contains("New file"),
"commands should be listed:\n{text}"
);
assert!(text.contains("ctrl+n"), "bindings should be listed");
}
#[test]
fn the_palette_shows_the_shortcut_for_a_command() {
let mut app = app();
app.apply(&crate::command::Command::OpenPalette);
let text = screen(&app, 160, 48);
assert!(text.contains("F6") || text.contains("ctrl+"), "{text}");
}
#[test]
fn a_prompt_appears_with_its_hint() {
let mut app = app();
app.apply(&crate::command::Command::GoToAddress);
let text = screen(&app, 160, 48);
assert!(text.contains("Go to address"));
assert!(text.contains("rsp-0x20"), "the hint should be shown");
}
#[test]
fn an_error_status_is_drawn_with_its_message() {
let mut app = app();
app.status = crate::app::Status::error("something went wrong");
let rows = render(&app, 160, 48);
let status = rows.last().expect("status bar");
assert!(status.contains("something went wrong"), "{status}");
}
#[test]
fn a_long_status_message_is_truncated_rather_than_wrapping() {
let mut app = app();
app.status = crate::app::Status::info("x".repeat(500));
let rows = render(&app, 100, 30);
assert_eq!(rows.len(), 30, "the layout must not grow");
assert_eq!(
rows.last().map(|row| row.chars().count()),
Some(100),
"the status bar stays exactly one row wide"
);
assert!(
rows.last().is_some_and(|row| row.contains("F3")),
"a truncated message must say how to reveal the rest"
);
}
#[test]
fn breakpoints_are_marked_in_the_gutter() {
let mut app = app();
app.workspace
.active_mut()
.set_path("/tmp/ratasm-test/main.asm");
app.workspace.active_mut().reload("nop\nnop\nnop\n");
app.apply(&crate::command::Command::ToggleBreakpoint);
if let crate::app::mode::Mode::Prompt(prompt) = &mut app.mode {
prompt.set_text("2");
}
app.accept_prompt();
let text = screen(&app, 160, 48);
let glyph = app.theme.symbols().breakpoint;
assert!(text.contains(glyph), "the breakpoint marker is missing");
}
#[test]
fn a_revealed_line_of_a_long_file_is_scrolled_into_view() {
let mut app = app();
let source: String = (1..=200).map(|n| format!(" nop ; {n}\n")).collect();
app.workspace.active_mut().reload(&source);
app.workspace.active_mut().reveal(149);
sync_scroll(&mut app, 160, 48);
let text = screen(&app, 160, 48);
assert!(text.contains("; 150"), "line 150 should be on screen");
}
#[test]
fn rendering_does_not_change_the_application() {
let mut app = app();
app.workspace.active_mut().reload(" mov rax, 1\n");
let before = (
app.focus,
app.workspace.active().scroll_line(),
app.status.text.clone(),
app.workspace.active().buffer().to_text(),
);
let _ = render(&app, 160, 48);
let after = (
app.focus,
app.workspace.active().scroll_line(),
app.status.text.clone(),
app.workspace.active().buffer().to_text(),
);
assert_eq!(before, after);
}
#[test]
fn every_theme_renders() {
let mut app = app();
for kind in crate::ui::theme::ThemeKind::ALL {
app.theme.set_kind(kind);
let text = screen(&app, 160, 48);
assert!(text.contains("Editor"), "{kind} failed to render");
}
}
#[test]
fn an_ascii_only_terminal_renders_without_unicode_glyphs() {
let mut app = app();
app.theme = crate::ui::Theme::new(crate::ui::theme::ThemeKind::Ansi16, false);
app.workspace
.active_mut()
.set_path("/tmp/ratasm-test/main.asm");
app.workspace.active_mut().reload("nop\n");
app.apply(&crate::command::Command::ToggleBreakpoint);
if let crate::app::mode::Mode::Prompt(prompt) = &mut app.mode {
prompt.set_text("1");
}
app.accept_prompt();
let text = screen(&app, 160, 48);
assert!(text.contains('*'), "the ASCII breakpoint marker is missing");
}
}