use std::path::Path;
use crate::actions::{nudge_enabled, stop_children_offered, stop_enabled};
use crate::app::Snapshot;
use crate::control::hold::Held;
use crate::git_tree::{Agent, AgentMark, AgentState};
use crate::nav::convs::{
Flight, ancestors, conversation_flight, display_name_of, root_of, seats, strip,
};
use crate::ui_state::UiState;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AgentView {
pub agent_id: String,
pub root: String,
pub ancestors: Vec<String>,
pub name: String,
pub display_only: bool,
pub tip: String,
pub state: AgentState,
pub marks: Vec<AgentMark>,
pub held: Option<Held>,
pub flight: Option<Flight>,
pub present: bool,
pub nudgeable: bool,
pub stoppable: bool,
pub stop_children: bool,
pub seats: Vec<crate::nav::convs::Seat>,
pub strip: Option<crate::nav::convs::FlightStrip>,
pub spend: crate::spend::Figure,
pub context: Option<crate::context::Fullness>,
}
pub fn agent(snap: &Snapshot, ui: &UiState, ws: &Path, agent: &str, now_unix: i64) -> AgentView {
let agents = snap
.trees
.get(ws)
.map(|tree| tree.agents.clone())
.unwrap_or_default();
let root = root_of(&agents, agent).unwrap_or_else(|| agent.to_owned());
let found = agents.iter().find(|a| a.agent_id == agent);
let bills = snap.bills.get(ws).cloned().unwrap_or_default();
AgentView {
spend: crate::spend::of_conversation(&bills, &root, &ui.prices()),
context: crate::context::of_conversation(&bills, &root, &snap.windows),
name: display_name_of(&agents, &root),
display_only: agents
.iter()
.any(|a| a.agent_id == root && a.name_display_only()),
tip: found.map(|a| a.tip_oid.clone()).unwrap_or_default(),
state: found.map_or(AgentState::Stopped, |a| a.state),
marks: found.map(Agent::marks).unwrap_or_default(),
held: found.and_then(|a| a.held.clone()),
present: found.is_some(),
nudgeable: nudge_enabled(Some(agent), &agents),
flight: conversation_flight(&agents, &root),
stoppable: stop_enabled(Some(agent), &agents),
stop_children: stop_children_offered(agent, &agents),
ancestors: ancestors(&agents, agent),
seats: seats(&agents, &root),
strip: strip(&agents, &root, now_unix),
agent_id: agent.to_owned(),
root,
}
}
#[cfg(test)]
mod tests;