Skip to main content

systemprompt_logging/services/cli/
display.rs

1//! Display primitives for CLI output.
2//!
3//! Defines the [`Display`] trait and the [`DisplayUtils`] helpers (levelled
4//! messages, section headers). All output goes to stderr via this sanctioned
5//! display sink.
6//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10use std::io::Write;
11
12use crate::services::cli::theme::{EmphasisType, MessageLevel, Theme};
13
14pub trait Display {
15    fn display(&self);
16}
17
18fn stderr_writeln(args: std::fmt::Arguments<'_>) {
19    let mut stderr = std::io::stderr();
20    writeln!(stderr, "{args}").ok();
21}
22
23const fn message_level_str(level: MessageLevel) -> &'static str {
24    match level {
25        MessageLevel::Success => "success",
26        MessageLevel::Warning => "warning",
27        MessageLevel::Error => "error",
28        MessageLevel::Info => "info",
29    }
30}
31
32#[derive(Debug, Copy, Clone)]
33pub struct DisplayUtils;
34
35impl DisplayUtils {
36    pub fn message(level: MessageLevel, text: &str) {
37        if crate::services::output::is_structured_output() {
38            crate::services::output::buffer_notice(message_level_str(level), text);
39            return;
40        }
41        stderr_writeln(format_args!(
42            "{} {}",
43            Theme::icon(level),
44            Theme::color(text, level)
45        ));
46    }
47
48    pub fn section_header(title: &str) {
49        stderr_writeln(format_args!(
50            "\n{}",
51            Theme::color(title, EmphasisType::Underlined)
52        ));
53    }
54
55    pub fn subsection_header(title: &str) {
56        stderr_writeln(format_args!(
57            "\n  {}",
58            Theme::color(title, EmphasisType::Bold)
59        ));
60    }
61}