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/style/output_style.rs | 122 +++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 crates/shirabe-symfony-console/src/style/output_style.rs (limited to 'crates/shirabe-symfony-console/src/style/output_style.rs') diff --git a/crates/shirabe-symfony-console/src/style/output_style.rs b/crates/shirabe-symfony-console/src/style/output_style.rs new file mode 100644 index 00000000..17c9489e --- /dev/null +++ b/crates/shirabe-symfony-console/src/style/output_style.rs @@ -0,0 +1,122 @@ +//! ref: composer/vendor/symfony/console/Style/OutputStyle.php + +use crate::formatter::OutputFormatterInterface; +use crate::helper::ProgressBar; +use crate::output::ConsoleOutputInterface; +use crate::output::OutputInterface; +use crate::output::output_interface::OUTPUT_NORMAL; + +/// Decorates output to add console style guide helpers. +#[derive(Debug)] +pub struct OutputStyle { + output: std::rc::Rc>, +} + +impl OutputStyle { + pub fn new(output: std::rc::Rc>) -> Self { + Self { output } + } + + pub fn create_progress_bar(&self, max: i64) -> ProgressBar { + ProgressBar::new(self.output.clone(), max, 1.0 / 25.0) + } + + pub fn new_line(&self, count: i64) { + self.output.borrow().write( + &[shirabe_php_shim::str_repeat( + shirabe_php_shim::PHP_EOL, + count as usize, + )], + false, + OUTPUT_NORMAL, + ); + } + + pub(crate) fn get_error_output(&self) -> std::rc::Rc> { + // PHP checks `$this->output instanceof ConsoleOutputInterface`; this requires + // runtime type information that the OutputInterface trait object lacks. + if !Self::is_console_output_interface(&self.output) { + return self.output.clone(); + } + + Self::as_console_output_interface(&self.output) + .unwrap() + .get_error_output() + } + + fn is_console_output_interface( + output: &std::rc::Rc>, + ) -> bool { + // ConsoleOutput is the only OutputInterface implementor that also implements + // ConsoleOutputInterface, so `instanceof ConsoleOutputInterface` reduces to this downcast. + shirabe_php_shim::AsAny::as_any(&*output.borrow()) + .downcast_ref::() + .is_some() + } + + /// PHP casts to `ConsoleOutputInterface`; `ConsoleOutput` being its only implementor, a + /// borrow of the concrete type serves as the cast result. + fn as_console_output_interface( + output: &std::rc::Rc>, + ) -> Option> { + std::cell::Ref::filter_map(output.borrow(), |output| { + output + .as_any() + .downcast_ref::() + }) + .ok() + } +} + +impl OutputInterface for OutputStyle { + fn write(&self, messages: &[String], newline: bool, options: i64) { + self.output.borrow().write(messages, newline, options); + } + + fn writeln(&self, messages: &[String], options: i64) { + self.output.borrow().writeln(messages, options); + } + + fn set_verbosity(&self, level: i64) { + self.output.borrow().set_verbosity(level); + } + + fn get_verbosity(&self) -> i64 { + self.output.borrow().get_verbosity() + } + + fn is_quiet(&self) -> bool { + self.output.borrow().is_quiet() + } + + fn is_verbose(&self) -> bool { + self.output.borrow().is_verbose() + } + + fn is_very_verbose(&self) -> bool { + self.output.borrow().is_very_verbose() + } + + fn is_debug(&self) -> bool { + self.output.borrow().is_debug() + } + + fn set_decorated(&self, decorated: bool) { + self.output.borrow().set_decorated(decorated); + } + + fn is_decorated(&self) -> bool { + self.output.borrow().is_decorated() + } + + fn set_formatter( + &self, + formatter: std::rc::Rc>, + ) { + self.output.borrow().set_formatter(formatter); + } + + fn get_formatter(&self) -> std::rc::Rc> { + self.output.borrow().get_formatter() + } +} -- cgit v1.3.1-4-g156e