systemprompt_logging/services/cli/
service.rs1use std::io::Write;
10use std::time::Duration;
11
12use crate::models::LoggingError;
13pub(super) type Result<T> = std::result::Result<T, LoggingError>;
14use indicatif::{ProgressBar, ProgressStyle};
15use serde::Serialize;
16use systemprompt_traits::LogEventLevel;
17
18use super::display::{CollectionDisplay, Display, DisplayUtils};
19use super::module::{BatchModuleOperations, ModuleDisplay, ModuleInstall, ModuleUpdate};
20use super::output::publish_log;
21use super::prompts::{PromptBuilder, Prompts};
22use super::summary::{OperationResult, ProgressSummary, ValidationSummary};
23use super::theme::{EmphasisType, ItemStatus, MessageLevel, ModuleType, Theme};
24
25#[derive(Copy, Clone, Debug)]
26pub struct CliService;
27
28impl CliService {
29 pub fn success(message: &str) {
30 publish_log(LogEventLevel::Info, "cli", message);
31 DisplayUtils::message(MessageLevel::Success, message);
32 }
33
34 pub fn warning(message: &str) {
35 publish_log(LogEventLevel::Warn, "cli", message);
36 DisplayUtils::message(MessageLevel::Warning, message);
37 }
38
39 pub fn error(message: &str) {
40 publish_log(LogEventLevel::Error, "cli", message);
41 DisplayUtils::message(MessageLevel::Error, message);
42 }
43
44 pub fn info(message: &str) {
45 publish_log(LogEventLevel::Info, "cli", message);
46 DisplayUtils::message(MessageLevel::Info, message);
47 }
48
49 pub fn debug(message: &str) {
50 let debug_msg = format!("DEBUG: {message}");
51 publish_log(LogEventLevel::Debug, "cli", &debug_msg);
52 DisplayUtils::message(MessageLevel::Info, &debug_msg);
53 }
54
55 pub fn verbose(message: &str) {
56 publish_log(LogEventLevel::Debug, "cli", message);
57 DisplayUtils::message(MessageLevel::Info, message);
58 }
59
60 pub fn fatal(message: &str, exit_code: i32) -> ! {
61 let fatal_msg = format!("FATAL: {message}");
62 DisplayUtils::message(MessageLevel::Error, &fatal_msg);
63 std::process::exit(exit_code);
64 }
65
66 pub fn section(title: &str) {
67 DisplayUtils::section_header(title);
68 }
69
70 pub fn subsection(title: &str) {
71 DisplayUtils::subsection_header(title);
72 }
73
74 pub fn clear_screen() {
75 let mut stderr = std::io::stderr();
76 if let Err(e) = write!(stderr, "\x1B[2J\x1B[1;1H") {
77 tracing::debug!(error = %e, "cli clear_screen write failed");
78 }
79 }
80
81 pub fn output(content: &str) {
82 let mut stdout = std::io::stdout();
83 if let Err(e) = writeln!(stdout, "{content}") {
84 tracing::debug!(error = %e, "cli output write failed");
85 }
86 }
87
88 pub fn json<T: Serialize>(value: &T) {
89 match serde_json::to_string_pretty(value) {
90 Ok(json) => {
91 let mut stdout = std::io::stdout();
92 if let Err(e) = writeln!(stdout, "{json}") {
93 tracing::debug!(error = %e, "cli json write failed");
94 }
95 },
96 Err(e) => Self::error(&format!("Failed to format log entry: {e}")),
97 }
98 }
99
100 pub fn json_compact<T: Serialize>(value: &T) {
101 match serde_json::to_string(value) {
102 Ok(json) => {
103 let mut stdout = std::io::stdout();
104 if let Err(e) = writeln!(stdout, "{json}") {
105 tracing::debug!(error = %e, "cli json_compact write failed");
106 }
107 },
108 Err(e) => Self::error(&format!("Failed to format log entry: {e}")),
109 }
110 }
111
112 pub fn yaml<T: Serialize>(value: &T) {
113 match serde_yaml::to_string(value) {
114 Ok(yaml) => {
115 let mut stdout = std::io::stdout();
116 if let Err(e) = write!(stdout, "{yaml}") {
117 tracing::debug!(error = %e, "cli yaml write failed");
118 }
119 },
120 Err(e) => Self::error(&format!("Failed to format log entry: {e}")),
121 }
122 }
123
124 pub fn key_value(label: &str, value: &str) {
125 let mut stderr = std::io::stderr();
126 if let Err(e) = writeln!(
127 stderr,
128 "{}: {}",
129 Theme::color(label, EmphasisType::Bold),
130 Theme::color(value, EmphasisType::Highlight)
131 ) {
132 tracing::debug!(error = %e, "cli key_value write failed");
133 }
134 }
135
136 pub fn status_line(label: &str, value: &str, status: ItemStatus) {
137 let mut stderr = std::io::stderr();
138 if let Err(e) = writeln!(
139 stderr,
140 "{} {}: {}",
141 Theme::icon(status),
142 Theme::color(label, EmphasisType::Bold),
143 Theme::color(value, status)
144 ) {
145 tracing::debug!(error = %e, "cli status_line write failed");
146 }
147 }
148
149 pub fn spinner(message: &str) -> ProgressBar {
150 let pb = ProgressBar::new_spinner();
151 pb.set_style(
152 ProgressStyle::default_spinner()
153 .template("{spinner:.cyan} {msg}")
154 .unwrap_or_else(|_| ProgressStyle::default_spinner()),
155 );
156 pb.set_message(message.to_owned());
157 pb.enable_steady_tick(Duration::from_millis(100));
158 pb
159 }
160
161 pub fn progress_bar(total: u64) -> ProgressBar {
162 let pb = ProgressBar::new(total);
163 pb.set_style(
164 ProgressStyle::default_bar()
165 .template(
166 "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}",
167 )
168 .unwrap_or_else(|_| ProgressStyle::default_bar())
169 .progress_chars("#>-"),
170 );
171 pb
172 }
173
174 pub fn timed<F, R>(label: &str, f: F) -> R
175 where
176 F: FnOnce() -> R,
177 {
178 let start = std::time::Instant::now();
179 let result = f();
180 let duration = start.elapsed();
181 let duration_secs = duration.as_secs_f64();
182 let info_msg = format!("{label} completed in {duration_secs:.2}s");
183 Self::info(&info_msg);
184 result
185 }
186
187 pub fn prompt_schemas(module_name: &str, schemas: &[(String, String)]) -> Result<bool> {
188 ModuleDisplay::prompt_apply_schemas(module_name, schemas)
189 }
190
191 pub fn prompt_seeds(module_name: &str, seeds: &[(String, String)]) -> Result<bool> {
192 ModuleDisplay::prompt_apply_seeds(module_name, seeds)
193 }
194
195 pub fn prompt_install(modules: &[String]) -> Result<bool> {
196 Prompts::confirm_install(modules)
197 }
198
199 pub fn prompt_update(updates: &[(String, String, String)]) -> Result<bool> {
200 Prompts::confirm_update(updates)
201 }
202
203 pub fn confirm(question: &str) -> Result<bool> {
204 Prompts::confirm(question, false)
205 }
206
207 pub fn confirm_default_yes(question: &str) -> Result<bool> {
208 Prompts::confirm(question, true)
209 }
210
211 pub fn display_validation_summary(summary: &ValidationSummary) {
212 summary.display();
213 }
214
215 pub fn display_result(result: &OperationResult) {
216 result.display();
217 }
218
219 pub fn display_progress(progress: &ProgressSummary) {
220 progress.display();
221 }
222
223 pub fn prompt_builder(message: &str) -> PromptBuilder {
224 PromptBuilder::new(message)
225 }
226
227 pub fn collection<T: Display>(title: &str, items: Vec<T>) -> CollectionDisplay<T> {
228 CollectionDisplay::new(title, items)
229 }
230
231 pub fn module_status(module_name: &str, message: &str) {
232 DisplayUtils::module_status(module_name, message);
233 }
234
235 pub fn relationship(from: &str, to: &str, status: ItemStatus, module_type: ModuleType) {
236 DisplayUtils::relationship(module_type, from, to, status);
237 }
238
239 pub fn item(status: ItemStatus, name: &str, detail: Option<&str>) {
240 DisplayUtils::item(status, name, detail);
241 }
242
243 pub fn batch_install(modules: &[ModuleInstall]) -> Result<bool> {
244 BatchModuleOperations::prompt_install_multiple(modules)
245 }
246
247 pub fn batch_update(updates: &[ModuleUpdate]) -> Result<bool> {
248 BatchModuleOperations::prompt_update_multiple(updates)
249 }
250
251 pub fn table(headers: &[&str], rows: &[Vec<String>]) {
252 super::table::render_table(headers, rows);
253 }
254}