lernie 0.1.64

lernie: the operator seat — the window and wire client for a yog server
//! **The reads whose subject is an engine rather than a wall** (DESIGN §4.21,
//! §4.27): its own verb table, what a needle found across everything it can
//! see, the trail of every act that crossed its boundary, and whether it is
//! wired at all.
//!
//! Split from [`super::tasks`] at the design-time budget, on the seam the
//! gestures themselves already have: these three name no workspace, so each is
//! asked of every channel this box holds and answered per channel
//! ([`crate::seat::fanned`]), where a ball or a board is one wall's.

use crate::reply::doctor::{Check, failing};
use crate::reply::help::HelpRow;
use crate::reply::ops::OpRow;
use crate::reply::search::Found;

use super::parts::{brief, line, line_over, listing, quoted, when};

/// **The trail**: every act that crossed one engine's boundary.
pub(super) fn ops(rows: &[OpRow]) -> String {
    let painted = rows
        .iter()
        .map(|row| {
            line(vec![
                Some(row.ts.clone()),
                // **Who made the act** (REMOTE §9.20). It is beside the
                // origin rather than in the detail because it is what tells
                // two identical rows apart, and nothing later can recover it.
                Some(row.client.clone()),
                Some(row.origin.clone()),
                Some(row.standing.clone()),
                when(row.failed, &format!("{} ({})", row.exit_label, row.exit)),
                Some(brief(&row.argv)),
            ])
        })
        .collect();
    listing("trail", painted, "the trail is empty")
}

/// **What a needle found**, and what could not be read looking for it — two
/// different claims, so they are two sections.
pub(super) fn found(found: &Found) -> String {
    let hits = found
        .rows
        .iter()
        .map(|hit| {
            line(vec![
                Some(hit.subject()),
                Some(hit.at_field()),
                quoted(&hit.excerpt),
            ])
        })
        .collect();
    line_over(
        &listing(
            &format!("found {:?}", found.needle),
            hits,
            "no match anywhere this engine can see",
        ),
        when(
            !found.unreadable.is_empty(),
            &format!("unread: {}", found.unreadable.join(", ")),
        ),
    )
}

/// **One engine's own verb table** — what that engine answers to.
pub(super) fn help(rows: &[HelpRow]) -> String {
    let painted = rows
        .iter()
        .map(|row| line(vec![Some(row.usage.clone()), Some(row.summary.clone())]))
        .collect();
    listing("the engine's own words", painted, "this engine names no op")
}

/// **Whether this box is wired** — one row per check, and under a row that
/// does not hold, the engine's remedy verbatim. The head counts the rows that
/// fail, because the answer carries no tally and a seat that wants one counts
/// what it was handed (`crate::reply::doctor`).
pub(super) fn doctor(rows: &[Check]) -> String {
    let painted = rows
        .iter()
        .map(|row| line_over(&row.headline(), row.remedy.clone()))
        .collect();
    let head = match failing(rows) {
        0 => "doctor: every check holds".to_owned(),
        n => format!("doctor: {n} of {} checks do not hold", rows.len()),
    };
    listing(&head, painted, "the engine ran no check")
}