aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/helper/formatter_helper.rs
blob: 8a6c97f63c127e94c59632a7b08a369a41cb4124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! ref: composer/vendor/symfony/console/Helper/FormatterHelper.php

use crate::symfony::console::formatter::output_formatter::OutputFormatter;
use crate::symfony::console::helper::helper::Helper;
use crate::symfony::console::helper::helper_interface::HelperInterface;
use crate::symfony::console::helper::helper_set::HelperSet;
use std::cell::RefCell;
use std::rc::Rc;

/// 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.to_string(),
            section.to_string(),
            style.to_string(),
            message.to_string(),
        )
    }

    /// 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<String> = 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<String> = 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.to_string(),
                messages[i].clone(),
                style.to_string(),
            );
            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<Rc<RefCell<HelperSet>>>) {
        self.inner.set_helper_set(helper_set);
    }

    fn get_helper_set(&self) -> Option<Rc<RefCell<HelperSet>>> {
        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<String>),
}