From b09e687aec359269d129049b04f5d4f4f3517884 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 15 May 2026 00:35:12 +0900 Subject: feat(port): port HtmlOutputFormatter.php --- .../shirabe/src/console/html_output_formatter.rs | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'crates/shirabe/src/console/html_output_formatter.rs') diff --git a/crates/shirabe/src/console/html_output_formatter.rs b/crates/shirabe/src/console/html_output_formatter.rs index e2b032a..f34ca1b 100644 --- a/crates/shirabe/src/console/html_output_formatter.rs +++ b/crates/shirabe/src/console/html_output_formatter.rs @@ -1 +1,84 @@ //! ref: composer/src/Composer/Console/HtmlOutputFormatter.php + +use shirabe_external_packages::composer::pcre::preg::Preg; +use shirabe_external_packages::symfony::console::formatter::output_formatter::OutputFormatter; +use shirabe_external_packages::symfony::console::formatter::output_formatter_style::OutputFormatterStyle; +use indexmap::IndexMap; + +#[derive(Debug)] +pub struct HtmlOutputFormatter { + inner: OutputFormatter, +} + +impl HtmlOutputFormatter { + const AVAILABLE_FOREGROUND_COLORS: &'static [(i64, &'static str)] = &[ + (30, "black"), + (31, "red"), + (32, "green"), + (33, "yellow"), + (34, "blue"), + (35, "magenta"), + (36, "cyan"), + (37, "white"), + ]; + + const AVAILABLE_BACKGROUND_COLORS: &'static [(i64, &'static str)] = &[ + (40, "black"), + (41, "red"), + (42, "green"), + (43, "yellow"), + (44, "blue"), + (45, "magenta"), + (46, "cyan"), + (47, "white"), + ]; + + const AVAILABLE_OPTIONS: &'static [(i64, &'static str)] = &[ + (1, "bold"), + (4, "underscore"), + //5 => "blink", + //7 => "reverse", + //8 => "conceal" + ]; + + pub fn new(styles: IndexMap) -> Self { + Self { + inner: OutputFormatter::new(true, styles), + } + } + + pub fn format(&self, message: Option<&str>) -> Option { + let formatted = self.inner.format(message)?; + + let clear_escape_codes = "(?:39|49|0|22|24|25|27|28)"; + let pattern = format!( + "{{\\033\\[([0-9;]+)m(.*?)\\033\\[(?:{};)*?{}m}}s", + clear_escape_codes, clear_escape_codes + ); + + Preg::replace_callback(&pattern, |matches| self.format_html(matches), formatted).ok() + } + + fn format_html(&self, matches: Vec>) -> String { + let codes_str = matches[1].as_deref().unwrap_or(""); + let content = matches[2].as_deref().unwrap_or(""); + let mut out = String::from(" out.push_str("font-weight:bold;"), + "underscore" => out.push_str("text-decoration:underline;"), + _ => {} + } + } + } + + format!("{}\">{}", out, content) + } +} -- cgit v1.3.1