commit_wizard/infra/ui/
mod.rs1use std::cell::RefCell;
11use std::io::{self, Write};
12mod logger;
13mod prompt;
14
15use crate::engine::{Error, ErrorCode};
16use scriba::{Config, Format, Logger, Meta, Output, envelope, output::render};
17pub type UiResult<T> = Result<T, Error>;
18
19thread_local! {
21 static UI_CACHE: RefCell<Option<Ui>> = const { RefCell::new(None) };
22}
23
24#[derive(Debug, Clone)]
25pub struct Ui {
26 config: Config,
27 envelope: envelope::EnvelopeConfig,
28 prompt_theme: scriba::prompt::PromptTheme,
29}
30
31impl Ui {
32 pub fn new() -> Self {
33 Self::with_config(Config::default())
34 }
35
36 pub fn with_config(config: Config) -> Self {
37 Self {
38 config,
39 envelope: envelope::EnvelopeConfig::default(),
40 prompt_theme: scriba::prompt::PromptTheme::default(),
41 }
42 }
43
44 pub fn cached_with_config(config: Config, envelope_mode: envelope::EnvelopeMode) -> Self {
48 UI_CACHE.with(|cache| {
49 let mut cached = cache.borrow_mut();
50 match cached.as_ref() {
51 Some(ui) if ui.config == config && ui.envelope.mode == envelope_mode => ui.clone(),
52 _ => {
53 let ui = Self::with_config(config).with_envelope_mode(envelope_mode);
54 *cached = Some(ui.clone());
55 ui
56 }
57 }
58 })
59 }
60
61 pub fn config(&self) -> &Config {
63 &self.config
64 }
65
66 pub fn use_color(&self) -> bool {
67 if self.config.color == scriba::ColorMode::Always
68 || (self.config.color == scriba::ColorMode::Auto && self.config.interactive)
69 {
70 return true;
71 }
72 false
73 }
74
75 pub fn envelope(&self) -> &envelope::EnvelopeConfig {
77 &self.envelope
78 }
79
80 pub fn with_envelope(mut self, config: envelope::EnvelopeConfig) -> Self {
81 self.envelope = config;
82 self
83 }
84
85 pub fn with_envelope_mode(mut self, mode: envelope::EnvelopeMode) -> Self {
86 self.envelope.mode = mode;
87 self
88 }
89
90 pub fn with_envelope_layout(mut self, layout: envelope::EnvelopeLayout) -> Self {
91 self.envelope.layout = layout;
92 self
93 }
94
95 pub fn with_envelope_fields(mut self, fields: envelope::EnvelopeFields) -> Self {
96 self.envelope.fields = fields;
97 self
98 }
99
100 pub fn with_prompt_theme(mut self, theme: scriba::prompt::PromptTheme) -> Self {
101 self.prompt_theme = theme;
102 self
103 }
104
105 pub fn prompt_theme(&self) -> &scriba::prompt::PromptTheme {
106 &self.prompt_theme
107 }
108
109 pub fn with_format(mut self, format: Format) -> Self {
110 self.config.format = format;
111 self
112 }
113
114 pub fn interactive(mut self, value: bool) -> Self {
115 self.config.interactive = value;
116 self
117 }
118
119 pub fn auto_yes(mut self, value: bool) -> Self {
120 self.config.auto_yes = value;
121 self
122 }
123
124 pub fn logger(&self) -> Logger<'_> {
125 Logger::new(&self.config)
126 }
127
128 pub fn new_output_content(&self) -> Output {
129 Output::new()
130 }
131
132 pub fn new_output_meta(&self) -> Meta {
133 Meta::default()
134 }
135
136 pub fn render(&self, output: &Output) -> UiResult<String> {
137 render::render_output(self.config.format, output).map_err(map_scriba_error_to_cw_error)
138 }
139
140 pub fn print(&self, output: &Output) -> UiResult<()> {
141 self.print_with_meta(output, None, true)
142 }
143
144 pub fn print_with_meta(&self, output: &Output, meta: Option<&Meta>, ok: bool) -> UiResult<()> {
145 let text = if self.envelope.mode.is_json() {
146 let content = render::render_output_value(self.config.format, output)
147 .map_err(map_scriba_error_to_cw_error)?;
148 let wrapped = envelope::wrap(
149 &self.envelope,
150 self.config.format.as_str(),
151 content,
152 meta,
153 ok,
154 );
155 serde_json::to_string_pretty(&wrapped)?
156 } else {
157 self.render(output)?
158 };
159 let mut stdout = io::stdout();
160 stdout.write_all(text.as_bytes())?;
161 stdout.flush()?;
162 Ok(())
163 }
164
165 pub fn git_diff(&self, diff: &str) -> String {
166 scriba::output::render_colored_diff(diff, self.use_color())
167 }
168}
169
170impl Default for Ui {
171 fn default() -> Self {
172 Self::new()
173 }
174}
175
176fn map_scriba_error_to_cw_error(err: scriba::Error) -> Error {
177 ErrorCode::UiPromptFailed
178 .error()
179 .with_context("error", err.to_string())
180}