use std::path::Path;
use rmux_core::{OptionStore, Session};
use rmux_proto::OptionName;
use crate::client_format::ClientFormatBindings;
use crate::format_runtime::RuntimeFormatContext;
use crate::pane_terminals::HandlerState;
use super::status::{
active_format_context, render_status_template_jobs_with_profile, status_job_cache_ttl,
};
pub(crate) struct ClientTitleContext<'a> {
pub(crate) state: Option<&'a HandlerState>,
pub(crate) attached_count: usize,
pub(crate) key_table: Option<&'a str>,
pub(crate) socket_path: Option<&'a Path>,
pub(crate) client: Option<&'a ClientFormatBindings>,
}
pub(crate) fn expand_client_title(
session: &Session,
options: &OptionStore,
context: &ClientTitleContext<'_>,
) -> Option<String> {
let session_name = session.name();
if options.resolve(Some(session_name), OptionName::SetTitles) != Some("on") {
return None;
}
let template = options
.resolve(Some(session_name), OptionName::SetTitlesString)
.unwrap_or_default();
let mut runtime = RuntimeFormatContext::new(active_format_context(
session,
context.attached_count,
context.key_table,
context.socket_path,
))
.with_options(options)
.with_session(session)
.with_window(session.active_window_index(), session.window());
if let Some(state) = context.state {
runtime = runtime.with_state(state);
}
if let Some(pane) = session.window().active_pane() {
runtime = runtime.with_pane(pane);
}
if let Some(client) = context.client {
runtime = client.apply(runtime);
}
let profile = template
.contains("#(")
.then(|| runtime.status_job_profile())
.flatten();
Some(render_status_template_jobs_with_profile(
template,
&runtime,
profile.as_ref(),
status_job_cache_ttl(options, session_name),
runtime.status_jobs(),
))
}