Lines
98.45 %
Functions
100 %
Branches
//! The Window menu lists the open windows, and M-n selects one in cua.
use super::*;
use crate::app::MenuId;
use crate::tabs::list::ListTab;
use crate::tabs::reports::{ReportKind, ReportsTab};
use crate::test_support::{alt, key, press};
use forms::FormValues;
use ratatui::crossterm::event::KeyCode;
/// The commands of the Window menu before the list.
const COMMANDS: usize = 8;
fn open_window_menu(app: &mut App) {
app.open_menu_of(MenuId::Window);
}
fn listed(app: &App) -> Vec<String> {
let entries = app.menu_entries();
entries[COMMANDS..]
.iter()
.map(|entry| entry.label.clone())
.collect()
fn choose(app: &mut App, label: &str) {
let at = app
.menu_entries()
.position(|entry| entry.label == label);
app.select_menu_item(at.unwrap_or(usize::MAX));
press(app, key(KeyCode::Enter));
#[test]
fn the_list_has_every_window_by_number_and_the_unnumbered_last() {
let mut app = crate::test_support::app();
open_window_menu(&mut app);
let expected = [
"1 Accounts",
"2 Transactions",
"3 Commodities",
"4 Balance",
"5 Config",
"6 SSH keys",
"7 Tags",
"8 Templates",
"9 Console",
"Scripts",
];
assert_eq!(listed(&app), expected);
app.raise_window(app.window_of(ViewId::Tags));
fn a_later_window_of_a_view_keeps_its_ordinal_when_others_close() {
app.close_window(app.window_of(ViewId::Console));
let nine = app.open_extra(ViewId::Tags);
let none = app.open_extra(ViewId::Tags);
let labels = listed(&app);
assert_eq!(
labels[6..],
["7 Tags", "8 Templates", "9 Tags (2)", "Scripts", "Tags (3)"]
);
app.close_window(app.window_of_number(7));
["8 Templates", "9 Tags (2)", "Scripts", "Tags (3)"]
assert_eq!(app.window_suffix(nine), " (2)");
assert_eq!(app.window_suffix(none), " (3)");
let fresh = app.open_extra(ViewId::Tags);
assert_eq!(app.window_suffix(fresh), " (4)");
fn the_commands_keep_their_mnemonics_and_the_list_has_none() {
assert!(
entries[..COMMANDS]
.all(|entry| tvui::hotkey(&entry.label).is_some())
.all(|entry| tvui::hotkey(&entry.label).is_none())
#[tokio::test]
async fn choosing_an_entry_raises_and_loads_that_window() {
app.attach_echo();
choose(&mut app, "2 Transactions");
let window = app.window_of(ViewId::Transactions);
assert_eq!(app.front_window(), Some(window));
assert!(app.menu.is_none());
assert!(app.win_by(window).is_some_and(|win| win.view.loading()));
fn the_list_entries_show_no_chord_column_in_any_preset() {
for mut app in [crate::test_support::app(), crate::test_support::emacs_app()] {
assert!(entries[COMMANDS..].iter().all(|entry| entry.key.is_empty()));
fn the_selection_stays_on_the_last_entry_when_the_list_shrinks() {
app.menu_last();
let last = app.menu_entries().len() - 1;
assert_eq!(app.menu_item(), Some(last));
assert_eq!(app.menu_item(), Some(last - 1));
app.menu_next();
assert_eq!(app.menu_item(), Some(0));
app.menu_prev();
fn the_selection_keeps_its_place_when_the_list_grows() {
let before = app.menu_item();
app.open_extra(ViewId::Tags);
assert_eq!(app.menu_item(), before);
assert_eq!(app.menu_item(), before.map(|at| at + 1));
fn alt_digit_selects_that_window_in_cua() {
press(&mut app, alt(KeyCode::Char('4')));
assert_eq!(app.front_kind(), Some(ViewId::Reports));
press(&mut app, alt(KeyCode::Char('2')));
assert_eq!(app.front_kind(), Some(ViewId::Transactions));
fn alt_digit_without_that_window_says_so() {
let front = app.front_window();
press(&mut app, alt(KeyCode::Char('9')));
assert_eq!(app.front_window(), front);
assert_eq!(app.status, "no window 9");
fn emacs_and_vim_leave_alt_digit_unbound() {
for mut app in [
crate::test_support::emacs_app(),
crate::test_support::vim_app(),
] {
press(&mut app, alt(KeyCode::Char('3')));
fn frame_top(app: &App) -> String {
let (buf, _) = crate::test_support::draw_frame(app, 120, 24);
(0..buf.area.width).map(|x| buf[(x, 1)].symbol()).collect()
fn entry_title(app: &mut App, id: WindowId) -> String {
open_window_menu(app);
let label = app
.into_iter()
.find(|entry| entry.intent == crate::event::Intent::RaiseWindow(id))
.map(|entry| entry.label);
let label = label.unwrap_or_default();
label
.trim_start_matches(|c: char| c.is_ascii_digit() || c == ' ')
.to_string()
fn the_list_entry_is_the_frame_title_of_its_window() {
for kind in ViewId::ALL {
let id = app.open_extra(kind);
let title = entry_title(&mut app, id);
app.close_menu();
assert!(title.ends_with(" (2)"), "{title}");
frame_top(&app).contains(&title),
"{title}: {}",
frame_top(&app)
fn a_window_titled_by_its_filter_needs_no_ordinal() {
let first = app.window_of(ViewId::Transactions);
let second = app.open_extra(ViewId::Transactions);
assert_eq!(app.window_suffix(second), " (2)");
if let Some(list) = app.win_mut::<ListTab>(second) {
list.set_filter(&FormValues::default(), "acc-1".to_string());
assert_eq!(app.window_suffix(second), "");
assert_eq!(app.window_suffix(first), "");
fn a_report_window_is_titled_by_its_report() {
let first = app.window_of(ViewId::Reports);
let second = app.open_extra(ViewId::Reports);
if let Some(tab) = app.win_mut::<ReportsTab>(second) {
tab.set_loading(1, ReportKind::Activity, &FormValues::default());
let title = |app: &App, id| {
app.win_by(id)
.map(|win| win.view.title(&app.window_suffix(id)))
.unwrap_or_default()
};
assert_eq!(title(&app, first), "Balance");
assert_eq!(title(&app, second), "Activity");
fn the_frame_title_marks_the_tail_the_list_title_does_not() {
let suffix = app.window_suffix(second);
let Some(view) = app.active_view() else {
panic!("a front window");
assert_eq!(view.title(&suffix), "Transactions (2)");
view.framed_title(&suffix),
tvui::joined_title("Transactions", " (2)")
assert_ne!(view.framed_title(&suffix), view.title(&suffix));