diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-09 11:19:03 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-09 11:19:03 +0900 |
| commit | 6051927c8fa32cfffa102d2a170c5a6cf747a1b9 (patch) | |
| tree | 3fe6769c90cb03ca8f1b9b825e38ad459182361e /crates/shirabe-symfony-console/src/style/output_style.rs | |
| parent | e3e8806aec771e482899ed3470e920f7b291fa95 (diff) | |
| download | php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.tar.gz php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.tar.zst php-shirabe-6051927c8fa32cfffa102d2a170c5a6cf747a1b9.zip | |
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) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-console/src/style/output_style.rs')
| -rw-r--r-- | crates/shirabe-symfony-console/src/style/output_style.rs | 122 |
1 files changed, 122 insertions, 0 deletions
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<std::cell::RefCell<dyn OutputInterface>>, +} + +impl OutputStyle { + pub fn new(output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>) -> 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<std::cell::RefCell<dyn OutputInterface>> { + // 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<std::cell::RefCell<dyn OutputInterface>>, + ) -> 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::<crate::output::console_output::ConsoleOutput>() + .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<std::cell::RefCell<dyn OutputInterface>>, + ) -> Option<std::cell::Ref<'_, crate::output::console_output::ConsoleOutput>> { + std::cell::Ref::filter_map(output.borrow(), |output| { + output + .as_any() + .downcast_ref::<crate::output::console_output::ConsoleOutput>() + }) + .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<std::cell::RefCell<dyn OutputFormatterInterface>>, + ) { + self.output.borrow().set_formatter(formatter); + } + + fn get_formatter(&self) -> std::rc::Rc<std::cell::RefCell<dyn OutputFormatterInterface>> { + self.output.borrow().get_formatter() + } +} |
