Lines
90 %
Functions
78.57 %
Branches
100 %
//! Delivery of a routed reply to the window or dialog it belongs to.
use crate::app::App;
use crate::route::{Route, RouteCtx};
use crate::tabs::config::ConfigTab;
use crate::tabs::fetch::Fetch;
use crate::tabs::list::ListTab;
use crate::tabs::nms::{ConsoleState, console_lines};
use crate::tabs::reports::ReportsTab;
use crate::view::ViewId;
use crate::window::WindowId;
impl App {
/// Dispatch a completed reply. A single match handles every `RouteCtx` in
/// one place — `route.ctx` is read exactly once, never re-inspected inside a
/// nested target match.
pub(super) fn deliver_reply(&mut self, route: Route, wire: &str) {
let refresh = refresh_after(&route.ctx, wire);
match route {
Route {
target: Some(window),
ctx: RouteCtx::AccountBalances { seq },
} => self.deliver_account_balances(window, seq, wire),
ctx: RouteCtx::Mutation { .. },
..
} => self.deliver_mutation_reply(wire),
target,
ctx: RouteCtx::DeleteTransaction { id },
} => self.deliver_delete_reply(target, &id, wire),
ctx: RouteCtx::BulkDelete { seq, id },
} => self.deliver_bulk_delete_reply(seq, &id, wire),
ctx: RouteCtx::FormOptions { seq, source },
} => self.deliver_form_options_reply(wire, seq, source),
ctx: RouteCtx::Suggestion { seq, hint },
} => self.deliver_tag_hints(seq, &hint, wire),
ctx: RouteCtx::MergePreview,
} => {}
ctx: RouteCtx::TransactionEdit,
} => self.deliver_transaction_edit_reply(target, wire),
ctx: RouteCtx::TransactionDetail { seq, id },
} => self.deliver_transaction_detail(seq, id, wire),
ctx: RouteCtx::AccountEdit,
} => self.deliver_account_edit_reply(target, wire),
ctx: RouteCtx::CommodityEdit,
} => self.deliver_commodity_edit_reply(target, wire),
ctx:
RouteCtx::TransactionFilter {
seq,
filter,
summary,
},
} => self.deliver_filter_reply(window, seq, &filter, summary, wire),
ctx: RouteCtx::Lookup { lookup, source },
} => self.deliver_lookup(lookup, source, wire),
ctx: RouteCtx::ConfigSet { key },
} => {
if let Some(config) = self.win_mut::<ConfigTab>(window) {
config.follow = Some(key);
}
self.read_ui_config();
ctx: RouteCtx::SshKeys { seq },
} => self.deliver_ssh_keys(window, seq, wire),
ctx: RouteCtx::Tags { seq },
} => self.deliver_tags(window, seq, wire),
ctx: RouteCtx::TagCreate,
} => self.deliver_tag_created(window, wire),
ctx: RouteCtx::TagEdit,
} => self.deliver_tag_edit_reply(target, wire),
ctx: RouteCtx::TagDelete { label },
} => self.deliver_tag_deleted(&label, wire),
ctx: RouteCtx::Template(route),
} => self.deliver_template(target, route, wire),
ctx: RouteCtx::Script(route),
} => self.deliver_script(target, route, wire),
ctx: RouteCtx::SshKeyAdd,
} => self.deliver_ssh_key_added(window, wire),
ctx: RouteCtx::SshKeyRemove { fingerprint },
} => self.deliver_ssh_key_removed(&fingerprint, wire),
RouteCtx::ConvertQuery {
amount_str,
from_label,
to_label,
} => self.deliver_convert_reply(wire, &amount_str, &from_label, &to_label),
ctx: RouteCtx::Reports { kind, seq, values },
if let Some(reports) = self.win_mut::<ReportsTab>(window)
&& reports.run_seq.awaits(seq)
{
reports.on_reply(wire, kind, values);
self.refetch_report_chart(window);
ctx: RouteCtx::ReportChart { seq, cells },
let env = self.terminal.env();
if let Some(reports) = self.win_mut::<ReportsTab>(window) {
reports.chart.accept(seq, cells, env, window, wire);
ctx: RouteCtx::ReportCsv { seq, file_name },
} => self.deliver_report_csv(window, seq, file_name, wire),
ctx: RouteCtx::ChartOverride { seq },
} => self.deliver_chart_override(window, seq, wire),
ctx: RouteCtx::UiConfig { seq },
} => self.deliver_ui_config(seq, wire),
ctx: RouteCtx::OptionsSave { changes },
} => self.deliver_options_saved(&changes, wire),
ctx: RouteCtx::Config { seq },
if let Some(config) = self.win_mut::<ConfigTab>(window)
&& config.seq.awaits(seq)
config.on_reply(wire);
ctx: RouteCtx::None,
} => self.deliver_to_window(window, wire),
Route { target: None, .. } => {}
if let Some(kind) = refresh {
self.refresh_windows(kind);
/// A reply with no special context goes to the window that asked, by the view it shows.
fn deliver_to_window(&mut self, window: WindowId, wire: &str) {
match self.window_view(window) {
Some(ViewId::Console) => {
if let Some(console) = self.win_mut::<ConsoleState>(window) {
for line in console_lines(wire) {
console.push_scrollback(line);
Some(ViewId::Accounts) => {
let loaded = self.win_mut::<ListTab>(window).is_some_and(|list| {
list.on_reply(wire);
matches!(list.state, Fetch::Loaded(_))
});
if loaded {
self.fetch_account_balances(window);
Some(ViewId::Transactions) => self.deliver_transactions_reply(window, wire),
Some(ViewId::Commodities) => {
if let Some(list) = self.win_mut::<ListTab>(window) {
Some(
ViewId::Reports
| ViewId::Config
| ViewId::SshKeys
| ViewId::Tags
| ViewId::Templates
| ViewId::Scripts
| ViewId::Viewer,
)
| None => {}
fn deliver_delete_reply(&mut self, target: Option<WindowId>, id: &str, wire: &str) {
if cli_core::render::parse_wire(wire).is_ok() {
self.forget_transaction(target, id);
self.deliver_mutation_reply(wire);
/// A successful mutation shows its notices.
fn deliver_mutation_reply(&mut self, wire: &str) {
match cli_core::render::parse_wire(wire) {
Ok(_) => {
let notices = rpc::parse_notices(wire);
if !notices.is_empty() {
self.status = notices.join("; ");
Err(e) => self.status = format!("mutation failed: {e}"),
/// The view whose windows go stale once `ctx`'s mutation has succeeded.
pub(super) fn refresh_after(ctx: &RouteCtx, wire: &str) -> Option<ViewId> {
ctx.refreshes()
.filter(|_| cli_core::render::parse_wire(wire).is_ok())