Lines
96.26 %
Functions
71.43 %
Branches
100 %
//! Eval bridge and routing logic for `App`.
//!
//! Methods that touch `console_eval`, submit nomiscript forms, route replies,
//! and keep the tab-fetch state machines in sync all live here so `app.rs`
//! stays focused on pure state shape and navigation.
use crate::tabs::nms::ConsoleState;
use cli_core::eval::config_text;
use cli_core::render::{WireValue, parse_wire};
use crate::app::App;
use crate::app::deliver::refresh_after;
use crate::app::pending::rejection;
use crate::form::Form;
use crate::route::{Route, RouteCtx};
use crate::tabs::config::ConfigTab;
use crate::tabs::fetch::Fetch;
use crate::tabs::list::ListTab;
use crate::tabs::nms::format_result;
use crate::tabs::nms_eval::{DrainItem, MAIN_STREAM, UNAVAILABLE};
use crate::view::ViewId;
use crate::window::WindowId;
impl App {
pub fn submit_console_form(&mut self, console: WindowId, form: String) {
if let Some(state) = self.win_mut::<ConsoleState>(console) {
state.push_scrollback(format!("> {form}"));
state.reset_scroll();
}
if self.console_eval.is_none() {
self.notify(vec!["console not connected".to_string()]);
return;
let id = self
.console_stream(console)
.and_then(|stream| self.submit_routed(stream, Some(console), RouteCtx::None, form));
if id.is_none() {
self.notify(vec![UNAVAILABLE.to_string()]);
/// The stream of console `console`; its first call opens it.
fn console_stream(&mut self, console: WindowId) -> Option<u32> {
if let Some(stream) = self.win::<ConsoleState>(console)?.stream {
return Some(stream);
let stream = self.console_eval.as_mut()?.open_stream()?;
self.win_mut::<ConsoleState>(console)?.stream = Some(stream);
Some(stream)
/// Closes the stream of console `console` when it had opened one.
pub(super) fn close_console_stream(&mut self, console: WindowId) {
let stream = self.win::<ConsoleState>(console).and_then(|c| c.stream);
if let (Some(stream), Some(eval)) = (stream, &mut self.console_eval) {
eval.close(stream);
/// Submits `form` to the front-most console window, or to a new one; returns it.
pub fn submit_to_console(&mut self, form: String) -> WindowId {
let console = self.window_or_open(ViewId::Console).window;
self.submit_console_form(console, form);
console
/// Shows the first line of an app notice in the status bar; the whole text goes to the log.
pub(super) fn notify(&mut self, lines: Vec<String>) {
let Some(first) = lines.first() else {
};
tracing::warn!("{}", lines.join("\n"));
let mut shown = first.clone();
if lines.len() > 1 {
shown.push_str(" … (see log)");
self.set_status(shown);
/// Aborts what the front console is evaluating.
pub fn interrupt_console(&mut self) {
let stream = self.front::<ConsoleState>().and_then(|c| c.stream);
eval.interrupt(stream);
/// Applies every ready reply; `true` when there was one, so the screen is stale.
pub fn drain_eval(&mut self) -> bool {
let items = {
let Some(eval) = &mut self.console_eval else {
return false;
eval.drain()
let any = !items.is_empty();
for item in items {
self.apply_drained(item);
any
pub(super) fn apply_drained(&mut self, item: DrainItem) {
match item {
DrainItem::Routed { id, wire } => self.route_reply(id, &wire),
DrainItem::Unroutable(wire) => self.notify(format_result(&wire)),
pub fn fetch_config(&mut self, window: WindowId) {
let Some(seq) = self
.win_mut::<ConfigTab>(window)
.map(|tab| tab.seq.advance())
else {
let ctx = RouteCtx::Config { seq };
if let Some(id) = self.dispatch_eval(window, ctx, "(list-config)".to_string())
&& let Some(config) = self.win_mut::<ConfigTab>(window)
{
config.set_loading(id);
pub(super) fn ensure_list_loaded(&mut self, window: WindowId) {
let Some(list) = self.win::<ListTab>(window) else {
if !matches!(list.state, Fetch::Idle) {
let form = list.request_form();
if let Some(id) = self.dispatch_eval(window, RouteCtx::None, form)
&& let Some(list) = self.win_mut::<ListTab>(window)
list.set_loading_id(id);
/// Submit `form` to the eval worker, insert a `Route` to `window`, and return the id.
///
/// Returns `None` when no console is attached or its ids ran out.
pub(super) fn dispatch_eval(
&mut self,
window: WindowId,
ctx: RouteCtx,
form: String,
) -> Option<i64> {
self.submit_routed(MAIN_STREAM, Some(window), ctx, form)
/// Like `dispatch_eval`, for a reply that belongs to the app or a dialog.
pub(super) fn dispatch_app_eval(&mut self, ctx: RouteCtx, form: String) -> Option<i64> {
self.submit_routed(MAIN_STREAM, None, ctx, form)
fn submit_routed(
stream: u32,
target: Option<WindowId>,
let eval = self.console_eval.as_mut()?;
let id = eval.submit(stream, form)?;
self.pending_routes.insert(id, Route { target, ctx });
Some(id)
/// Drop the filter of Transactions window `window` and fetch the whole list again.
pub fn clear_transaction_filter(&mut self, window: WindowId) {
self.next_filter_seq(window);
if self
.win_mut::<ListTab>(window)
.is_some_and(ListTab::clear_filter)
self.load_window(window);
/// Route one drained reply to its destination window, or, if the id has
/// no pending route, show the reply as an orphan-warn status. A reply to the
/// pending form's own request settles the form first.
pub(crate) fn route_reply(&mut self, id: i64, wire: &str) {
if let Some(route) = self.take_orphan_route(id) {
self.settle_orphan(route, wire);
let form = self.take_pending_form(id);
let route = self.pending_routes.remove(&id);
let mutates = route.as_ref().is_some_and(|route| route.ctx.mutates());
let target = route.as_ref().and_then(|route| route.target);
let failed = parse_wire(wire).is_err();
if !failed && target.is_some() && self.status_owner == target {
self.clear_status();
let before = self.status.clone();
match (route, form) {
(route, Some(form)) => self.settle_pending_form(form, route, wire),
(Some(route), None) if self.is_stale_page(&route, id) => {}
(Some(route), None) => self.deliver_reply(route, wire),
(None, None) => {
let mut lines = format_result(wire);
let head = format!("[warn] orphan reply id={id}");
match lines.first_mut() {
Some(first) => *first = format!("{head}: {first}"),
None => lines.push(head),
self.notify(lines);
if failed && self.status != before {
self.status_owner = target.filter(|_| !mutates);
/// The route of request `id` when the window it targeted has closed.
fn take_orphan_route(&mut self, id: i64) -> Option<Route> {
let target = self.pending_routes.get(&id)?.target?;
match self.window_view(target) {
Some(_) => None,
None => self.pending_routes.remove(&id),
/// A reply for a closed window is dropped, except that a succeeded mutation still
/// refreshes every window of its kind.
fn settle_orphan(&mut self, route: Route, wire: &str) {
if matches!(route.ctx, RouteCtx::BulkDelete { .. }) {
return self.settle_orphan_bulk(wire);
let Some(kind) = refresh_after(&route.ctx, wire) else {
self.refresh_windows(kind);
if matches!(route.ctx, RouteCtx::ConfigSet { .. }) {
self.read_ui_config();
/// A page fetch of the Transactions tab nobody waits for any more: a newer
/// request (a filter, a refresh) superseded it.
fn is_stale_page(&self, route: &Route, id: i64) -> bool {
let Some(window) = route.target else {
self.window_view(window) == Some(ViewId::Transactions)
&& matches!(route.ctx, RouteCtx::None)
&& self
.win::<ListTab>(window)
.is_some_and(|list| !list.awaits(id))
fn settle_pending_form(&mut self, form: Form, route: Option<Route>, wire: &str) {
match parse_wire(wire) {
Ok(WireValue::Value(value)) => match route {
Some(Route {
ctx: RouteCtx::MergePreview,
..
}) => self.confirm_merge_preview(form, &value),
Some(route) => self.deliver_reply(route, wire),
None => {}
},
Err(e) => {
self.rollback_options(&form);
self.unlock_pending_form(form, rejection(&e));
/// The text a `get-config` reply holds; a failed or empty read is no value.
pub(super) fn config_value_in(wire: &str) -> Option<String> {
let Ok(WireValue::Value(value)) = parse_wire(wire) else {
return None;
config_text(&value).ok().flatten()