Lines
99.33 %
Functions
100 %
Branches
//! The status-bar hints of the focused form field, which the form footers no longer carry.
use crate::app::App;
use crate::form::Form;
use crate::modal::Modal;
use crate::tabs::reports::ReportKind;
use crate::test_support::hint_pairs;
use crate::widgets::Widget;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
const PRESETS: [&str; 3] = ["cua", "emacs", "vim"];
fn shown_labels(preset: &str, mut form: Form, field: impl Fn(&Widget) -> bool) -> Vec<String> {
form.focus = form
.fields
.iter()
.position(|f| field(&f.widget))
.expect("the field is in the form");
let mut app: App = crate::test_support::app_with_preset(preset);
app.overlays.push(Modal::Form(form));
hint_pairs(&app)
.into_iter()
.map(|(_, label)| label)
.collect()
}
fn assert_shows(preset: &str, labels: &[String], wanted: &[&str]) {
for label in wanted {
assert!(
labels.iter().any(|l| l == label),
"{preset}: {label} in {labels:?}"
);
#[test]
fn a_split_cell_hints_its_row_and_tag_keys() {
for preset in PRESETS {
let labels = shown_labels(preset, Form::transaction_create(), |w| {
matches!(w, Widget::Splits(_))
});
assert_shows(
preset,
&labels,
&["Add row", "Remove row", "Tags", "Suggest"],
fn a_rows_cell_hints_its_row_keys_and_suggestion() {
let none = forms::FormValues::default();
let labels = shown_labels(preset, Form::account_edit(&none), |w| {
matches!(w, Widget::Rows(_))
assert_shows(preset, &labels, &["Add row", "Remove row", "Suggest"]);
fn a_text_area_hints_its_submit_and_newline_keys() {
let labels = shown_labels(
Form::transaction_create(),
|w| matches!(w, Widget::Text(ed) if ed.is_multiline()),
assert_shows(preset, &labels, &["Newline"]);
fn a_date_field_hints_the_day_steps() {
matches!(w, Widget::Date(_))
assert_shows(preset, &labels, &["Day back", "Day forward"]);
fn the_hints_stay_off_a_plain_field() {
let labels = shown_labels("cua", Form::config_set(), |w| matches!(w, Widget::Text(_)));
for label in ["Newline", "Add row", "Tags", "Suggest"] {
assert!(!labels.iter().any(|l| l == label), "{label} in {labels:?}");
fn status_row(preset: &str, mut form: Form, focus: usize) -> String {
form.focus = focus;
let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("a test terminal");
terminal
.draw(|frame| crate::draw::draw(frame, &app, &mut crate::hits::Hits::default()))
.expect("the frame draws");
let buffer = terminal.backend().buffer();
(0..80).map(|x| buffer[(x, 23)].symbol()).collect()
fn field_of(form: &Form, key: &str) -> usize {
form.fields
.position(|f| f.key == Some(key))
.expect("the field is in the form")
fn a_filter_field_shows_the_builder_key_and_other_fields_do_not() {
let form = || Form::report(ReportKind::Balance, &forms::FormValues::default());
let on = field_of(&form(), "filter");
let row = status_row("cua", form(), on);
assert!(row.contains("C-b Builder"), "{row}");
let row = status_row("cua", form(), 0);
assert!(!row.contains("Builder"), "{row}");
fn the_template_form_shows_test_not_tags_on_each_field() {
for key in ["name", "source"] {
let form = Form::template_create();
let focus = field_of(&form, key);
let row = status_row("cua", form, focus);
assert!(row.contains("C-t Test"), "{key}: {row}");
assert!(!row.contains("Tags"), "{key}: {row}");
fn a_split_cell_shows_the_tags_key_at_80_columns() {
let form = Form::transaction_create();
let focus = form
.position(|f| matches!(f.widget, Widget::Splits(_)))
.expect("the splits field");
let row = status_row(preset, form, focus);
assert!(row.contains("Tags"), "{preset}: {row}");
fn splits_app(overrides: &str) -> (App, usize) {
let mut app: App = crate::test_support::app();
app.keys = app.keys.overridden(overrides).expect("the override parses");
let mut form = Form::transaction_create();
let focus = form.focus;
(app, focus)
fn hint_keys(app: &App) -> Vec<String> {
hint_pairs(app)
.map(|(key, label)| format!("{key} {label}"))
fn tags_open(app: &App, focus: usize) -> bool {
let Some(Modal::Form(form)) = app.overlays.top() else {
panic!("the form is open");
};
matches!(&form.fields[focus].widget, Widget::Splits(sw) if sw.tags_open())
fn the_tags_hint_comes_before_the_row_keys_on_a_split_cell() {
let (app, _) = splits_app("");
let shown = hint_keys(&app);
let at = |hint: &str| {
shown
.position(|s| s == hint)
.expect("the hint shows")
assert!(at("C-t Tags") < at("+ Add row"), "{shown:?}");
fn rebinding_the_overlay_chord_moves_the_split_hint_and_the_key() {
let (mut app, focus) = splits_app("overlay: C-t = none; overlay: C-g = toggle-tags");
assert!(shown.contains(&"C-g Tags".to_string()), "{shown:?}");
assert!(!shown.iter().any(|s| s.starts_with("C-t")), "{shown:?}");
crate::test_support::press(
&mut app,
crate::test_support::ctrl(ratatui::crossterm::event::KeyCode::Char('t')),
assert!(!tags_open(&app, focus));
crate::test_support::ctrl(ratatui::crossterm::event::KeyCode::Char('g')),
assert!(tags_open(&app, focus));