Lines
92.91 %
Functions
45.83 %
Branches
100 %
use crate::app::App;
use crate::event::Intent;
use crate::form::{Field, Form, FormKind};
use crate::keymap;
use crate::widgets::rows::empty_note;
use crate::widgets::{Editor, SelectWidget, Widget};
use ratatui::text::Line;
use std::cell::Cell;
use tvui::Viewport;
use footer::footer_lines;
mod body;
pub(super) use body::FormBody;
mod footer;
mod grid;
mod text_area;
pub(super) use body::draw_form;
use body::{Block, Input, Popup};
use text_area::TextArea;
pub(super) fn render_field<'a>(
i: usize,
f: &'a Field,
focus: usize,
width: usize,
suggestions: Option<&str>,
add_key: Option<&str>,
) -> Block<'a> {
let focused = i == focus;
let label = format!(" {:<width$}", format!("{}:", f.label));
let column = u16::try_from(width + 3).unwrap_or(u16::MAX);
let input =
|text: String, cursor: usize, scroll: Option<&'a Cell<Viewport>>, open: Option<bool>| {
Block::Input(Input {
label: label.clone(),
column,
text,
cursor,
scroll,
focused,
combo: open.is_some(),
caret: open.unwrap_or(true),
})
};
let plain = |ed: &'a Editor| {
input(
ed.buffer().to_string(),
ed.cursor(),
Some(ed.scroll()),
None,
)
match &f.widget {
Widget::Text(ed) if ed.is_multiline() => {
Block::Text(TextArea::new(label, column, ed, focused))
}
Widget::Text(ed) => input(
ed.buffer().replace('\n', "\u{23ce}"),
),
Widget::Amount(aw) => plain(aw.editor()),
Widget::Date(dw) => plain(dw.editor()),
Widget::Select(sw) => input(
sw.shown().to_string(),
sw.query().chars().count(),
Some(sw.scroll()),
Some(sw.open),
Widget::Splits(sw) => Block::Grid(
sw.grid(focused, suggestions, add_key)
.titled(format!(" {}:", f.label), focused),
Widget::Rows(rw) => {
let note = empty_note(&f.label.to_lowercase(), add_key);
Block::Grid(
rw.grid(focused, suggestions, Some(¬e))
fn popup_of(sw: &SelectWidget) -> Popup<'_> {
let options = sw.options();
Popup {
labels: sw
.matches()
.into_iter()
.map(|index| options[index].label.clone())
.collect(),
selected: sw.focused_in_filtered(),
window: sw.window(),
pub(super) fn title(kind: FormKind) -> &'static str {
match kind {
FormKind::ConfigSet => "Set config",
FormKind::Options => "Options",
FormKind::SshKeyAdd => "Add SSH key",
FormKind::CsvSave => "Save CSV",
FormKind::Report(kind) => kind.descriptor().title,
FormKind::CommodityCreate => "Create Commodity",
FormKind::AccountCreate => "Create Account",
FormKind::TransactionCreate => "Create Transaction",
FormKind::TransactionEdit => "Edit Transaction",
FormKind::AccountEdit => "Edit Account",
FormKind::CommodityEdit => "Edit Commodity",
FormKind::TagCreate => "Create Tag",
FormKind::TagEdit => "Edit Tag",
FormKind::TemplateCreate => "Create Template",
FormKind::TemplateEdit => "Edit Template",
FormKind::TemplatePick => "New from Template",
FormKind::ScriptCreate => "Create Script",
FormKind::ScriptEdit => "Edit Script",
FormKind::AccountMerge => "Merge Accounts",
FormKind::TransactionFilter => "Filter Transactions",
FormKind::FilterAdd => "Add Filter Row",
FormKind::FilterRow(shape) => shape.title(),
FormKind::CommodityConvert => "Convert Amount",
pub(super) fn form_body<'a>(
form: &'a Form,
(pending, active): (bool, bool),
app: &App,
) -> body::FormBody<'a> {
let keys = keymap::in_form(app, form);
let popup = form.focused_select().filter(|sw| sw.open && active);
let focus = if active { form.focus } else { usize::MAX };
let footer = match pending {
true => vec!["Submitting… (input locked until the server replies)".to_string()],
false => footer_lines(form.kind),
let width = form.fields.iter().map(|f| f.label.len()).max().unwrap_or(0) + 1;
let suggestions = form.suggestion_line(keys.key(Intent::CompleteSuggestion));
let add_key = keymap::in_rows(app).key(Intent::SplitAddRow);
let blocks = form.fields.iter().enumerate().map(|(i, f)| {
let shown = suggestions.as_deref().filter(|_| i == focus);
render_field(i, f, focus, width, shown, add_key.as_deref())
});
let mut tail = super::messages::message_lines("! ", &form.errors);
if !footer.is_empty() {
tail.push(Line::from(""));
tail.extend(footer.into_iter().map(Line::from));
body::FormBody {
title: title(form.kind),
notices: super::messages::message_lines("note: ", &form.notices),
blocks: blocks.collect(),
tail,
focus: form.focus,
button: form.focused_button().filter(|_| active),
pressed: app.pressed_button().filter(|_| !pending && active),
dragging: app.dragging_layer() && !pending && active,
active,
popup: popup.map(popup_of),
#[cfg(test)]
mod body_tests;
mod empty_note_tests;
mod field_look_tests;
mod grid_tests;
mod history_tests;
mod select_tests;
mod split_columns_tests;
pub(super) mod tests;
mod text_area_tests;