258 lines
9.9 KiB
Rust
258 lines
9.9 KiB
Rust
use ratatui::{Terminal, backend::TestBackend, style::Modifier};
|
|
use rcom::app::{AppState, Clipboard, Command, Overlay, PaneId, PaneMode, PaneSnapshot};
|
|
use rcom::domain::{Entry, EntryId, EntryKind, Location};
|
|
use rcom::fs::FsError;
|
|
use rcom::operations::OperationKind;
|
|
use rcom::preview::{Preview, PreviewKind};
|
|
use rcom::ssh::HostPickerState;
|
|
use rcom::ui::{Theme, ThemeId, render};
|
|
use unicode_width::UnicodeWidthStr;
|
|
|
|
fn rich_app() -> AppState {
|
|
let location = Location::local("/fixture");
|
|
let mut app = AppState::new(location.clone());
|
|
app.panes[0].entries = vec![
|
|
Entry::new("docs", EntryKind::Directory),
|
|
Entry::new("README.md", EntryKind::File),
|
|
Entry::new("archive.tar.gz", EntryKind::File),
|
|
Entry::new("image.png", EntryKind::File),
|
|
Entry::new("main.rs", EntryKind::File),
|
|
Entry::new("movie.mp4", EntryKind::File),
|
|
Entry::new("run.sh", EntryKind::File).with_metadata(Some(1), None, Some(0o755)),
|
|
Entry::new("socket", EntryKind::Other),
|
|
Entry::new("link", EntryKind::Symlink),
|
|
];
|
|
let len = app.panes[0].entries.len();
|
|
app.panes[0].selection.set(Some(0), len);
|
|
let marked = EntryId {
|
|
source: location.source.clone(),
|
|
path: location.join("docs").path,
|
|
};
|
|
app.panes[0].selection_set.insert(marked.clone());
|
|
app.clipboard = Some(Clipboard {
|
|
kind: OperationKind::Move,
|
|
source_pane: PaneId::Left,
|
|
source: PaneSnapshot {
|
|
pane: PaneId::Left,
|
|
location,
|
|
listing_generation: 0,
|
|
session_epoch: None,
|
|
},
|
|
sources: vec![marked],
|
|
});
|
|
app.panes[0].preview.content = Some(Preview {
|
|
kind: PreviewKind::Text,
|
|
lines: vec!["1 │ preview text".into(), "2 │ selected state".into()],
|
|
truncated: true,
|
|
directory_entries: Vec::new(),
|
|
});
|
|
app
|
|
}
|
|
|
|
fn render_frame(app: &AppState, width: u16, height: u16) -> ratatui::buffer::Buffer {
|
|
let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
|
|
terminal
|
|
.draw(|frame| {
|
|
render(frame, app, Some(&HostPickerState::default()), &app.theme);
|
|
})
|
|
.unwrap();
|
|
terminal.backend().buffer().clone()
|
|
}
|
|
|
|
fn find_text_from(
|
|
buffer: &ratatui::buffer::Buffer,
|
|
text: &str,
|
|
minimum_x: u16,
|
|
) -> Option<(u16, u16)> {
|
|
(0..buffer.area.height).find_map(|y| {
|
|
let row = (0..buffer.area.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>();
|
|
let prefix = row
|
|
.char_indices()
|
|
.nth(usize::from(minimum_x))
|
|
.map(|(byte, _)| byte)
|
|
.unwrap_or(row.len());
|
|
row[prefix..]
|
|
.find(text)
|
|
.map(|byte| minimum_x + row[prefix..][..byte].chars().count() as u16)
|
|
.map(|x| (x, y))
|
|
})
|
|
}
|
|
|
|
fn find_text(buffer: &ratatui::buffer::Buffer, text: &str) -> Option<(u16, u16)> {
|
|
find_text_from(buffer, text, 0)
|
|
}
|
|
|
|
fn find_text_on_row(buffer: &ratatui::buffer::Buffer, text: &str, y: u16) -> Option<(u16, u16)> {
|
|
let row = (0..buffer.area.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>();
|
|
row.find(text)
|
|
.map(|byte| (row[..byte].chars().count() as u16, y))
|
|
}
|
|
|
|
#[test]
|
|
fn every_theme_renders_file_states_and_all_overlay_families_without_layout_changes() {
|
|
for id in ThemeId::ALL {
|
|
let mut app = rich_app();
|
|
app.theme = Theme::with_capabilities_for(id, true, false);
|
|
let normal = render_frame(&app, 160, 40);
|
|
assert!(
|
|
normal
|
|
.content()
|
|
.iter()
|
|
.any(|cell| cell.bg == app.theme.palette.selection
|
|
&& cell.fg == app.theme.palette.selection_text),
|
|
"{id}: selected effective colors are not visible"
|
|
);
|
|
let selected_y = (2..normal.area.height)
|
|
.find(|y| (32..96).any(|x| normal.cell((x, *y)).unwrap().symbol() == "▸"))
|
|
.unwrap();
|
|
let selected = find_text_on_row(&normal, "docs/", selected_y).unwrap();
|
|
for x in selected.0.saturating_sub(5)..selected.0 + 5 {
|
|
if x > 0
|
|
&& UnicodeWidthStr::width(normal.cell((x - 1, selected.1)).unwrap().symbol()) > 1
|
|
{
|
|
continue; // Ratatui deliberately resets the continuation cell of a wide glyph.
|
|
}
|
|
let cell = normal.cell((x, selected.1)).unwrap();
|
|
assert_eq!(cell.bg, app.theme.palette.selection, "{id} selected x={x}");
|
|
if cell.symbol() != " " {
|
|
assert_eq!(
|
|
cell.fg, app.theme.palette.selection_text,
|
|
"{id} selected x={x}"
|
|
);
|
|
assert!(cell.modifier.contains(Modifier::BOLD));
|
|
assert!(!cell.modifier.contains(Modifier::REVERSED));
|
|
}
|
|
}
|
|
let document = find_text(&normal, "README.md").unwrap();
|
|
let document_cell = normal.cell(document).unwrap();
|
|
assert_eq!(
|
|
document_cell.fg, app.theme.palette.file_types.document,
|
|
"{id}"
|
|
);
|
|
assert_eq!(document_cell.bg, app.theme.palette.panel, "{id}");
|
|
let rust = find_text(&normal, "main.rs").unwrap();
|
|
assert_eq!(
|
|
normal.cell(rust).unwrap().fg,
|
|
app.theme.palette.file_types.regular
|
|
);
|
|
let rust_icon = (0..rust.0)
|
|
.rev()
|
|
.map(|x| normal.cell((x, rust.1)).unwrap())
|
|
.find(|cell| cell.symbol() != " ")
|
|
.unwrap();
|
|
assert_eq!(rust_icon.fg, app.theme.palette.file_types.rust_icon, "{id}");
|
|
let active = normal.cell(find_text(&normal, "ACTIVE").unwrap()).unwrap();
|
|
assert_eq!(active.fg, app.theme.palette.active, "{id}");
|
|
assert_eq!(
|
|
normal.cell((0, 1)).unwrap().fg,
|
|
app.theme.palette.muted,
|
|
"{id}"
|
|
);
|
|
for overlay in [
|
|
Overlay::Help,
|
|
Overlay::Preview,
|
|
Overlay::ThemePicker,
|
|
Overlay::Message,
|
|
Overlay::DestinationPrompt,
|
|
Overlay::ConfirmOperation,
|
|
Overlay::Collision,
|
|
Overlay::EntryPrompt,
|
|
Overlay::ConfirmEntryAction,
|
|
Overlay::Undo,
|
|
Overlay::Edit,
|
|
Overlay::Tasks,
|
|
Overlay::TaskDetails,
|
|
Overlay::Quit,
|
|
] {
|
|
app.overlay = overlay;
|
|
if overlay == Overlay::Message {
|
|
app.panes[0].error = Some(FsError::PermissionDenied);
|
|
}
|
|
if overlay == Overlay::ThemePicker {
|
|
app.overlay = Overlay::None;
|
|
app.reduce(Command::OpenThemePicker);
|
|
}
|
|
let buffer = render_frame(&app, 160, 40);
|
|
assert_eq!(buffer.area.width, 160);
|
|
assert_eq!(buffer.area.height, 40);
|
|
if overlay == Overlay::Message {
|
|
let heading = buffer.cell(find_text(&buffer, "MESSAGE").unwrap()).unwrap();
|
|
assert_eq!(heading.fg, app.theme.palette.error, "{id}");
|
|
assert!(heading.modifier.contains(Modifier::BOLD));
|
|
}
|
|
if overlay == Overlay::ThemePicker {
|
|
let position = format!(
|
|
"{}/20",
|
|
app.theme_picker.as_ref().unwrap().selected_index() + 1
|
|
);
|
|
let selected = buffer.cell(find_text(&buffer, &position).unwrap()).unwrap();
|
|
assert_eq!(selected.bg, app.theme.palette.selection);
|
|
assert_eq!(selected.fg, app.theme.palette.selection_text);
|
|
}
|
|
}
|
|
app.overlay = Overlay::None;
|
|
app.panes[0].error = None;
|
|
app.theme = Theme::with_capabilities_for(id, false, true);
|
|
let fallback = render_frame(&app, 40, 8);
|
|
assert!(fallback.content().iter().any(|cell| cell.symbol() == ">"));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_theme_preserves_inactive_file_colors_and_visible_warning_styles() {
|
|
for id in ThemeId::ALL {
|
|
let mut app = rich_app();
|
|
app.theme = Theme::with_capabilities_for(id, true, false);
|
|
app.mode = PaneMode::Dual;
|
|
app.panes[1] = app.panes[0].clone();
|
|
let dual = render_frame(&app, 160, 40);
|
|
let inactive = dual
|
|
.cell(find_text_from(&dual, "README.md", 80).unwrap())
|
|
.unwrap();
|
|
assert_eq!(inactive.fg, app.theme.palette.file_types.document, "{id}");
|
|
assert_eq!(inactive.bg, app.theme.palette.panel, "{id}");
|
|
|
|
let picker = HostPickerState::from_strings(
|
|
vec!["host".to_owned()],
|
|
vec!["Include warning".to_owned()],
|
|
);
|
|
let mut terminal = Terminal::new(TestBackend::new(120, 40)).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, &app, Some(&picker), &app.theme))
|
|
.unwrap();
|
|
app.overlay = Overlay::HostPicker;
|
|
terminal
|
|
.draw(|frame| render(frame, &app, Some(&picker), &app.theme))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
let warning = buffer.cell(find_text(buffer, "WARNING").unwrap()).unwrap();
|
|
assert_eq!(warning.fg, app.theme.palette.secondary, "{id}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn copied_and_cut_combined_selection_keep_textual_markers_in_every_theme() {
|
|
for (kind, marker) in [(OperationKind::Copy, "±"), (OperationKind::Move, "∓")] {
|
|
for id in ThemeId::ALL {
|
|
let mut app = rich_app();
|
|
app.theme = Theme::with_capabilities_for(id, true, false);
|
|
if let Some(clipboard) = &mut app.clipboard {
|
|
clipboard.kind = kind;
|
|
}
|
|
let buffer = render_frame(&app, 160, 40);
|
|
assert!(
|
|
buffer.content().iter().any(|cell| cell.symbol() == marker),
|
|
"{id}: {:?}",
|
|
buffer
|
|
.content()
|
|
.iter()
|
|
.map(|cell| cell.symbol())
|
|
.collect::<String>()
|
|
);
|
|
}
|
|
}
|
|
}
|