aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-symfony-console/src/helper/helper.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/helper/helper.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/helper/helper.rs')
-rw-r--r--crates/shirabe-symfony-console/src/helper/helper.rs162
1 files changed, 162 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-console/src/helper/helper.rs b/crates/shirabe-symfony-console/src/helper/helper.rs
new file mode 100644
index 00000000..0af2830f
--- /dev/null
+++ b/crates/shirabe-symfony-console/src/helper/helper.rs
@@ -0,0 +1,162 @@
+//! ref: composer/vendor/symfony/console/Helper/Helper.php
+
+use crate::formatter::output_formatter_interface::OutputFormatterInterface;
+use crate::helper::helper_set::HelperSet;
+use shirabe_php_shim::php_regex;
+use shirabe_symfony_string::unicode_string::UnicodeString;
+
+/// Helper is the base class for all helper classes.
+#[derive(Debug, Default)]
+pub struct Helper {
+ pub(crate) helper_set: Option<std::rc::Rc<std::cell::RefCell<HelperSet>>>,
+}
+
+impl Helper {
+ pub fn set_helper_set(
+ &mut self,
+ helper_set: Option<std::rc::Rc<std::cell::RefCell<HelperSet>>>,
+ ) {
+ self.helper_set = helper_set;
+ }
+
+ pub fn get_helper_set(&self) -> Option<std::rc::Rc<std::cell::RefCell<HelperSet>>> {
+ self.helper_set.clone()
+ }
+
+ /// Returns the length of a string, using mb_strwidth if it is available.
+ ///
+ /// @deprecated since Symfony 5.3
+ pub fn strlen(string: &str) -> i64 {
+ shirabe_php_shim::trigger_deprecation(
+ "symfony/console",
+ "5.3",
+ "Method \"%s()\" is deprecated and will be removed in Symfony 6.0. Use Helper::width() or Helper::length() instead.",
+ "Helper::strlen",
+ );
+
+ Self::width(string)
+ }
+
+ /// Returns the width of a string, using mb_strwidth if it is available.
+ /// The width is how many characters positions the string will use.
+ pub fn width(string: &str) -> i64 {
+ if shirabe_php_shim::preg_match(php_regex!("//u"), string, &mut Vec::new()) {
+ return UnicodeString::new(string).width(false);
+ }
+
+ let encoding = shirabe_php_shim::mb_detect_encoding(string, None, true);
+ let encoding = match encoding {
+ Some(encoding) => encoding,
+ None => return shirabe_php_shim::strlen(string),
+ };
+
+ shirabe_php_shim::mb_strwidth(string, Some(&encoding))
+ }
+
+ /// Returns the length of a string, using mb_strlen if it is available.
+ /// The length is related to how many bytes the string will use.
+ pub fn length(string: &str) -> i64 {
+ if shirabe_php_shim::preg_match(php_regex!("//u"), string, &mut Vec::new()) {
+ return UnicodeString::new(string).length();
+ }
+
+ let encoding = shirabe_php_shim::mb_detect_encoding(string, None, true);
+ let encoding = match encoding {
+ Some(encoding) => encoding,
+ None => return shirabe_php_shim::strlen(string),
+ };
+
+ shirabe_php_shim::mb_strlen(string, &encoding)
+ }
+
+ /// Returns the subset of a string, using mb_substr if it is available.
+ pub fn substr(string: &str, from: i64, length: Option<i64>) -> String {
+ let encoding = shirabe_php_shim::mb_detect_encoding(string, None, true);
+ let encoding = match encoding {
+ Some(encoding) => encoding,
+ None => return shirabe_php_shim::substr(string, from, length),
+ };
+
+ shirabe_php_shim::mb_substr(string, from, length, Some(&encoding))
+ }
+
+ pub fn format_time(secs: f64) -> Option<String> {
+ // [threshold, label, divisor?]
+ let time_formats: [(f64, &str, Option<f64>); 9] = [
+ (0.0, "< 1 sec", None),
+ (1.0, "1 sec", None),
+ (2.0, "secs", Some(1.0)),
+ (60.0, "1 min", None),
+ (120.0, "mins", Some(60.0)),
+ (3600.0, "1 hr", None),
+ (7200.0, "hrs", Some(3600.0)),
+ (86400.0, "1 day", None),
+ (172800.0, "days", Some(86400.0)),
+ ];
+
+ for (index, format) in time_formats.iter().enumerate() {
+ if secs >= format.0
+ && ((index + 1 < time_formats.len() && secs < time_formats[index + 1].0)
+ || index == time_formats.len() - 1)
+ {
+ match format.2 {
+ None => return Some(format.1.to_string()),
+ Some(divisor) => {
+ return Some(format!("{} {}", (secs / divisor).floor(), format.1));
+ }
+ }
+ }
+ }
+
+ None
+ }
+
+ pub fn format_memory(memory: i64) -> String {
+ if memory >= 1024 * 1024 * 1024 {
+ return format!("{:.1} GiB", memory as f64 / 1024.0 / 1024.0 / 1024.0);
+ }
+
+ if memory >= 1024 * 1024 {
+ return format!("{:.1} MiB", memory as f64 / 1024.0 / 1024.0);
+ }
+
+ if memory >= 1024 {
+ return format!("{} KiB", memory / 1024);
+ }
+
+ format!("{} B", memory)
+ }
+
+ /// @deprecated since Symfony 5.3
+ pub fn strlen_without_decoration(
+ formatter: &mut dyn OutputFormatterInterface,
+ string: &str,
+ ) -> i64 {
+ shirabe_php_shim::trigger_deprecation(
+ "symfony/console",
+ "5.3",
+ "Method \"%s()\" is deprecated and will be removed in Symfony 6.0. Use Helper::removeDecoration() instead.",
+ "Helper::strlenWithoutDecoration",
+ );
+
+ Self::width(&Self::remove_decoration(formatter, string))
+ }
+
+ pub fn remove_decoration(formatter: &mut dyn OutputFormatterInterface, string: &str) -> String {
+ let is_decorated = formatter.is_decorated();
+ formatter.set_decorated(false);
+ // remove <...> formatting
+ let string = formatter.format(Some(string)).unwrap().unwrap_or_default();
+ // remove already formatted characters
+ let string = shirabe_php_shim::preg_replace(php_regex!("/\u{1b}\\[[^m]*m/"), "", &string);
+ // remove terminal hyperlinks
+ let string = shirabe_php_shim::preg_replace(
+ php_regex!("/\u{1b}]8;[^;]*;[^\u{1b}]*\u{1b}\\\\/"),
+ "",
+ &string,
+ );
+ formatter.set_decorated(is_decorated);
+
+ string
+ }
+}