aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-console/src/output/trimmed_buffer_output.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-08-09 11:19:03 +0900
committernsfisis <nsfisis@gmail.com>2026-08-09 11:19:03 +0900
commit6051927c8fa32cfffa102d2a170c5a6cf747a1b9 (patch)
tree3fe6769c90cb03ca8f1b9b825e38ad459182361e /crates/shirabe-symfony-console/src/output/trimmed_buffer_output.rs
parente3e8806aec771e482899ed3470e920f7b291fa95 (diff)
downloadphp-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/output/trimmed_buffer_output.rs')
-rw-r--r--crates/shirabe-symfony-console/src/output/trimmed_buffer_output.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-console/src/output/trimmed_buffer_output.rs b/crates/shirabe-symfony-console/src/output/trimmed_buffer_output.rs
new file mode 100644
index 00000000..9f8be71a
--- /dev/null
+++ b/crates/shirabe-symfony-console/src/output/trimmed_buffer_output.rs
@@ -0,0 +1,99 @@
+//! ref: composer/vendor/symfony/console/Output/TrimmedBufferOutput.php
+
+use crate::exception::InvalidArgumentException;
+use crate::formatter::OutputFormatterInterface;
+use crate::output::OutputInterface;
+use crate::output::output::{DoWrite, Output};
+
+/// A BufferedOutput that keeps only the last N chars.
+#[derive(Debug)]
+pub struct TrimmedBufferOutput {
+ inner: Output,
+ max_length: i64,
+ buffer: std::cell::RefCell<String>,
+}
+
+impl TrimmedBufferOutput {
+ pub fn new(
+ max_length: i64,
+ verbosity: Option<i64>,
+ decorated: bool,
+ formatter: Option<std::rc::Rc<std::cell::RefCell<dyn OutputFormatterInterface>>>,
+ ) -> Result<Self, InvalidArgumentException> {
+ if max_length <= 0 {
+ return Err(InvalidArgumentException::new(format!(
+ "\"{}()\" expects a strictly positive maxLength. Got {}.",
+ "Symfony\\Component\\Console\\Output\\TrimmedBufferOutput::__construct", max_length,
+ )));
+ }
+
+ Ok(Self {
+ inner: Output::new(verbosity, decorated, formatter),
+ max_length,
+ buffer: std::cell::RefCell::new(String::new()),
+ })
+ }
+
+ /// Empties buffer and returns its content.
+ pub fn fetch(&self) -> String {
+ let content = self.buffer.borrow().clone();
+ *self.buffer.borrow_mut() = String::new();
+
+ content
+ }
+}
+
+impl DoWrite for TrimmedBufferOutput {
+ fn do_write(&self, message: &str, newline: bool) {
+ self.buffer.borrow_mut().push_str(message);
+
+ if newline {
+ self.buffer.borrow_mut().push_str(shirabe_php_shim::PHP_EOL);
+ }
+
+ let trimmed = shirabe_php_shim::substr(&self.buffer.borrow(), 0 - self.max_length, None);
+ *self.buffer.borrow_mut() = trimmed;
+ }
+}
+
+impl OutputInterface for TrimmedBufferOutput {
+ fn write(&self, messages: &[String], newline: bool, options: i64) {
+ self.inner.write(self, messages, newline, options);
+ }
+ fn writeln(&self, messages: &[String], options: i64) {
+ self.inner.writeln(self, messages, options);
+ }
+ fn set_verbosity(&self, level: i64) {
+ self.inner.set_verbosity(level);
+ }
+ fn get_verbosity(&self) -> i64 {
+ self.inner.get_verbosity()
+ }
+ fn is_quiet(&self) -> bool {
+ self.inner.is_quiet()
+ }
+ fn is_verbose(&self) -> bool {
+ self.inner.is_verbose()
+ }
+ fn is_very_verbose(&self) -> bool {
+ self.inner.is_very_verbose()
+ }
+ fn is_debug(&self) -> bool {
+ self.inner.is_debug()
+ }
+ fn set_decorated(&self, decorated: bool) {
+ self.inner.set_decorated(decorated);
+ }
+ fn is_decorated(&self) -> bool {
+ self.inner.is_decorated()
+ }
+ fn set_formatter(
+ &self,
+ formatter: std::rc::Rc<std::cell::RefCell<dyn OutputFormatterInterface>>,
+ ) {
+ self.inner.set_formatter(formatter);
+ }
+ fn get_formatter(&self) -> std::rc::Rc<std::cell::RefCell<dyn OutputFormatterInterface>> {
+ self.inner.get_formatter()
+ }
+}