From 6051927c8fa32cfffa102d2a170c5a6cf747a1b9 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 9 Aug 2026 11:19:03 +0900 Subject: refactor(symfony-console): extract symfony/console into the shirabe-symfony-console crate Move `Symfony\Component\Console` out of shirabe-external-packages and into its own crate, so the path is `shirabe_symfony_console::application::Application` instead of `shirabe_external_packages::symfony::console::application::Application`. The `delegate_to_inner!` and `delegate_command_trait_impls_to_inner!` macros move with it. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/helper/formatter_helper.rs | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 crates/shirabe-symfony-console/src/helper/formatter_helper.rs (limited to 'crates/shirabe-symfony-console/src/helper/formatter_helper.rs') diff --git a/crates/shirabe-symfony-console/src/helper/formatter_helper.rs b/crates/shirabe-symfony-console/src/helper/formatter_helper.rs new file mode 100644 index 00000000..f4e1b643 --- /dev/null +++ b/crates/shirabe-symfony-console/src/helper/formatter_helper.rs @@ -0,0 +1,99 @@ +//! ref: composer/vendor/symfony/console/Helper/FormatterHelper.php + +use crate::formatter::output_formatter::OutputFormatter; +use crate::helper::helper::Helper; +use crate::helper::helper_interface::HelperInterface; +use crate::helper::helper_set::HelperSet; + +/// The Formatter class provides helpers to format messages. +#[derive(Debug, Default)] +pub struct FormatterHelper { + inner: Helper, +} + +impl FormatterHelper { + /// Formats a message within a section. + pub fn format_section(&self, section: &str, message: &str, style: &str) -> String { + format!("<{}>[{}] {}", style, section, style, message) + } + + /// Formats a message as a block of text. + /// + /// @param string|array $messages The message to write in the block + pub fn format_block(&self, messages: FormatBlockMessages, style: &str, large: bool) -> String { + let messages = match messages { + FormatBlockMessages::String(message) => vec![message], + FormatBlockMessages::Array(messages) => messages, + }; + + let mut len: i64 = 0; + let mut lines: Vec = Vec::new(); + for message in &messages { + let message = OutputFormatter::escape(message).unwrap(); + lines.push(if large { + format!(" {} ", message) + } else { + format!(" {} ", message) + }); + len = std::cmp::max(Helper::width(&message) + (if large { 4 } else { 2 }), len); + } + + let mut messages: Vec = if large { + vec![shirabe_php_shim::str_repeat(" ", len as usize)] + } else { + vec![] + }; + let mut i = 0; + while i < lines.len() { + messages.push(format!( + "{}{}", + lines[i], + shirabe_php_shim::str_repeat(" ", (len - Helper::width(&lines[i])) as usize) + )); + i += 1; + } + if large { + messages.push(shirabe_php_shim::str_repeat(" ", len as usize)); + } + + let mut i = 0; + while i < messages.len() { + messages[i] = format!("<{}>{}", style, messages[i].clone(), style); + i += 1; + } + + messages.join("\n") + } + + /// Truncates a message to the given length. + pub fn truncate(&self, message: &str, length: i64, suffix: &str) -> String { + let computed_length = length - Helper::width(suffix); + + if computed_length > Helper::width(message) { + return message.to_string(); + } + + format!("{}{}", Helper::substr(message, 0, Some(length)), suffix) + } +} + +impl HelperInterface for FormatterHelper { + fn set_helper_set(&mut self, helper_set: Option>>) { + self.inner.set_helper_set(helper_set); + } + + fn get_helper_set(&self) -> Option>> { + self.inner.get_helper_set() + } + + fn get_name(&self) -> String { + "formatter".to_string() + } +} + +/// `formatBlock` accepts either a single string or an array of strings. +#[derive(Debug)] +pub enum FormatBlockMessages { + String(String), + Array(Vec), +} -- cgit v1.3.1-4-g156e