refactor: split common/mod.rs into per-widget modules
Split the 816-line monolithic common/mod.rs into 5 focused files: - breadcrumb.rs (208 lines) — navigation trail with truncation - status_bar.rs (173 lines) — bottom bar with key hints - loading.rs (179 lines) — spinner indicators - error_toast.rs (124 lines) — floating error messages - help_overlay.rs (173 lines) — centered keybinding modal mod.rs is now a 17-line re-export hub. Public API unchanged. Filed beads for remaining large file splits: - bd-14q8: commands.rs (807 lines) -> commands/ module - bd-29wn: app.rs (712 lines) -> app/ module - bd-2cbw: who.rs (3742 lines) -> who/ module 172 tests pass, clippy clean, fmt clean.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1 +1 @@
|
||||
bd-26f2
|
||||
bd-2cbw
|
||||
|
||||
208
crates/lore-tui/src/view/common/breadcrumb.rs
Normal file
208
crates/lore-tui/src/view/common/breadcrumb.rs
Normal file
@@ -0,0 +1,208 @@
|
||||
//! Navigation breadcrumb trail ("Dashboard > Issues > #42").
|
||||
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::navigation::NavigationStack;
|
||||
|
||||
/// Render the navigation breadcrumb trail.
|
||||
///
|
||||
/// Shows "Dashboard > Issues > Issue" with " > " separators. When the
|
||||
/// trail exceeds the available width, entries are truncated from the left
|
||||
/// with a leading "...".
|
||||
pub fn render_breadcrumb(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
nav: &NavigationStack,
|
||||
text_color: PackedRgba,
|
||||
muted_color: PackedRgba,
|
||||
) {
|
||||
if area.height == 0 || area.width < 3 {
|
||||
return;
|
||||
}
|
||||
|
||||
let crumbs = nav.breadcrumbs();
|
||||
let separator = " > ";
|
||||
|
||||
// Build the full breadcrumb string and calculate width.
|
||||
let full: String = crumbs.join(separator);
|
||||
let max_width = area.width as usize;
|
||||
|
||||
let display = if full.len() <= max_width {
|
||||
full
|
||||
} else {
|
||||
// Truncate from the left: show "... > last_crumbs"
|
||||
truncate_breadcrumb_left(&crumbs, separator, max_width)
|
||||
};
|
||||
|
||||
let base = Cell {
|
||||
fg: text_color,
|
||||
..Cell::default()
|
||||
};
|
||||
let muted = Cell {
|
||||
fg: muted_color,
|
||||
..Cell::default()
|
||||
};
|
||||
|
||||
// Render each segment with separators in muted color.
|
||||
let mut x = area.x;
|
||||
let max_x = area.x.saturating_add(area.width);
|
||||
|
||||
if let Some(rest) = display.strip_prefix("...") {
|
||||
// Render ellipsis in muted, then the rest
|
||||
x = frame.print_text_clipped(x, area.y, "...", muted, max_x);
|
||||
if !rest.is_empty() {
|
||||
render_crumb_segments(frame, x, area.y, rest, separator, base, muted, max_x);
|
||||
}
|
||||
} else {
|
||||
render_crumb_segments(frame, x, area.y, &display, separator, base, muted, max_x);
|
||||
}
|
||||
}
|
||||
|
||||
/// Render breadcrumb text with separators in muted color.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_crumb_segments(
|
||||
frame: &mut Frame<'_>,
|
||||
start_x: u16,
|
||||
y: u16,
|
||||
text: &str,
|
||||
separator: &str,
|
||||
base: Cell,
|
||||
muted: Cell,
|
||||
max_x: u16,
|
||||
) {
|
||||
let mut x = start_x;
|
||||
let parts: Vec<&str> = text.split(separator).collect();
|
||||
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
if i > 0 {
|
||||
x = frame.print_text_clipped(x, y, separator, muted, max_x);
|
||||
}
|
||||
x = frame.print_text_clipped(x, y, part, base, max_x);
|
||||
if x >= max_x {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Truncate breadcrumb from the left to fit within max_width.
|
||||
fn truncate_breadcrumb_left(crumbs: &[&str], separator: &str, max_width: usize) -> String {
|
||||
let ellipsis = "...";
|
||||
|
||||
// Try showing progressively fewer crumbs from the right.
|
||||
for skip in 1..crumbs.len() {
|
||||
let tail = &crumbs[skip..];
|
||||
let tail_str: String = tail.join(separator);
|
||||
let candidate = format!("{ellipsis}{separator}{tail_str}");
|
||||
if candidate.len() <= max_width {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: just the current screen truncated.
|
||||
let last = crumbs.last().unwrap_or(&"");
|
||||
if last.len() + ellipsis.len() <= max_width {
|
||||
return format!("{ellipsis}{last}");
|
||||
}
|
||||
|
||||
// Truly tiny terminal: just ellipsis.
|
||||
ellipsis.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::message::Screen;
|
||||
use crate::navigation::NavigationStack;
|
||||
use ftui::render::grapheme_pool::GraphemePool;
|
||||
|
||||
macro_rules! with_frame {
|
||||
($width:expr, $height:expr, |$frame:ident| $body:block) => {{
|
||||
let mut pool = GraphemePool::new();
|
||||
let mut $frame = Frame::new($width, $height, &mut pool);
|
||||
$body
|
||||
}};
|
||||
}
|
||||
|
||||
fn white() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
fn gray() -> PackedRgba {
|
||||
PackedRgba::rgb(0x80, 0x80, 0x80)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_single_screen() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let nav = NavigationStack::new();
|
||||
render_breadcrumb(&mut frame, Rect::new(0, 0, 80, 1), &nav, white(), gray());
|
||||
|
||||
let cell = frame.buffer.get(0, 0).unwrap();
|
||||
assert!(
|
||||
cell.content.as_char() == Some('D'),
|
||||
"Expected 'D' at (0,0), got {:?}",
|
||||
cell.content.as_char()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_multi_screen() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let mut nav = NavigationStack::new();
|
||||
nav.push(Screen::IssueList);
|
||||
render_breadcrumb(&mut frame, Rect::new(0, 0, 80, 1), &nav, white(), gray());
|
||||
|
||||
let d = frame.buffer.get(0, 0).unwrap();
|
||||
assert_eq!(d.content.as_char(), Some('D'));
|
||||
|
||||
// "Dashboard > Issues" = 'I' at 12
|
||||
let i_cell = frame.buffer.get(12, 0).unwrap();
|
||||
assert_eq!(i_cell.content.as_char(), Some('I'));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_truncation() {
|
||||
let crumbs = vec!["Dashboard", "Issues", "Issue"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 20);
|
||||
assert!(
|
||||
result.starts_with("..."),
|
||||
"Expected ellipsis prefix, got: {result}"
|
||||
);
|
||||
assert!(result.len() <= 20, "Result too long: {result}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_zero_height_noop() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let nav = NavigationStack::new();
|
||||
render_breadcrumb(&mut frame, Rect::new(0, 0, 80, 0), &nav, white(), gray());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncate_breadcrumb_fits() {
|
||||
let crumbs = vec!["A", "B"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 100);
|
||||
assert!(result.contains("..."), "Should always add ellipsis");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncate_breadcrumb_single_entry() {
|
||||
let crumbs = vec!["Dashboard"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 5);
|
||||
assert_eq!(result, "...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncate_breadcrumb_shows_last_entries() {
|
||||
let crumbs = vec!["Dashboard", "Issues", "Issue Detail"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 30);
|
||||
assert!(result.starts_with("..."));
|
||||
assert!(result.contains("Issue Detail"));
|
||||
}
|
||||
}
|
||||
124
crates/lore-tui/src/view/common/error_toast.rs
Normal file
124
crates/lore-tui/src/view/common/error_toast.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
//! Floating error toast at bottom-right.
|
||||
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
/// Render a floating error toast at the bottom-right of the area.
|
||||
///
|
||||
/// The toast has a colored background and truncates long messages.
|
||||
pub fn render_error_toast(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
msg: &str,
|
||||
error_bg: PackedRgba,
|
||||
error_fg: PackedRgba,
|
||||
) {
|
||||
if area.height < 3 || area.width < 10 || msg.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Toast dimensions: message + padding, max 60 chars or half screen.
|
||||
let max_toast_width = (area.width / 2).clamp(20, 60);
|
||||
let toast_text = if msg.len() as u16 > max_toast_width.saturating_sub(4) {
|
||||
let trunc_len = max_toast_width.saturating_sub(7) as usize;
|
||||
format!(" {}... ", &msg[..trunc_len.min(msg.len())])
|
||||
} else {
|
||||
format!(" {msg} ")
|
||||
};
|
||||
let toast_width = toast_text.len() as u16;
|
||||
let toast_height: u16 = 1;
|
||||
|
||||
// Position: bottom-right with 1-cell margin.
|
||||
let x = area.right().saturating_sub(toast_width + 1);
|
||||
let y = area.bottom().saturating_sub(toast_height + 1);
|
||||
|
||||
let toast_rect = Rect::new(x, y, toast_width, toast_height);
|
||||
|
||||
// Fill background.
|
||||
let bg_cell = Cell {
|
||||
bg: error_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.draw_rect_filled(toast_rect, bg_cell);
|
||||
|
||||
// Render text.
|
||||
let text_cell = Cell {
|
||||
fg: error_fg,
|
||||
bg: error_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(x, y, &toast_text, text_cell, area.right());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ftui::render::grapheme_pool::GraphemePool;
|
||||
|
||||
macro_rules! with_frame {
|
||||
($width:expr, $height:expr, |$frame:ident| $body:block) => {{
|
||||
let mut pool = GraphemePool::new();
|
||||
let mut $frame = Frame::new($width, $height, &mut pool);
|
||||
$body
|
||||
}};
|
||||
}
|
||||
|
||||
fn white() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
fn red_bg() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0x00, 0x00)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_toast_renders() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_error_toast(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
"Database is busy",
|
||||
red_bg(),
|
||||
white(),
|
||||
);
|
||||
|
||||
let y = 22u16;
|
||||
let has_content = (40..80u16).any(|x| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
});
|
||||
assert!(has_content, "Expected error toast at bottom-right");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_toast_empty_message_noop() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_error_toast(&mut frame, Rect::new(0, 0, 80, 24), "", red_bg(), white());
|
||||
|
||||
let has_content = (0..80u16).any(|x| {
|
||||
(0..24u16).any(|y| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
})
|
||||
});
|
||||
assert!(!has_content, "Empty message should render nothing");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_toast_truncates_long_message() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
let long_msg = "A".repeat(200);
|
||||
render_error_toast(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&long_msg,
|
||||
red_bg(),
|
||||
white(),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
173
crates/lore-tui/src/view/common/help_overlay.rs
Normal file
173
crates/lore-tui/src/view/common/help_overlay.rs
Normal file
@@ -0,0 +1,173 @@
|
||||
//! Centered modal listing keybindings for the current screen.
|
||||
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::commands::CommandRegistry;
|
||||
use crate::message::Screen;
|
||||
|
||||
/// Render a centered help overlay listing keybindings for the current screen.
|
||||
///
|
||||
/// The overlay is a bordered modal that lists all commands from the
|
||||
/// registry that are available on the current screen.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_help_overlay(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
registry: &CommandRegistry,
|
||||
screen: &Screen,
|
||||
border_color: PackedRgba,
|
||||
text_color: PackedRgba,
|
||||
muted_color: PackedRgba,
|
||||
scroll_offset: usize,
|
||||
) {
|
||||
if area.height < 5 || area.width < 20 {
|
||||
return;
|
||||
}
|
||||
|
||||
// Overlay dimensions: 60% of screen, capped.
|
||||
let overlay_width = (area.width * 3 / 5).clamp(30, 70);
|
||||
let overlay_height = (area.height * 3 / 5).clamp(8, 30);
|
||||
|
||||
let overlay_x = area.x + (area.width.saturating_sub(overlay_width)) / 2;
|
||||
let overlay_y = area.y + (area.height.saturating_sub(overlay_height)) / 2;
|
||||
let overlay_rect = Rect::new(overlay_x, overlay_y, overlay_width, overlay_height);
|
||||
|
||||
// Draw border.
|
||||
let border_cell = Cell {
|
||||
fg: border_color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.draw_border(
|
||||
overlay_rect,
|
||||
ftui::render::drawing::BorderChars::ROUNDED,
|
||||
border_cell,
|
||||
);
|
||||
|
||||
// Title.
|
||||
let title = " Help (? to close) ";
|
||||
let title_x = overlay_x + (overlay_width.saturating_sub(title.len() as u16)) / 2;
|
||||
let title_cell = Cell {
|
||||
fg: border_color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(title_x, overlay_y, title, title_cell, overlay_rect.right());
|
||||
|
||||
// Inner content area (inside border).
|
||||
let inner = Rect::new(
|
||||
overlay_x + 2,
|
||||
overlay_y + 1,
|
||||
overlay_width.saturating_sub(4),
|
||||
overlay_height.saturating_sub(2),
|
||||
);
|
||||
|
||||
// Get commands for this screen.
|
||||
let commands = registry.help_entries(screen);
|
||||
let visible_lines = inner.height as usize;
|
||||
|
||||
let key_cell = Cell {
|
||||
fg: text_color,
|
||||
..Cell::default()
|
||||
};
|
||||
let desc_cell = Cell {
|
||||
fg: muted_color,
|
||||
..Cell::default()
|
||||
};
|
||||
|
||||
for (i, cmd) in commands.iter().skip(scroll_offset).enumerate() {
|
||||
if i >= visible_lines {
|
||||
break;
|
||||
}
|
||||
let y = inner.y + i as u16;
|
||||
|
||||
// Key binding label (left).
|
||||
let key_label = cmd
|
||||
.keybinding
|
||||
.as_ref()
|
||||
.map_or_else(String::new, |kb| kb.display());
|
||||
let label_end = frame.print_text_clipped(inner.x, y, &key_label, key_cell, inner.right());
|
||||
|
||||
// Spacer + description (right).
|
||||
let desc_x = label_end.saturating_add(2);
|
||||
if desc_x < inner.right() {
|
||||
frame.print_text_clipped(desc_x, y, cmd.help_text, desc_cell, inner.right());
|
||||
}
|
||||
}
|
||||
|
||||
// Scroll indicator if needed.
|
||||
if commands.len() > visible_lines + scroll_offset {
|
||||
let indicator = format!("({}/{})", scroll_offset + visible_lines, commands.len());
|
||||
let ind_x = inner.right().saturating_sub(indicator.len() as u16);
|
||||
let ind_y = overlay_rect.bottom().saturating_sub(1);
|
||||
frame.print_text_clipped(ind_x, ind_y, &indicator, desc_cell, overlay_rect.right());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::commands::build_registry;
|
||||
use crate::message::Screen;
|
||||
use ftui::render::grapheme_pool::GraphemePool;
|
||||
|
||||
macro_rules! with_frame {
|
||||
($width:expr, $height:expr, |$frame:ident| $body:block) => {{
|
||||
let mut pool = GraphemePool::new();
|
||||
let mut $frame = Frame::new($width, $height, &mut pool);
|
||||
$body
|
||||
}};
|
||||
}
|
||||
|
||||
fn white() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
fn gray() -> PackedRgba {
|
||||
PackedRgba::rgb(0x80, 0x80, 0x80)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_help_overlay_renders_border() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
let registry = build_registry();
|
||||
render_help_overlay(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
gray(),
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
// The overlay should have non-empty cells in the center area.
|
||||
let has_content = (20..60u16).any(|x| {
|
||||
(8..16u16).any(|y| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
})
|
||||
});
|
||||
assert!(has_content, "Expected help overlay in center area");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_help_overlay_tiny_terminal_noop() {
|
||||
with_frame!(15, 4, |frame| {
|
||||
let registry = build_registry();
|
||||
render_help_overlay(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 15, 4),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
gray(),
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
179
crates/lore-tui/src/view/common/loading.rs
Normal file
179
crates/lore-tui/src/view/common/loading.rs
Normal file
@@ -0,0 +1,179 @@
|
||||
//! Loading spinner indicators (full-screen and corner).
|
||||
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::state::LoadState;
|
||||
|
||||
/// Braille spinner frames for loading animation.
|
||||
const SPINNER_FRAMES: &[char] = &['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
||||
|
||||
/// Select spinner frame from tick count.
|
||||
#[must_use]
|
||||
pub(crate) fn spinner_char(tick: u64) -> char {
|
||||
SPINNER_FRAMES[(tick as usize) % SPINNER_FRAMES.len()]
|
||||
}
|
||||
|
||||
/// Render a loading indicator.
|
||||
///
|
||||
/// - `LoadingInitial`: centered full-screen spinner with "Loading..."
|
||||
/// - `Refreshing`: subtle spinner in top-right corner
|
||||
/// - Other states: no-op
|
||||
pub fn render_loading(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
load_state: &LoadState,
|
||||
text_color: PackedRgba,
|
||||
muted_color: PackedRgba,
|
||||
tick: u64,
|
||||
) {
|
||||
match load_state {
|
||||
LoadState::LoadingInitial => {
|
||||
render_centered_spinner(frame, area, "Loading...", text_color, tick);
|
||||
}
|
||||
LoadState::Refreshing => {
|
||||
render_corner_spinner(frame, area, muted_color, tick);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a centered spinner with message.
|
||||
fn render_centered_spinner(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
message: &str,
|
||||
color: PackedRgba,
|
||||
tick: u64,
|
||||
) {
|
||||
if area.height == 0 || area.width < 5 {
|
||||
return;
|
||||
}
|
||||
|
||||
let spinner = spinner_char(tick);
|
||||
let text = format!("{spinner} {message}");
|
||||
let text_len = text.len() as u16;
|
||||
|
||||
// Center horizontally and vertically.
|
||||
let x = area
|
||||
.x
|
||||
.saturating_add(area.width.saturating_sub(text_len) / 2);
|
||||
let y = area.y.saturating_add(area.height / 2);
|
||||
|
||||
let cell = Cell {
|
||||
fg: color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(x, y, &text, cell, area.right());
|
||||
}
|
||||
|
||||
/// Render a subtle spinner in the top-right corner.
|
||||
fn render_corner_spinner(frame: &mut Frame<'_>, area: Rect, color: PackedRgba, tick: u64) {
|
||||
if area.width < 2 || area.height == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let spinner = spinner_char(tick);
|
||||
let x = area.right().saturating_sub(2);
|
||||
let y = area.y;
|
||||
|
||||
let cell = Cell {
|
||||
fg: color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(x, y, &spinner.to_string(), cell, area.right());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ftui::render::grapheme_pool::GraphemePool;
|
||||
|
||||
macro_rules! with_frame {
|
||||
($width:expr, $height:expr, |$frame:ident| $body:block) => {{
|
||||
let mut pool = GraphemePool::new();
|
||||
let mut $frame = Frame::new($width, $height, &mut pool);
|
||||
$body
|
||||
}};
|
||||
}
|
||||
|
||||
fn white() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
fn gray() -> PackedRgba {
|
||||
PackedRgba::rgb(0x80, 0x80, 0x80)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_loading_initial_renders_spinner() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_loading(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&LoadState::LoadingInitial,
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
let center_y = 12u16;
|
||||
let has_content = (0..80u16).any(|x| {
|
||||
let cell = frame.buffer.get(x, center_y).unwrap();
|
||||
!cell.is_empty()
|
||||
});
|
||||
assert!(has_content, "Expected loading spinner at center row");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_loading_refreshing_renders_corner() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_loading(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&LoadState::Refreshing,
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
let cell = frame.buffer.get(78, 0).unwrap();
|
||||
assert!(!cell.is_empty(), "Expected corner spinner");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_loading_idle_noop() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_loading(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&LoadState::Idle,
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
let has_content = (0..80u16).any(|x| {
|
||||
(0..24u16).any(|y| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
})
|
||||
});
|
||||
assert!(!has_content, "Idle state should render nothing");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spinner_animation_cycles() {
|
||||
let frame0 = spinner_char(0);
|
||||
let frame1 = spinner_char(1);
|
||||
let frame_wrap = spinner_char(SPINNER_FRAMES.len() as u64);
|
||||
|
||||
assert_ne!(frame0, frame1, "Adjacent frames should differ");
|
||||
assert_eq!(frame0, frame_wrap, "Should wrap around");
|
||||
}
|
||||
}
|
||||
@@ -1,816 +1,17 @@
|
||||
#![allow(dead_code)] // Phase 1: consumed by screen views in Phase 2+
|
||||
|
||||
//! Common widgets shared across all TUI screens.
|
||||
//!
|
||||
//! These are pure rendering functions — they write directly into the
|
||||
//! Each widget is a pure rendering function — writes directly into the
|
||||
//! [`Frame`] buffer using ftui's `Draw` trait. No state mutation,
|
||||
//! no side effects.
|
||||
//!
|
||||
//! - [`render_breadcrumb`] — navigation trail ("Dashboard > Issues > #42")
|
||||
//! - [`render_status_bar`] — bottom bar with key hints and mode indicator
|
||||
//! - [`render_loading`] — full-screen spinner or subtle refresh indicator
|
||||
//! - [`render_error_toast`] — floating error message at bottom-right
|
||||
//! - [`render_help_overlay`] — centered modal listing keybindings
|
||||
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::commands::CommandRegistry;
|
||||
use crate::message::{InputMode, Screen};
|
||||
use crate::navigation::NavigationStack;
|
||||
use crate::state::LoadState;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Spinner frames
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Braille spinner frames for loading animation.
|
||||
const SPINNER_FRAMES: &[char] = &['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
|
||||
|
||||
/// Select spinner frame from tick count.
|
||||
#[must_use]
|
||||
fn spinner_char(tick: u64) -> char {
|
||||
SPINNER_FRAMES[(tick as usize) % SPINNER_FRAMES.len()]
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render_breadcrumb
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Render the navigation breadcrumb trail.
|
||||
///
|
||||
/// Shows "Dashboard > Issues > Issue" with " > " separators. When the
|
||||
/// trail exceeds the available width, entries are truncated from the left
|
||||
/// with a leading "...".
|
||||
pub fn render_breadcrumb(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
nav: &NavigationStack,
|
||||
text_color: PackedRgba,
|
||||
muted_color: PackedRgba,
|
||||
) {
|
||||
if area.height == 0 || area.width < 3 {
|
||||
return;
|
||||
}
|
||||
|
||||
let crumbs = nav.breadcrumbs();
|
||||
let separator = " > ";
|
||||
|
||||
// Build the full breadcrumb string and calculate width.
|
||||
let full: String = crumbs.join(separator);
|
||||
let max_width = area.width as usize;
|
||||
|
||||
let display = if full.len() <= max_width {
|
||||
full
|
||||
} else {
|
||||
// Truncate from the left: show "... > last_crumbs"
|
||||
truncate_breadcrumb_left(&crumbs, separator, max_width)
|
||||
};
|
||||
|
||||
let base = Cell {
|
||||
fg: text_color,
|
||||
..Cell::default()
|
||||
};
|
||||
let muted = Cell {
|
||||
fg: muted_color,
|
||||
..Cell::default()
|
||||
};
|
||||
|
||||
// Render each segment with separators in muted color.
|
||||
let mut x = area.x;
|
||||
let max_x = area.x.saturating_add(area.width);
|
||||
|
||||
if let Some(rest) = display.strip_prefix("...") {
|
||||
// Render ellipsis in muted, then the rest
|
||||
x = frame.print_text_clipped(x, area.y, "...", muted, max_x);
|
||||
if !rest.is_empty() {
|
||||
render_crumb_segments(frame, x, area.y, rest, separator, base, muted, max_x);
|
||||
}
|
||||
} else {
|
||||
render_crumb_segments(frame, x, area.y, &display, separator, base, muted, max_x);
|
||||
}
|
||||
}
|
||||
|
||||
/// Render breadcrumb text with separators in muted color.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn render_crumb_segments(
|
||||
frame: &mut Frame<'_>,
|
||||
start_x: u16,
|
||||
y: u16,
|
||||
text: &str,
|
||||
separator: &str,
|
||||
base: Cell,
|
||||
muted: Cell,
|
||||
max_x: u16,
|
||||
) {
|
||||
let mut x = start_x;
|
||||
let parts: Vec<&str> = text.split(separator).collect();
|
||||
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
if i > 0 {
|
||||
x = frame.print_text_clipped(x, y, separator, muted, max_x);
|
||||
}
|
||||
x = frame.print_text_clipped(x, y, part, base, max_x);
|
||||
if x >= max_x {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Truncate breadcrumb from the left to fit within max_width.
|
||||
fn truncate_breadcrumb_left(crumbs: &[&str], separator: &str, max_width: usize) -> String {
|
||||
let ellipsis = "...";
|
||||
|
||||
// Try showing progressively fewer crumbs from the right.
|
||||
for skip in 1..crumbs.len() {
|
||||
let tail = &crumbs[skip..];
|
||||
let tail_str: String = tail.join(separator);
|
||||
let candidate = format!("{ellipsis}{separator}{tail_str}");
|
||||
if candidate.len() <= max_width {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
// Last resort: just the current screen truncated.
|
||||
let last = crumbs.last().unwrap_or(&"");
|
||||
if last.len() + ellipsis.len() <= max_width {
|
||||
return format!("{ellipsis}{last}");
|
||||
}
|
||||
|
||||
// Truly tiny terminal: just ellipsis.
|
||||
ellipsis.to_string()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render_status_bar
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Render the bottom status bar with key hints and mode indicator.
|
||||
///
|
||||
/// Layout: `[mode] ─── [key hints]`
|
||||
///
|
||||
/// Key hints are sourced from the [`CommandRegistry`] filtered to the
|
||||
/// current screen, showing only the most important bindings.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_status_bar(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
registry: &CommandRegistry,
|
||||
screen: &Screen,
|
||||
mode: &InputMode,
|
||||
bar_bg: PackedRgba,
|
||||
text_color: PackedRgba,
|
||||
accent_color: PackedRgba,
|
||||
) {
|
||||
if area.height == 0 || area.width < 5 {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fill the bar background.
|
||||
let bg_cell = Cell {
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.draw_rect_filled(area, bg_cell);
|
||||
|
||||
let mode_label = match mode {
|
||||
InputMode::Normal => "NORMAL",
|
||||
InputMode::Text => "INPUT",
|
||||
InputMode::Palette => "PALETTE",
|
||||
InputMode::GoPrefix { .. } => "g...",
|
||||
};
|
||||
|
||||
// Left side: mode indicator.
|
||||
let mode_cell = Cell {
|
||||
fg: accent_color,
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
let mut x = frame.print_text_clipped(
|
||||
area.x.saturating_add(1),
|
||||
area.y,
|
||||
mode_label,
|
||||
mode_cell,
|
||||
area.right(),
|
||||
);
|
||||
|
||||
// Spacer.
|
||||
x = x.saturating_add(2);
|
||||
|
||||
// Right side: key hints from registry (formatted as "key:action").
|
||||
let hints = registry.status_hints(screen);
|
||||
let hint_cell = Cell {
|
||||
fg: text_color,
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
let key_cell = Cell {
|
||||
fg: accent_color,
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
|
||||
for hint in &hints {
|
||||
if x >= area.right().saturating_sub(1) {
|
||||
break;
|
||||
}
|
||||
// Split "q:quit" into key part and description part.
|
||||
if let Some((key_part, desc_part)) = hint.split_once(':') {
|
||||
x = frame.print_text_clipped(x, area.y, key_part, key_cell, area.right());
|
||||
x = frame.print_text_clipped(x, area.y, ":", hint_cell, area.right());
|
||||
x = frame.print_text_clipped(x, area.y, desc_part, hint_cell, area.right());
|
||||
} else {
|
||||
x = frame.print_text_clipped(x, area.y, hint, hint_cell, area.right());
|
||||
}
|
||||
x = x.saturating_add(2);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render_loading
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Render a loading indicator.
|
||||
///
|
||||
/// - `LoadingInitial`: centered full-screen spinner with "Loading..."
|
||||
/// - `Refreshing`: subtle spinner in top-right corner
|
||||
/// - Other states: no-op
|
||||
pub fn render_loading(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
load_state: &LoadState,
|
||||
text_color: PackedRgba,
|
||||
muted_color: PackedRgba,
|
||||
tick: u64,
|
||||
) {
|
||||
match load_state {
|
||||
LoadState::LoadingInitial => {
|
||||
render_centered_spinner(frame, area, "Loading...", text_color, tick);
|
||||
}
|
||||
LoadState::Refreshing => {
|
||||
render_corner_spinner(frame, area, muted_color, tick);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Render a centered spinner with message.
|
||||
fn render_centered_spinner(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
message: &str,
|
||||
color: PackedRgba,
|
||||
tick: u64,
|
||||
) {
|
||||
if area.height == 0 || area.width < 5 {
|
||||
return;
|
||||
}
|
||||
|
||||
let spinner = spinner_char(tick);
|
||||
let text = format!("{spinner} {message}");
|
||||
let text_len = text.len() as u16;
|
||||
|
||||
// Center horizontally and vertically.
|
||||
let x = area
|
||||
.x
|
||||
.saturating_add(area.width.saturating_sub(text_len) / 2);
|
||||
let y = area.y.saturating_add(area.height / 2);
|
||||
|
||||
let cell = Cell {
|
||||
fg: color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(x, y, &text, cell, area.right());
|
||||
}
|
||||
|
||||
/// Render a subtle spinner in the top-right corner.
|
||||
fn render_corner_spinner(frame: &mut Frame<'_>, area: Rect, color: PackedRgba, tick: u64) {
|
||||
if area.width < 2 || area.height == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let spinner = spinner_char(tick);
|
||||
let x = area.right().saturating_sub(2);
|
||||
let y = area.y;
|
||||
|
||||
let cell = Cell {
|
||||
fg: color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(x, y, &spinner.to_string(), cell, area.right());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render_error_toast
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Render a floating error toast at the bottom-right of the area.
|
||||
///
|
||||
/// The toast has a colored background and truncates long messages.
|
||||
pub fn render_error_toast(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
msg: &str,
|
||||
error_bg: PackedRgba,
|
||||
error_fg: PackedRgba,
|
||||
) {
|
||||
if area.height < 3 || area.width < 10 || msg.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
// Toast dimensions: message + padding, max 60 chars or half screen.
|
||||
let max_toast_width = (area.width / 2).clamp(20, 60);
|
||||
let toast_text = if msg.len() as u16 > max_toast_width.saturating_sub(4) {
|
||||
let trunc_len = max_toast_width.saturating_sub(7) as usize;
|
||||
format!(" {}... ", &msg[..trunc_len.min(msg.len())])
|
||||
} else {
|
||||
format!(" {msg} ")
|
||||
};
|
||||
let toast_width = toast_text.len() as u16;
|
||||
let toast_height: u16 = 1;
|
||||
|
||||
// Position: bottom-right with 1-cell margin.
|
||||
let x = area.right().saturating_sub(toast_width + 1);
|
||||
let y = area.bottom().saturating_sub(toast_height + 1);
|
||||
|
||||
let toast_rect = Rect::new(x, y, toast_width, toast_height);
|
||||
|
||||
// Fill background.
|
||||
let bg_cell = Cell {
|
||||
bg: error_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.draw_rect_filled(toast_rect, bg_cell);
|
||||
|
||||
// Render text.
|
||||
let text_cell = Cell {
|
||||
fg: error_fg,
|
||||
bg: error_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(x, y, &toast_text, text_cell, area.right());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// render_help_overlay
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Render a centered help overlay listing keybindings for the current screen.
|
||||
///
|
||||
/// The overlay is a bordered modal that lists all commands from the
|
||||
/// registry that are available on the current screen.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_help_overlay(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
registry: &CommandRegistry,
|
||||
screen: &Screen,
|
||||
border_color: PackedRgba,
|
||||
text_color: PackedRgba,
|
||||
muted_color: PackedRgba,
|
||||
scroll_offset: usize,
|
||||
) {
|
||||
if area.height < 5 || area.width < 20 {
|
||||
return;
|
||||
}
|
||||
|
||||
// Overlay dimensions: 60% of screen, capped.
|
||||
let overlay_width = (area.width * 3 / 5).clamp(30, 70);
|
||||
let overlay_height = (area.height * 3 / 5).clamp(8, 30);
|
||||
|
||||
let overlay_x = area.x + (area.width.saturating_sub(overlay_width)) / 2;
|
||||
let overlay_y = area.y + (area.height.saturating_sub(overlay_height)) / 2;
|
||||
let overlay_rect = Rect::new(overlay_x, overlay_y, overlay_width, overlay_height);
|
||||
|
||||
// Draw border.
|
||||
let border_cell = Cell {
|
||||
fg: border_color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.draw_border(
|
||||
overlay_rect,
|
||||
ftui::render::drawing::BorderChars::ROUNDED,
|
||||
border_cell,
|
||||
);
|
||||
|
||||
// Title.
|
||||
let title = " Help (? to close) ";
|
||||
let title_x = overlay_x + (overlay_width.saturating_sub(title.len() as u16)) / 2;
|
||||
let title_cell = Cell {
|
||||
fg: border_color,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.print_text_clipped(title_x, overlay_y, title, title_cell, overlay_rect.right());
|
||||
|
||||
// Inner content area (inside border).
|
||||
let inner = Rect::new(
|
||||
overlay_x + 2,
|
||||
overlay_y + 1,
|
||||
overlay_width.saturating_sub(4),
|
||||
overlay_height.saturating_sub(2),
|
||||
);
|
||||
|
||||
// Get commands for this screen.
|
||||
let commands = registry.help_entries(screen);
|
||||
let visible_lines = inner.height as usize;
|
||||
|
||||
let key_cell = Cell {
|
||||
fg: text_color,
|
||||
..Cell::default()
|
||||
};
|
||||
let desc_cell = Cell {
|
||||
fg: muted_color,
|
||||
..Cell::default()
|
||||
};
|
||||
|
||||
for (i, cmd) in commands.iter().skip(scroll_offset).enumerate() {
|
||||
if i >= visible_lines {
|
||||
break;
|
||||
}
|
||||
let y = inner.y + i as u16;
|
||||
|
||||
// Key binding label (left).
|
||||
let key_label = cmd
|
||||
.keybinding
|
||||
.as_ref()
|
||||
.map_or_else(String::new, |kb| kb.display());
|
||||
let label_end = frame.print_text_clipped(inner.x, y, &key_label, key_cell, inner.right());
|
||||
|
||||
// Spacer + description (right).
|
||||
let desc_x = label_end.saturating_add(2);
|
||||
if desc_x < inner.right() {
|
||||
frame.print_text_clipped(desc_x, y, cmd.help_text, desc_cell, inner.right());
|
||||
}
|
||||
}
|
||||
|
||||
// Scroll indicator if needed.
|
||||
if commands.len() > visible_lines + scroll_offset {
|
||||
let indicator = format!("({}/{})", scroll_offset + visible_lines, commands.len());
|
||||
let ind_x = inner.right().saturating_sub(indicator.len() as u16);
|
||||
let ind_y = overlay_rect.bottom().saturating_sub(1);
|
||||
frame.print_text_clipped(ind_x, ind_y, &indicator, desc_cell, overlay_rect.right());
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::commands::build_registry;
|
||||
use crate::message::Screen;
|
||||
use crate::navigation::NavigationStack;
|
||||
use crate::state::LoadState;
|
||||
use ftui::render::grapheme_pool::GraphemePool;
|
||||
|
||||
macro_rules! with_frame {
|
||||
($width:expr, $height:expr, |$frame:ident| $body:block) => {{
|
||||
let mut pool = GraphemePool::new();
|
||||
let mut $frame = Frame::new($width, $height, &mut pool);
|
||||
$body
|
||||
}};
|
||||
}
|
||||
|
||||
fn white() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
fn gray() -> PackedRgba {
|
||||
PackedRgba::rgb(0x80, 0x80, 0x80)
|
||||
}
|
||||
|
||||
fn red_bg() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0x00, 0x00)
|
||||
}
|
||||
|
||||
// --- Breadcrumb tests ---
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_single_screen() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let nav = NavigationStack::new(); // Dashboard only
|
||||
render_breadcrumb(&mut frame, Rect::new(0, 0, 80, 1), &nav, white(), gray());
|
||||
|
||||
// Verify "Dashboard" was rendered by checking first cell.
|
||||
let cell = frame.buffer.get(0, 0).unwrap();
|
||||
assert!(
|
||||
cell.content.as_char() == Some('D'),
|
||||
"Expected 'D' at (0,0), got {:?}",
|
||||
cell.content.as_char()
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_multi_screen() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let mut nav = NavigationStack::new();
|
||||
nav.push(Screen::IssueList);
|
||||
render_breadcrumb(&mut frame, Rect::new(0, 0, 80, 1), &nav, white(), gray());
|
||||
|
||||
// Should render "Dashboard > Issues"
|
||||
// Check 'D' at start and 'I' after separator.
|
||||
let d = frame.buffer.get(0, 0).unwrap();
|
||||
assert_eq!(d.content.as_char(), Some('D'));
|
||||
|
||||
// "Dashboard > Issues" = 'D' at 0, ' > ' at 9, 'I' at 12
|
||||
let i_cell = frame.buffer.get(12, 0).unwrap();
|
||||
assert_eq!(i_cell.content.as_char(), Some('I'));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_truncation() {
|
||||
// Very narrow terminal — should show "..." prefix.
|
||||
let crumbs = vec!["Dashboard", "Issues", "Issue"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 20);
|
||||
assert!(
|
||||
result.starts_with("..."),
|
||||
"Expected ellipsis prefix, got: {result}"
|
||||
);
|
||||
assert!(result.len() <= 20, "Result too long: {result}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_breadcrumb_zero_height_noop() {
|
||||
// Frame requires height >= 1, but Rect can have height=0.
|
||||
with_frame!(80, 1, |frame| {
|
||||
let nav = NavigationStack::new();
|
||||
// Should not panic — early return for zero-height area.
|
||||
render_breadcrumb(&mut frame, Rect::new(0, 0, 80, 0), &nav, white(), gray());
|
||||
});
|
||||
}
|
||||
|
||||
// --- Status bar tests ---
|
||||
|
||||
#[test]
|
||||
fn test_status_bar_renders_mode() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let registry = build_registry();
|
||||
render_status_bar(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 1),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
&InputMode::Normal,
|
||||
gray(),
|
||||
white(),
|
||||
white(),
|
||||
);
|
||||
|
||||
// "NORMAL" should appear starting at x=1.
|
||||
let n_cell = frame.buffer.get(1, 0).unwrap();
|
||||
assert_eq!(n_cell.content.as_char(), Some('N'));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status_bar_text_mode() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let registry = build_registry();
|
||||
render_status_bar(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 1),
|
||||
®istry,
|
||||
&Screen::Search,
|
||||
&InputMode::Text,
|
||||
gray(),
|
||||
white(),
|
||||
white(),
|
||||
);
|
||||
|
||||
// "INPUT" should appear at x=1.
|
||||
let i_cell = frame.buffer.get(1, 0).unwrap();
|
||||
assert_eq!(i_cell.content.as_char(), Some('I'));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status_bar_narrow_terminal() {
|
||||
with_frame!(4, 1, |frame| {
|
||||
let registry = build_registry();
|
||||
// Width < 5, should be a no-op.
|
||||
render_status_bar(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 4, 1),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
&InputMode::Normal,
|
||||
gray(),
|
||||
white(),
|
||||
white(),
|
||||
);
|
||||
// First cell should be empty (no-op).
|
||||
let cell = frame.buffer.get(0, 0).unwrap();
|
||||
assert!(cell.is_empty());
|
||||
});
|
||||
}
|
||||
|
||||
// --- Loading indicator tests ---
|
||||
|
||||
#[test]
|
||||
fn test_loading_initial_renders_spinner() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_loading(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&LoadState::LoadingInitial,
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
// Spinner should be centered at y=12.
|
||||
// The spinner char is at the center position.
|
||||
let center_y = 12u16;
|
||||
// Find any non-empty cell on the center row.
|
||||
let has_content = (0..80u16).any(|x| {
|
||||
let cell = frame.buffer.get(x, center_y).unwrap();
|
||||
!cell.is_empty()
|
||||
});
|
||||
assert!(has_content, "Expected loading spinner at center row");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_loading_refreshing_renders_corner() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_loading(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&LoadState::Refreshing,
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
// Corner spinner should be at (78, 0).
|
||||
let cell = frame.buffer.get(78, 0).unwrap();
|
||||
assert!(!cell.is_empty(), "Expected corner spinner");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_loading_idle_noop() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_loading(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&LoadState::Idle,
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
// All cells should be empty.
|
||||
let has_content = (0..80u16).any(|x| {
|
||||
(0..24u16).any(|y| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
})
|
||||
});
|
||||
assert!(!has_content, "Idle state should render nothing");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spinner_animation_cycles() {
|
||||
// Different tick values produce different spinner chars.
|
||||
let frame0 = spinner_char(0);
|
||||
let frame1 = spinner_char(1);
|
||||
let frame_wrap = spinner_char(SPINNER_FRAMES.len() as u64);
|
||||
|
||||
assert_ne!(frame0, frame1, "Adjacent frames should differ");
|
||||
assert_eq!(frame0, frame_wrap, "Should wrap around");
|
||||
}
|
||||
|
||||
// --- Error toast tests ---
|
||||
|
||||
#[test]
|
||||
fn test_error_toast_renders() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_error_toast(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
"Database is busy",
|
||||
red_bg(),
|
||||
white(),
|
||||
);
|
||||
|
||||
// Toast should be at bottom-right. Check row 22 (24-1-1).
|
||||
let y = 22u16;
|
||||
let has_content = (40..80u16).any(|x| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
});
|
||||
assert!(has_content, "Expected error toast at bottom-right");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_toast_empty_message_noop() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
render_error_toast(&mut frame, Rect::new(0, 0, 80, 24), "", red_bg(), white());
|
||||
|
||||
// No content should be rendered.
|
||||
let has_content = (0..80u16).any(|x| {
|
||||
(0..24u16).any(|y| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
})
|
||||
});
|
||||
assert!(!has_content, "Empty message should render nothing");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_error_toast_truncates_long_message() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
let long_msg = "A".repeat(200);
|
||||
// Should not panic and should truncate.
|
||||
render_error_toast(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
&long_msg,
|
||||
red_bg(),
|
||||
white(),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// --- Help overlay tests ---
|
||||
|
||||
#[test]
|
||||
fn test_help_overlay_renders_border() {
|
||||
with_frame!(80, 24, |frame| {
|
||||
let registry = build_registry();
|
||||
render_help_overlay(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 24),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
gray(),
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
|
||||
// The overlay should have non-empty cells in the center area.
|
||||
let has_content = (20..60u16).any(|x| {
|
||||
(8..16u16).any(|y| {
|
||||
let cell = frame.buffer.get(x, y).unwrap();
|
||||
!cell.is_empty()
|
||||
})
|
||||
});
|
||||
assert!(has_content, "Expected help overlay in center area");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_help_overlay_tiny_terminal_noop() {
|
||||
with_frame!(15, 4, |frame| {
|
||||
let registry = build_registry();
|
||||
// Too small — should be a no-op.
|
||||
render_help_overlay(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 15, 4),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
gray(),
|
||||
white(),
|
||||
gray(),
|
||||
0,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
// --- Truncation helper tests ---
|
||||
|
||||
#[test]
|
||||
fn test_truncate_breadcrumb_fits() {
|
||||
let crumbs = vec!["A", "B"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 100);
|
||||
// When it doesn't need truncation, this function is not called
|
||||
// in practice, but it should still work.
|
||||
assert!(result.contains("..."), "Should always add ellipsis");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncate_breadcrumb_single_entry() {
|
||||
let crumbs = vec!["Dashboard"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 5);
|
||||
assert_eq!(result, "...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_truncate_breadcrumb_shows_last_entries() {
|
||||
let crumbs = vec!["Dashboard", "Issues", "Issue Detail"];
|
||||
let result = truncate_breadcrumb_left(&crumbs, " > ", 30);
|
||||
assert!(result.starts_with("..."));
|
||||
assert!(result.contains("Issue Detail"));
|
||||
}
|
||||
}
|
||||
mod breadcrumb;
|
||||
mod error_toast;
|
||||
mod help_overlay;
|
||||
mod loading;
|
||||
mod status_bar;
|
||||
|
||||
pub use breadcrumb::render_breadcrumb;
|
||||
pub use error_toast::render_error_toast;
|
||||
pub use help_overlay::render_help_overlay;
|
||||
pub use loading::render_loading;
|
||||
pub use status_bar::render_status_bar;
|
||||
|
||||
173
crates/lore-tui/src/view/common/status_bar.rs
Normal file
173
crates/lore-tui/src/view/common/status_bar.rs
Normal file
@@ -0,0 +1,173 @@
|
||||
//! Bottom status bar with key hints and mode indicator.
|
||||
|
||||
use ftui::core::geometry::Rect;
|
||||
use ftui::render::cell::{Cell, PackedRgba};
|
||||
use ftui::render::drawing::Draw;
|
||||
use ftui::render::frame::Frame;
|
||||
|
||||
use crate::commands::CommandRegistry;
|
||||
use crate::message::{InputMode, Screen};
|
||||
|
||||
/// Render the bottom status bar with key hints and mode indicator.
|
||||
///
|
||||
/// Layout: `[mode] ─── [key hints]`
|
||||
///
|
||||
/// Key hints are sourced from the [`CommandRegistry`] filtered to the
|
||||
/// current screen, showing only the most important bindings.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_status_bar(
|
||||
frame: &mut Frame<'_>,
|
||||
area: Rect,
|
||||
registry: &CommandRegistry,
|
||||
screen: &Screen,
|
||||
mode: &InputMode,
|
||||
bar_bg: PackedRgba,
|
||||
text_color: PackedRgba,
|
||||
accent_color: PackedRgba,
|
||||
) {
|
||||
if area.height == 0 || area.width < 5 {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fill the bar background.
|
||||
let bg_cell = Cell {
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
frame.draw_rect_filled(area, bg_cell);
|
||||
|
||||
let mode_label = match mode {
|
||||
InputMode::Normal => "NORMAL",
|
||||
InputMode::Text => "INPUT",
|
||||
InputMode::Palette => "PALETTE",
|
||||
InputMode::GoPrefix { .. } => "g...",
|
||||
};
|
||||
|
||||
// Left side: mode indicator.
|
||||
let mode_cell = Cell {
|
||||
fg: accent_color,
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
let mut x = frame.print_text_clipped(
|
||||
area.x.saturating_add(1),
|
||||
area.y,
|
||||
mode_label,
|
||||
mode_cell,
|
||||
area.right(),
|
||||
);
|
||||
|
||||
// Spacer.
|
||||
x = x.saturating_add(2);
|
||||
|
||||
// Right side: key hints from registry (formatted as "key:action").
|
||||
let hints = registry.status_hints(screen);
|
||||
let hint_cell = Cell {
|
||||
fg: text_color,
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
let key_cell = Cell {
|
||||
fg: accent_color,
|
||||
bg: bar_bg,
|
||||
..Cell::default()
|
||||
};
|
||||
|
||||
for hint in &hints {
|
||||
if x >= area.right().saturating_sub(1) {
|
||||
break;
|
||||
}
|
||||
// Split "q:quit" into key part and description part.
|
||||
if let Some((key_part, desc_part)) = hint.split_once(':') {
|
||||
x = frame.print_text_clipped(x, area.y, key_part, key_cell, area.right());
|
||||
x = frame.print_text_clipped(x, area.y, ":", hint_cell, area.right());
|
||||
x = frame.print_text_clipped(x, area.y, desc_part, hint_cell, area.right());
|
||||
} else {
|
||||
x = frame.print_text_clipped(x, area.y, hint, hint_cell, area.right());
|
||||
}
|
||||
x = x.saturating_add(2);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::commands::build_registry;
|
||||
use crate::message::Screen;
|
||||
use ftui::render::grapheme_pool::GraphemePool;
|
||||
|
||||
macro_rules! with_frame {
|
||||
($width:expr, $height:expr, |$frame:ident| $body:block) => {{
|
||||
let mut pool = GraphemePool::new();
|
||||
let mut $frame = Frame::new($width, $height, &mut pool);
|
||||
$body
|
||||
}};
|
||||
}
|
||||
|
||||
fn white() -> PackedRgba {
|
||||
PackedRgba::rgb(0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
fn gray() -> PackedRgba {
|
||||
PackedRgba::rgb(0x80, 0x80, 0x80)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status_bar_renders_mode() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let registry = build_registry();
|
||||
render_status_bar(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 1),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
&InputMode::Normal,
|
||||
gray(),
|
||||
white(),
|
||||
white(),
|
||||
);
|
||||
|
||||
let n_cell = frame.buffer.get(1, 0).unwrap();
|
||||
assert_eq!(n_cell.content.as_char(), Some('N'));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status_bar_text_mode() {
|
||||
with_frame!(80, 1, |frame| {
|
||||
let registry = build_registry();
|
||||
render_status_bar(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 80, 1),
|
||||
®istry,
|
||||
&Screen::Search,
|
||||
&InputMode::Text,
|
||||
gray(),
|
||||
white(),
|
||||
white(),
|
||||
);
|
||||
|
||||
let i_cell = frame.buffer.get(1, 0).unwrap();
|
||||
assert_eq!(i_cell.content.as_char(), Some('I'));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_status_bar_narrow_terminal() {
|
||||
with_frame!(4, 1, |frame| {
|
||||
let registry = build_registry();
|
||||
render_status_bar(
|
||||
&mut frame,
|
||||
Rect::new(0, 0, 4, 1),
|
||||
®istry,
|
||||
&Screen::Dashboard,
|
||||
&InputMode::Normal,
|
||||
gray(),
|
||||
white(),
|
||||
white(),
|
||||
);
|
||||
let cell = frame.buffer.get(0, 0).unwrap();
|
||||
assert!(cell.is_empty());
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user