Skip to main content

systemprompt_logging/services/cli/
banners.rs

1//! Banner and session-context rendering helpers for [`CliService`].
2//!
3//! These methods own the side-effect of writing branded headers and footers to
4//! stdout. They are co-located with [`CliService`] but split out for cohesion.
5
6use std::io::Write;
7use std::time::Duration;
8
9use indicatif::{ProgressBar, ProgressStyle};
10use systemprompt_traits::LogEventLevel;
11
12use super::output::publish_log;
13use super::service::CliService;
14use super::startup::{
15    render_phase_header, render_phase_info, render_phase_success, render_phase_warning,
16    render_startup_banner,
17};
18use super::table::{ServiceTableEntry, render_service_table, render_startup_complete};
19use super::theme::{EmphasisType, Theme};
20
21impl CliService {
22    pub fn startup_banner(subtitle: Option<&str>) {
23        render_startup_banner(subtitle);
24    }
25
26    pub fn phase(name: &str) {
27        publish_log(LogEventLevel::Info, "cli", &format!("Phase: {}", name));
28        render_phase_header(name);
29    }
30
31    pub fn phase_success(message: &str, detail: Option<&str>) {
32        publish_log(LogEventLevel::Info, "cli", message);
33        render_phase_success(message, detail);
34    }
35
36    pub fn phase_info(message: &str, detail: Option<&str>) {
37        publish_log(LogEventLevel::Info, "cli", message);
38        render_phase_info(message, detail);
39    }
40
41    pub fn phase_warning(message: &str, detail: Option<&str>) {
42        publish_log(LogEventLevel::Warn, "cli", message);
43        render_phase_warning(message, detail);
44    }
45
46    pub fn service_spinner(service_name: &str, port: Option<u16>) -> ProgressBar {
47        let msg = port.map_or_else(
48            || format!("Starting {}", service_name),
49            |p| format!("Starting {} on :{}", service_name, p),
50        );
51        let pb = ProgressBar::new_spinner();
52        let spinner_template = concat!("{spinner:.208}", " {msg}");
53        pb.set_style(
54            ProgressStyle::default_spinner()
55                .template(spinner_template)
56                .unwrap_or_else(|_| ProgressStyle::default_spinner()),
57        );
58        pb.set_message(msg);
59        pb.enable_steady_tick(Duration::from_millis(80));
60        pb
61    }
62
63    pub fn service_table(title: &str, services: &[ServiceTableEntry]) {
64        render_service_table(title, services);
65    }
66
67    pub fn startup_complete(duration: Duration, api_url: &str) {
68        publish_log(
69            LogEventLevel::Info,
70            "cli",
71            &format!("Startup complete in {:.1}s", duration.as_secs_f64()),
72        );
73        render_startup_complete(duration, api_url);
74    }
75
76    pub fn session_context(
77        profile: &str,
78        session_id: &systemprompt_identifiers::SessionId,
79        tenant: Option<&str>,
80    ) {
81        Self::session_context_with_url(profile, session_id, tenant, None);
82    }
83
84    pub fn session_context_with_url(
85        profile: &str,
86        session_id: &systemprompt_identifiers::SessionId,
87        tenant: Option<&str>,
88        api_url: Option<&str>,
89    ) {
90        let session_str = session_id.as_str();
91        let truncated_session = session_str
92            .get(..12)
93            .map_or_else(|| session_str.to_string(), |s| format!("{}...", s));
94
95        let tenant_info = tenant.map_or_else(String::new, |t| format!(" | tenant: {}", t));
96
97        let url_info = api_url.map_or_else(String::new, |u| format!(" | {}", u));
98
99        let banner = format!(
100            "[profile: {} | session: {}{}{}]",
101            profile, truncated_session, tenant_info, url_info
102        );
103
104        let mut stdout = std::io::stdout();
105        writeln!(stdout, "{}", Theme::color(&banner, EmphasisType::Dim)).ok();
106    }
107
108    pub fn profile_banner(profile_name: &str, is_cloud: bool, tenant: Option<&str>) {
109        let target_label = if is_cloud { "cloud" } else { "local" };
110        let tenant_info = tenant.map_or_else(String::new, |t| format!(" | tenant: {}", t));
111        let banner = format!(
112            "[profile: {} ({}){}]",
113            profile_name, target_label, tenant_info
114        );
115        let mut stdout = std::io::stdout();
116        writeln!(stdout, "{}", Theme::color(&banner, EmphasisType::Dim)).ok();
117    }
118}